[Ruby] conditional execution of block / method?
Ryan Davis
ryand-ruby at zenspider.com
Fri Jun 8 10:12:15 PDT 2007
On Jun 8, 2007, at 06:47 , Bastiaan wrote:
> I have been coding Ruby for a little while now, and I find myself
> doing this sometimes:
>
> if(foo=bar.something)
> foo.something_else
> end
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?
btw, the space bar and return key are your friends.
as far as your #if_so goes:
> # This allows you to only call a method or block on something that is
> true.
> class Object
> # calls method or yields block if self
> def if_so(method=nil, *args, &block)
> if self
> block_given? ? yield(self) : self.send(method.to_sym, args)
> end
> end
> end
>
> bar.something.if_so.something_else
do you see the bug already?
that and this smells of "clever", which has been my antithesis lately...
More information about the Ruby
mailing list