如何在ruby中的块中使用实例变量和方法?
class Foo
@var = "my var"
def meth()
"my method"
end
Module.something do |variable|
p @var #=> Undefined
p meth() #=> Undefined
end
end
答案 0 :(得分:0)
我不确定我是否完全理解上下文,但这可能有所帮助:
class Foo
def initialize
@var = "my var"
end
def meth()
"my method"
end
def fee
AModule.something do |variable|
p @var
p meth()
p variable
end
end
end
# test:
module AModule
def self.something
yield "bar"
end
end
foo = Foo.new
foo.fee