0

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

# This works

method :argument do
  other_method
end

# This does not

method :argument {
  other_method
}

Why?

It seems like the interpreter is confused and thinks that the { ... } is a hash.

I always get angry when an interpreter can't understand a code that is actually valid. It resembles PHP that had many problems of this kind.

Community
  • 1
  • 1
tillda
  • 18,150
  • 16
  • 51
  • 70
  • This is a duplicate of [Ruby Block Syntax Error](http://StackOverflow.Com/q/6854283/), [Code block passed to `each` works with brackets but not with `do`-`end` (ruby)](http://StackOverflow.Com/q/6718340/), [Block definition - difference between braces and `do`-`end` ?](http://StackOverflow.Com/q/6179442/), [Ruby multiline block without `do` `end`](http://StackOverflow.Com/q/3680097/), [Using `do` block vs brackets `{}`](http://StackOverflow.Com/q/2122380/), [What is the difference or value of these block coding styles in Ruby?](http://StackOverflow.Com/q/533008/), … – Jörg W Mittag Oct 01 '11 at 14:27
  • … [Ruby block and unparenthesized arguments](http://StackOverflow.Com/q/420147/) and [Why aren't `do`/`end` and `{}` always equivalent?](http://StackOverflow.Com/q/7487664/). – Jörg W Mittag Oct 01 '11 at 14:27
  • 2
    I'd love to see *you* write a parser that does the right thing every time. –  Oct 01 '11 at 14:28
  • Your title's spelling has weird imperfections. – Andrew Grimm Oct 01 '11 at 23:04

1 Answers1

4

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675