在以下示例中,我在跟踪自我时遇到了一些麻烦:
# a simple class
class Foo; end
# open the Class class to add an instance method
class Class
# breakpoint 1 - self is equal to Class right here, I get why
puts self
# usage: Foo.some_method(1,2,3)
def some_method(*args)
# breakpoint 2 - when we call Foo.some_method(*args)
# self is equal to Foo, and once again I understand why
puts self
args.each do |arg|
# breakpoint 3 - self is still equal to Foo, even though we are calling each
# on an explicit receiver (an array called args)
puts self
end
end
end
那么当我调用args 数组时,为什么self不会改变?each
我的印象是 self 总是等于接收者,那肯定是数组?