Hi --

On Tue, 21 Jan 2003, Eric Hodel wrote:

> Dmitri Colebatch (dim / colebatch.com) wrote:
>
> > how should I be doing the above?  I ended up doing this:
> >
> >   def foo(foobar)
> >     bar =
> >       if foobar != nil
> >         bar = foobar * 4
> >       else
> >         bar = "foobar!"
> >       end
> >     bar
> >   end
> >
> > which worked fine, but I was wondering if there was a way to do what I
> > was trying initially.
>
> Yes, the value of the last statement in an if/elsif/else/end block is
> the value of the if :)  So you can just do this:
>
> def foo(foobar)
>   bar =
>    if foobar != nil
>      foobar * 4
>    else
>      bar = "foobar!"

(You forgot to erase that last "bar =" :-)

I thought along with the if/else and ?: versions I'd throw in:

  def foo(foobar)
    foobar.nil? && "foobar!" || foobar * 4
  end


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav