[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

is iif available

Junkone

1/13/2008 6:43:00 PM

is there a IIF equavalent in Ruby. I could not find one
I am looking for something like
result=IIF(left==right,returntrueval, returnfalseval)

I can wriet a quick function to do it but if something exists, i dont
want to reinvent the wheel.
8 Answers

Rick DeNatale

1/13/2008 7:00:00 PM

0

On 1/13/08, Junkone <junkone1@gmail.com> wrote:
> is there a IIF equavalent in Ruby. I could not find one
> I am looking for something like
> result=IIF(left==right,returntrueval, returnfalseval)
>
> I can wriet a quick function to do it but if something exists, i dont
> want to reinvent the wheel.

result = left == right ? trueval : falseval


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Vitor Peres

1/13/2008 7:01:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Jan 13, 2008 4:44 PM, Junkone <junkone1@gmail.com> wrote:

> is there a IIF equavalent in Ruby. I could not find one
> I am looking for something like
> result=IIF(left==right,returntrueval, returnfalseval)
>
> I can wriet a quick function to do it but if something exists, i dont
> want to reinvent the wheel.
>
>
There's the ternary operator, familiar to those from a C (and derivatives)
background:

result = left == right ? trueval : falseval

--
Vitor Peres (dodecaphonic)
------------------------------------
http://twitter.com/do...
http://dodecaphonic.bl...

Sebastian Hungerecker

1/13/2008 7:05:00 PM

0

Rick DeNatale wrote:
> > result =3D IIF(left=3D=3Dright,returntrueval, returnfalseval)
>
> =A0 =A0result =3D left =3D=3D right ? trueval : falseval

Or result =3D if left =3D=3D right then trueval else falseval end


=2D-=20
NP: AEBA - Gottesmord
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Dober

1/13/2008 7:11:00 PM

0

On Jan 13, 2008 8:00 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
>
> On 1/13/08, Junkone <junkone1@gmail.com> wrote:
> > is there a IIF equavalent in Ruby. I could not find one
> > I am looking for something like
> > result=IIF(left==right,returntrueval, returnfalseval)
> >
> > I can wriet a quick function to do it but if something exists, i dont
> > want to reinvent the wheel.
>
> result = left == right ? trueval : falseval
and often overseen
result = if whatever then
some_more_whatever
trueval
else
even_more_whatever
falseval
end

which in simle cases may look like

clever = if your_answer == 42 then
"very"
else
"not so much"
end

please note that you can use elsifs or case statements in the same sense
although
result = case ...
end
might seem strange I like case at the end of a method for returning
different values a lot

def ...
...
case ...
when 42
"clever"
else
"less so"
end*

HTH
Robert
>
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

MonkeeSage

1/14/2008 1:32:00 AM

0

On Jan 13, 1:11 pm, Robert Dober <robert.do...@gmail.com> wrote:

> Whereof one cannot speak, thereof one must be silent.
> Ludwig Wittgenstein

OT:
I like Wittgenstein's penultimate point in the expanded final edition
of TLP (viz., 6.54), rather than the final original point in your sig
(viz., 7.00). I.e., "My propositions are elucidatory in this way: he
who understands me finally recognizes them as senseless, when he has
climbed out through them, on them, over them. (He must so to speak
throw away the ladder, after he has climbed up on it.)" ;)

Regards,
Jordan

Robert Dober

1/14/2008 11:37:00 AM

0

On Jan 14, 2008 2:35 AM, MonkeeSage <MonkeeSage@gmail.com> wrote:
> On Jan 13, 1:11 pm, Robert Dober <robert.do...@gmail.com> wrote:
>
> > Whereof one cannot speak, thereof one must be silent.
> > Ludwig Wittgenstein
>
> OT:
> I like Wittgenstein's penultimate point in the expanded final edition
> of TLP (viz., 6.54), rather than the final original point in your sig
> (viz., 7.00). I.e., "My propositions are elucidatory in this way: he
> who understands me finally recognizes them as senseless, when he has
> climbed out through them, on them, over them. (He must so to speak
> throw away the ladder, after he has climbed up on it.)" ;)

Not that much OT: I have chosen my sig as such because it struck me
that the least I could do was to admit that my posts are too long and
sometimes too frequent, that must be my way of thinking during
writing, I could also have used:
"How can I know what I mean before I read what I have written. -- unknown"
But I am alas most unfamiliar with all philosophical work and cannot
do anything than scratch the surface :(.

Cheers
Robert
>
> Regards,
> Jordan
>
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Rick DeNatale

1/14/2008 1:12:00 PM

0

On 1/13/08, Robert Dober <robert.dober@gmail.com> wrote:
> On Jan 13, 2008 8:00 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
> >
> > On 1/13/08, Junkone <junkone1@gmail.com> wrote:
> > > is there a IIF equavalent in Ruby. I could not find one
> > > I am looking for something like
> > > result=IIF(left==right,returntrueval, returnfalseval)
> > >
> > > I can wriet a quick function to do it but if something exists, i dont
> > > want to reinvent the wheel.
> >
> > result = left == right ? trueval : falseval
> and often overseen
> result = if whatever then
> some_more_whatever
> trueval
> else
> even_more_whatever
> falseval
> end
>
> which in simle cases may look like
>
> clever = if your_answer == 42 then
> "very"
> else
> "not so much"
> end
>
> please note that you can use elsifs or case statements in the same sense
> although
> result = case ...
> end
> might seem strange I like case at the end of a method for returning
> different values a lot
>
> def ...
> ...
> case ...
> when 42
> "clever"
> else
> "less so"
> end*

Well I was tempted to reply with such a litany in my original post,
but decided that the ternary sytax was probably closest to what the OP
was looking for.

For the most part which of the alternatives to choose is a question of
style, I've always valued brevity all other things being equal.

On the other hand, there's also an interaction with the tools you use.
I've had a tendency of late to use

if x
then
y
else
z
end

rather than x ? y :z

in many cases because the latter, being only one line makes it
impossible to discriminate the two legs for tools like rcov.

OTOH, I personally don'f find much merit in the one like form

if x then y else z

But that's just my personal opinion.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Robert Dober

1/14/2008 4:14:00 PM

0

On Jan 14, 2008 2:11 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
For me it is the choice Ruby gives us which is interesting, the
ternary operator is limitted but I would use it whenever it suffices.
I just wanted to put emphasis on the fact that if and case are indeed
full fledged expressions, something OP did not seem to be aware of.
As I mentioned before using the value of a case expression has often
proved useful for me.

But just to be clear very often ? : will be all one needs.

Cheers
Robert



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein