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
Rails with Ruby-Debugger Throw 'Symbol Not Found: _Ruby_Current_Thread (Loaderror)'
Colon (:) Appears as Forward Slash (/) When Creating File Name
Looping Through Bits in an Integer, Ruby
String#Encode Not Fixing "Invalid Byte Sequence in Utf-8" Error
Ruby 'Encode': "\Xc3" from Ascii-8Bit to Utf-8 (Encoding::Undefinedconversionerror)
What's the Point of Argv in Ruby
Google Maps, Ruby on Rails, Zoom Level with One Marker
Block Syntax Difference Causes "Localjumperror: No Block Given (Yield)"
How to Use Rspec's Should_Raise with Any Kind of Exception
Rails - the System Cannot Find the Path Specified
How to Get the Name of the Command Called for Usage Prompts in Ruby
How to Find the Key of the Largest Value Hash
Rails 4 Unpermitted Parameters for Array
Trying to Set Up Postgres for Ror App, Getting Error - Fe_Sendauth: No Password Supplied