在块/lambda 内产生问题
我有以下 Ruby 代码:
# func1 generates a sequence of items derived from x
# func2 does something with the items generated by func1
def test(x, func1, func2)
func1.call(x) do | y |
func2.call(y)
end
end
func1 = lambda do | x |
for i in 1 .. 5
yield x * i
end
end
func2 = lambda do | y |
puts y
end
test(2, func1, func2) # Should print '2', '4', '6', '8', and '10'
当然,这是行不通的。
test.rb:11: no block given (LocalJumpError)
from test.rb:10:in `each'
from test.rb:10
from test.rb:4:in `call'
from test.rb:4:in `test'
from test.rb:20
I have the following Ruby code:
# func1 generates a sequence of items derived from x
# func2 does something with the items generated by func1
def test(x, func1, func2)
func1.call(x) do | y |
func2.call(y)
end
end
func1 = lambda do | x |
for i in 1 .. 5
yield x * i
end
end
func2 = lambda do | y |
puts y
end
test(2, func1, func2) # Should print '2', '4', '6', '8', and '10'
This does not work, of course.
test.rb:11: no block given (LocalJumpError)
from test.rb:10:in `each'
from test.rb:10
from test.rb:4:in `call'
from test.rb:4:in `test'
from test.rb:20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Lambda 不像常规方法那样隐式接受块,因此您的
func1
无法产生。这样做:具体来说,我相信这是因为yield 会将控制发送回调用者的块,其中不包括 lambda 调用。所以下面的代码就像你“期望的”一样工作:
Lambdas don't implicitly accept blocks like regular methods do, so your
func1
can't yield. Do this instead:Specifically, I believe this is because yield would send control back to the
caller
's block, which would not include lambda invocations. So the following code works like you "expect":仅在 Ruby 1.9 中:
In Ruby 1.9 only:
根据 Nikita Misharin 的回答:[https://stackoverflow.com/a/45571976/2165560],我喜欢这:
它在这里回答了我的问题 [在 Ruby 中,可以使用 lambda 或 Proc 调用方法来调用迭代器吗?。
通过包装迭代器,它可以任意传递给其他方法并迭代它们的块。
Based upon Nikita Misharin's answer here:[https://stackoverflow.com/a/45571976/2165560], I like this:
It answers my question here [In Ruby, can you use the lambda or or Proc call method to invoke an iterator?.
By wrapping the iterator it can be arbitrarily passed to other methods and iterate on their blocks.