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.denhaven2.com/