The reason is that nested method are always applied to the containing class. The second method will be added to the A
class, since it's the containing class of the first method.
However, it will be added only when its body code is evaluated, and it is evaluated when you first call the outer method.
class Test
def foo
puts "outside method"
def bar
puts "inside method"
end
end
end
test = Test.new
test.methods - Object.methods # => [:foo]
test.foo
test.methods - Object.methods # => [:foo, :bar]
In ruby, defining a method with the same name within a class twice will override the first version.
class Test
def foo
"first version"
end
def foo
"second version"
end
end
test = Test.new
test.foo # => "second version"
Therefore, after the first execution of A#a
, the inner implementation remains.