From: Austin Ziegler Date: 2007-09-26T01:59:18+09:00 Subject: Re: so surprised syntax error not caught here On 9/25/07, SpringFlowers AutumnMoon wrote: > the following syntax error for "elif" is not caught? (should be elsif) > > def foo(i) > if i < 0 > return "Less than 0" > elif i == 0 > return "it is zero" > else > return "Greater than 0" > end > end > > p foo(-2) > p foo(0) > p foo(10) It's treating "elif" as a (possible) method call that's never reached because of the "return". You don't need "return" for what you have: def foo(i) if i < 0 "Less than 0" elif i.zero? # i == 0 "Equal to 0" else "Greater than 0" end end That will fail, but not with a syntax error. (It should fail with an UnknownMethod.) Ruby is expression-oriented, and the last expression executed in the execution path is returned from the execution path. -austin -- Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/ * austin@halostatue.ca * http://www.halostatue.ca/feed/ * austin@zieglers.ca