[Ruby] conditional execution of block / method?

Harry Dean Hudson Jr. dean at ero.com
Fri Jun 8 12:51:05 PDT 2007


On 6/8/07, Ryan Davis <ryand-ruby at zenspider.com> wrote:

> personally I can't stand assignment in conditionals and prefer my
> code to be as clean and clear as possible, so I vote for:
>
>      foo = bar.blah
>
>      foo.do_something unless foo.nil?

Along these same lines (but without separating the assignment):

foo = bar.something and foo.something_else

also does what you're trying to do without mucking about in Object and
in about the same amount of typing as calling your #if_so. The unless
idiom is probably wiser, though. Keeping the assignment and the test
separate is much more clear if you plan on using foo later in your
code.


More information about the Ruby mailing list