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.
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.
On 1/13/08, Junkone [email protected] 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.denhaven2.com/
Rick DeNatale wrote:
result = IIF(left==right,returntrueval, returnfalseval)
result = left == right ? trueval : falseval
Or result = if left == right then trueval else falseval end
On Jan 13, 2008 4:44 PM, Junkone [email protected] 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
On Jan 13, 1:11 pm, Robert D. [email protected] 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
On Jan 13, 2008 8:00 PM, Rick DeNatale [email protected] wrote:
On 1/13/08, Junkone [email protected] 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 DeNataleMy blog on Ruby
http://talklikeaduck.denhaven2.com/
–
http://ruby-smalltalk.blogspot.com/
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein
On 1/13/08, Robert D. [email protected] wrote:
result = left == right ? trueval : falseval
might seem strange I like case at the end of a method for returning
different values a lotdef ... ... 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.denhaven2.com/
On Jan 14, 2008 2:11 PM, Rick DeNatale [email protected] 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.blogspot.com/
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein
On Jan 14, 2008 2:35 AM, MonkeeSage [email protected] wrote:
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.blogspot.com/
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs