From: Robert Klemme Date: 2006-09-28T03:46:19+09:00 Subject: Re: Ahh, the hazards of an interpretive language... Pete wrote: > This sort of thing has most probably been noted and reported by other > people before, but as a relative newcomer to ruby I found it amusing... > > At a point in the code I was writing I found I wanted an 'alternative if', > so I glanced at the quick-reference and wrote something like this (the > actual code being more useful, of course): > > def tt x > if x == :this > return "got this" > elseif x == :that > return "got that" > end > ## ... more actions if neither is true > end Btw, the typical solution in Ruby would be to use a Hash. Then tt will boil down to "def tt(x) your_hash[x] end". The next best solution (and also more similar to what you did) is a case statement def tt x case x when :this then "got this" when :that then "got that" else "foo" end end Note: semantics are not identical since this case statement uses "===" for evaluation of cases. If you want identical behavior you can use case when x == :this then "got this" ... end > and was totally flummoxed for half an hour or so because the second > {':that') action was never being executed! No error messages, ever, > so I figured the syntax *must* be OK... (:-/) [Ruby up to that point > had been superb at identifying errors.] Well, the syntax *is* ok. > Oh well. At least it's the sort of slip one doesn't repeat. Certainly. :-) Kind regards robert