我可以动态定义采用块的 Ruby 方法吗?

发布于 2024-12-06 22:30:11 字数 513 浏览 7 评论 0原文

我知道我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-12-13 22:30:12

这适用于 Ruby 1.8.7,但不适用于 1.8.6:

class X
  define_method(:foo) do |bar, &baz|
    puts bar
    baz.call if baz
  end
end

测试使用:

X.new.foo("No block")
X.new.foo("With block") { puts "  In the block!"}
p = proc {puts "  In the proc!"}
X.new.foo("With proc", &p)

给出:(

No block
With block
  In the block!
With proc
  In the proc!

在 1.8.6 中它给出语法错误,意外的 tAMPER,期望 '|'。)

如果您想要可选参数除了阻止之外,您还可以尝试这样的操作:

class X
  define_method(:foo) do |*args, &baz|
    if args[0]
      bar = args[0]
    else
      bar = "default"
    end
    puts bar
    baz.call if baz
  end
end

testing with:

X.new.foo
X.new.foo { puts "  No arg but block"}

给出:

default
default
  No arg but block

This works with Ruby 1.8.7, but not 1.8.6:

class X
  define_method(:foo) do |bar, &baz|
    puts bar
    baz.call if baz
  end
end

Testing with:

X.new.foo("No block")
X.new.foo("With block") { puts "  In the block!"}
p = proc {puts "  In the proc!"}
X.new.foo("With proc", &p)

gives:

No block
With block
  In the block!
With proc
  In the proc!

(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:

class X
  define_method(:foo) do |*args, &baz|
    if args[0]
      bar = args[0]
    else
      bar = "default"
    end
    puts bar
    baz.call if baz
  end
end

testing with:

X.new.foo
X.new.foo { puts "  No arg but block"}

gives:

default
default
  No arg but block
|煩躁 2024-12-13 22:30:12

您可以做的是将 class_eval 与字符串一起使用,而不是 define_method。这样做的缺点(除了不那么优雅之外)是你失去了词法范围。但这通常是不需要的。

What you could do is use class_eval with a string instead of define_method. The downside to this (apart from not being as elegant) is that you lose lexical scoping. But this is often not needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文