我可以动态定义采用块的 Ruby 方法吗?
我知道我可以使用 define_method
在类上动态定义方法,并且我使用块的数量指定该方法采用的参数。
我想动态定义一个同时接受可选参数和块的方法。在 Ruby 1.9 中,这很容易,因为现在允许将块传递给块。
不幸的是,Ruby 1.8 不允许这样做,因此以下内容将不起作用:
#Ruby 1.8
class X
define_method :foo do |bar, &baz|
puts bar
baz.call if block_given?
end
end
x = X.new
x.foo("foo") { puts "called!"} #=> LocalJumpError: no block given
用 yield
替换显式 block.call
也无法解决问题。
不幸的是,升级到 Ruby 1.9 对我来说不是一个选择。这是一个棘手的问题,还是有办法解决它?
I know that I can dynamically define methods on a class using define_method
, and that I specify the parameters this method takes using the arity of the block.
I want to dynamically define a method that accepts both optional parameters and a block. In Ruby 1.9, this is easy because passing a block to a block is now allowed.
Unfortunately, Ruby 1.8 doesn't allow this, so the following won't work:
#Ruby 1.8
class X
define_method :foo do |bar, &baz|
puts bar
baz.call if block_given?
end
end
x = X.new
x.foo("foo") { puts "called!"} #=> LocalJumpError: no block given
Replacing the explicit block.call
with yield
doesn't fix the problem either.
Upgrading to Ruby 1.9 is unfortunately not an option for me. Is this an intractable problem, or is there a way around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这适用于 Ruby 1.8.7,但不适用于 1.8.6:
测试使用:
给出:(
在 1.8.6 中它给出
语法错误,意外的 tAMPER,期望 '|'
。)如果您想要可选参数除了阻止之外,您还可以尝试这样的操作:
testing with:
给出:
This works with Ruby 1.8.7, but not 1.8.6:
Testing with:
gives:
(with 1.8.6 it gives
syntax error, unexpected tAMPER, expecting '|'
.)If you want optional arguments as well as block, you could try something like this:
testing with:
gives:
您可以做的是将
class_eval
与字符串一起使用,而不是define_method
。这样做的缺点(除了不那么优雅之外)是你失去了词法范围。但这通常是不需要的。What you could do is use
class_eval
with a string instead ofdefine_method
. The downside to this (apart from not being as elegant) is that you lose lexical scoping. But this is often not needed.