Marcelo Alvim
9/27/2006 6:31:00 AM
On 9/27/06, Pete <neverland@jwgibbs.cchem.berkeley.edu> wrote:
> def tt x
> if x == :this
> return "got this"
> elseif x == :that
> return "got that"
> end
> ## ... more actions if neither is true
> end
I've never fallen into that trap but, as a complete newbie to Ruby, I
was bound to, sooner or later. The problem is, even after reading your
code and explanation, I wasn't able to really 'get' what was
happening. So I fired up irb, did some testing and finally understood.
So, for the sake of other newbies like me, I'll post a code that is
similar to yours, with little 'didatic' changes. I know it's just what
you were trying to explain, I'm only putting it another way in order
to (hopefully) help someone who, like me, couldn't 'get it' the first
time:
def tt(x)
if x == :this
return "got this"
zoinks #notice how the interpreter doesn't
zinks #care about these lines
return "got that" #neither does he care about this one
end
return "got something else"
end
So:
tt(:this) #=> "got this"
tt(:anything_else) #=> "got something else"
Thanks, Pete, for bringing that up. I'm sure I'd fall into this one at
some point. :)
Cheers,
Alvim.