Search code examples
rubyblock

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


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)

Solution

  • 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.