P
Phil Tomson
The following doesn't quite do what I would expect:
module A
def foo
"A::foo"
end
end
module B
def foo
"B::foo"
end
end
class Demo
def to_a
self.extend(A)
end
def to_b
self.extend(B)
end
def initialize
self.extend(A)
end
end
d = Demo.new
puts d.foo #=> A::foo
d.to_b
puts d.foo #=> B::foo
d.to_a
puts d.foo #=> B::foo (but I expected A::foo)
OK, I can kind of see why this happended; B's foo 'hide's A's foo method. But
I'm not sure why it didn't happen in the second 'puts' above (ie. why
wouldn't it just keep printing A::foo every time?). I think the answer is that
A has already been mixed-in so it's not really mixed-in again (true?).
And how would I go about making this work so that it prints:
A::foo
B::foo
A::foo
?
Phil
module A
def foo
"A::foo"
end
end
module B
def foo
"B::foo"
end
end
class Demo
def to_a
self.extend(A)
end
def to_b
self.extend(B)
end
def initialize
self.extend(A)
end
end
d = Demo.new
puts d.foo #=> A::foo
d.to_b
puts d.foo #=> B::foo
d.to_a
puts d.foo #=> B::foo (but I expected A::foo)
OK, I can kind of see why this happended; B's foo 'hide's A's foo method. But
I'm not sure why it didn't happen in the second 'puts' above (ie. why
wouldn't it just keep printing A::foo every time?). I think the answer is that
A has already been mixed-in so it's not really mixed-in again (true?).
And how would I go about making this work so that it prints:
A::foo
B::foo
A::foo
?
Phil