我有一个带有初始化方法的Ruby类:
def initialize(params)
@foo = private_method(params || {})
end
稍后在同一课程中,我看到以下内容:
def new_method_for(user)
foo.each { |f| other_method(f) }
end
为什么在@
中foo前面缺少other_method
?当我在foo.each...
之前放入binding.pry时,foo和@foo都被定义了。
答案 0 :(得分:1)
检查包含new_method_for(user)
方法的类,您应该看到attr_reader
,attr_writer
或两者都由attr_accessor
所以看起来应该是这样的:
class SomeClass
attr_accessor :foo
end