Weird Imoperfection in Ruby Blocks

Weird imoperfection in Ruby blocks

It doesn't think it's a hash - it's a precedence issue. {} binds tighter than do end, so method :argument { other_method } is parsed as method(:argument {other_method}), which is not syntactically valid (but it would be if instead of a symbol the argument would be another method call).

If you add parentheses (method(:argument) { other_method }), it will work fine.

And no, the code is not actually valid. If it were, it would work.

What is the difference or value of these block coding styles in Ruby?

The rails team and many other rubyists prefer to use curly braces for one line blocks and do...end for multi-line ones.

The only functional difference between the two is that the precedence of a do...end block is lower than that of a {...} block.



Related Topics



Leave a reply



Submit