is iif available

J

Junkone

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.
 
R

Rick DeNatale

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
 
V

Vitor Peres

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

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
 
S

Sebastian Hungerecker

Rick said:
=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: (e-mail address removed)
ICQ: 205544826
 
R

Robert Dober

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
 
M

MonkeeSage

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
 
R

Robert Dober

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
 
R

Rick DeNatale

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.
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top