[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

alternatives to ? : contruct

John-Mason P. Shackelford

5/11/2005 7:18:00 PM

As an alternative to:

a = y == z ? b : c

we can say:

a = (b if y == x) || c

Can anyone think of others?

I'd really like to be able to say:

a = b if y==z otherwise c

but the precedence of _or_ is not low enough for this to work. Would
changing the precedence of _or_ to be lower than expression modifiers
brake much? I believe _or_ is presently just above the expression
modifiers today. Then again perhaps that would be confusing. Perhaps
an _otherwise_ operator would do the trick.

This is trivial really, but ruby specializes in niceties which many
might deem trivial but which all add up to an amazingly productive
environment.

--
John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
john-mason.shackelford@pearson.com
http://pearsonedmeasu...


20 Answers

Martin DeMello

5/11/2005 7:31:00 PM

0

John-Mason P. Shackelford <jpshack@gmail.com> wrote:
> As an alternative to:
>
> a = y == z ? b : c
>
> we can say:
>
> a = (b if y == x) || c
>
> Can anyone think of others?

a = if y == z then b else c end

martin

ES

5/11/2005 7:32:00 PM

0


Le 11/5/2005, "John-Mason P. Shackelford" <jpshack@gmail.com> a écrit:
>As an alternative to:
>
>a = y == z ? b : c
>
>we can say:
>
>a = (b if y == x) || c
>
>Can anyone think of others?
>
>I'd really like to be able to say:
>
>a = b if y==z otherwise c
>
>but the precedence of _or_ is not low enough for this to work. Would
>changing the precedence of _or_ to be lower than expression modifiers
>brake much? I believe _or_ is presently just above the expression
>modifiers today. Then again perhaps that would be confusing. Perhaps
>an _otherwise_ operator would do the trick.

Is this not called the 'else' operator? :)

>This is trivial really, but ruby specializes in niceties which many
>might deem trivial but which all add up to an amazingly productive
>environment.
>
>John-Mason Shackelford

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }


Paul Battley

5/11/2005 7:34:00 PM

0

> I'd really like to be able to say:
>
> a = b if y==z otherwise c

How about:
a = if (y == z) then b else c end

Paul.


Gyoung-Yoon Noh

5/11/2005 7:35:00 PM

0

I like this one:

a = if condition then x else y end

and same way:

a = case condition when branch_x : value_x when branch_y : value_y
else other_value end


On 5/12/05, John-Mason P. Shackelford <jpshack@gmail.com> wrote:
> As an alternative to:
>
> a = y == z ? b : c
>
> we can say:
>
> a = (b if y == x) || c
>
> Can anyone think of others?
>
> I'd really like to be able to say:
>
> a = b if y==z otherwise c
>
> but the precedence of _or_ is not low enough for this to work. Would
> changing the precedence of _or_ to be lower than expression modifiers
> brake much? I believe _or_ is presently just above the expression
> modifiers today. Then again perhaps that would be confusing. Perhaps
> an _otherwise_ operator would do the trick.
>
> This is trivial really, but ruby specializes in niceties which many
> might deem trivial but which all add up to an amazingly productive
> environment.
>
> --
> John-Mason Shackelford
>
> Software Developer
> Pearson Educational Measurement
>
> 2510 North Dodge St.
> Iowa City, IA 52245
> ph. 319-354-9200x6214
> john-mason.shackelford@pearson.com
> http://pearsonedmeasu...
>
>


--
http://nohmad.su...


Joel VanderWerf

5/11/2005 7:37:00 PM

0

John-Mason P. Shackelford wrote:
...
> I'd really like to be able to say:
>
> a = b if y==z otherwise c

Just kidding:

class Object
def incase(cond)
if cond
self
else
yield
end
end
end

a = 3.incase(1==2) {"c"}
p a
a = 3.incase(1==1) {"c"}
p a


John-Mason P. Shackelford

5/11/2005 7:38:00 PM

0

I don't believe one can use _else_ with an expression modifier. If we
try to use an if block in this case, things get ugly fast:

a = if y == x; b; else; c; end

Of course I may be missing something. I need to read more code than
just my own :s

--
John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
john-mason.shackelford@pearson.com
http://pearsonedmeasu...


James Gray

5/11/2005 7:47:00 PM

0

On May 11, 2005, at 2:37 PM, John-Mason P. Shackelford wrote:

> I don't believe one can use _else_ with an expression modifier. If we
> try to use an if block in this case, things get ugly fast:
>
> a = if y == x; b; else; c; end
>
> Of course I may be missing something. I need to read more code than
> just my own :s

The if ... then ... else ... end statements people have been posting
are perfectly legal Ruby. Fire up irb an give them a spin. It's
basically just collapsing an if statement to one line, then capturing
the proper return (since statements return things in Ruby). The
"then" is just a pretty replacement for the ; here.

Hope that helps.

James Edward Gray II



threeve.org

5/11/2005 8:08:00 PM

0

On 5/11/05, James Edward Gray II <james@grayproductions.net> wrote:
> On May 11, 2005, at 2:37 PM, John-Mason P. Shackelford wrote:
>
> > I don't believe one can use _else_ with an expression modifier. If we
> > try to use an if block in this case, things get ugly fast:
> >
> > a = if y == x; b; else; c; end
> >
> > Of course I may be missing something. I need to read more code than
> > just my own :s
>
> The if ... then ... else ... end statements people have been posting
> are perfectly legal Ruby. Fire up irb an give them a spin. It's
> basically just collapsing an if statement to one line, then capturing
> the proper return (since statements return things in Ruby). The
> "then" is just a pretty replacement for the ; here.
>
> Hope that helps.
>
> James Edward Gray II
>
>

I'd prefer it with less ;'s as well:

a = if (y==z): b else c end

which works perfectly fine:

irb(main):025:0> a = if (1==2): 'foo' else 'bar' end
=> "bar"

I really don't like the idea of having an else/otherwise/whatever
operator that allows the results of the statement to be the first and
last pieces, and the operators and conditions all squished in the
middle. B if A else C doesn't read well to me, and would get
confusing I think, whereas if A then B else C is very natural.


Jason


Martin DeMello

5/11/2005 8:15:00 PM

0

Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
>
> Just kidding:
>
> class Object
> def incase(cond)
> if cond
> self
> else
> yield
> end
> end
> end
>
> a = 3.incase(1==2) {"c"}
> p a
> a = 3.incase(1==1) {"c"}
> p a

That's only lazy in its second argument :) Maybe

def only(&block)
block
end

class Proc
def if(arg)
arg ? self.call : arg
end
end

class Object
def then
self ? yield : self
end

def else
self ? self : yield
end

alias :otherwise :else
end

p (1==1).then { 5 }.else { 1/0 }

p only {1/0}.if(1 == 2).otherwise { 4 }

martin

Gary Wright

5/11/2005 8:22:00 PM

0

On May 11, 2005, at 4:07 PM, Jason Foreman wrote:
>
> I'd prefer it with less ;'s as well:
>
> a = if (y==z): b else c end

Is this really better than:

a = (y==z) ? b : c

?

The tertiary operator ?: can be used if you want
something terse and "if then else end" if you want something
more verbose. I'm not sure I see the need for yet another
variation.


Gary Wright