将实例方法发送到模块
给定以下模块,
module Foo
def bar
:baz
end
end
def send_to_foo(method)
# ...?
end
send_to_foo(:bar) # => :baz
send_to_foo
中应该包含哪些代码才能使最后一行按预期工作? (send_to_foo
显然不是我实现这个的方式;它只是让我更清楚我在寻找什么。)
我期望 Foo.send(:bar)
首先能够工作,但事实并非如此。如果该方法被定义为 def self.bar 就会出现这种情况,但这并不有趣。
Given the following module,
module Foo
def bar
:baz
end
end
def send_to_foo(method)
# ...?
end
send_to_foo(:bar) # => :baz
What code should go in send_to_foo
to make the last line work as expected? (send_to_foo
is obviously not how I would implement this; it just makes clearer what I'm looking for.)
I expected Foo.send(:bar)
to work at first, but it makes sense that it doesn't. It would if the method were defined as def self.bar
, but that's no fun.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,最简单的方法就是
现在
well, the easy way would be
So now