【发布时间】:2015-03-03 23:11:00
【问题描述】:
我有以下 Ruby 代码:
class Baz
def foo()
qux = Class.new() {
def call()
bar()
end
}.new()
qux.call()
end
def bar()
puts "bar"
end
end
b = Baz.new()
b.foo()
如何从匿名类访问方法bar,即从qux.call?有可能吗?
我不断收到这条消息:
classes-test.rb:5:in `call': undefined method `bar' for #<#<Class:0x00000002d9c248>:0x00000002d9c1a8> (NoMethodError)
我是 Ruby 新手,因此我们将不胜感激任何建议甚至更深入的问题解释。提前致谢。
【问题讨论】:
标签: ruby closures anonymous-class