块语法差异导致"LocalJumpError:未给出块(产量)". [英] block syntax difference causes "LocalJumpError: no block given (yield)"

查看:78
本文介绍了块语法差异导致"LocalJumpError:未给出块(产量)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到一个奇怪的案子,试图弄清楚这里发生了什么:

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)

推荐答案

解析器识别出这一点

p test do
  1
end

是这样

p(test) do
  1
end

该块将传递给p,而不是test.因此,yield无法屈服并引发该错误.

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

这篇关于块语法差异导致"LocalJumpError:未给出块(产量)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆