block syntax difference causes "LocalJumpError: no block given (yield)"

Brian Armstrong picture Brian Armstrong · Sep 4, 2013 · Viewed 17.9k times · Source

Saw a strange case come up, trying to figure out what is happening here:

> def test
>   p yield
> end
=> nil
> test { 1 }
1
=> 1
> p test { 1 }
1
1
=> 1
> p test do
>   1
> end
LocalJumpError: no block given (yield)

Answer

Sergio Tulentsev picture Sergio Tulentsev · Sep 4, 2013

The parser recognizes this

p test do
  1
end

as this

p(test) do
  1
end

The block is passed to p, not test. Therefore, yield can't yield and raises that error.