test_super_does_not_work_cross_method
Friends,
I’m working my way through the koans and I’m at test_super_does_not_work_cross_method in ruby_koans-master/koans/about_inheritance.rb
I get that super attempts to invoke a method with the same name from the most immediate ancestor in the ancestor hierarchy. Thus george.growl throws a NoMethodError exception because there is no growl method in any of the ancestors.
What I am now curious about is how to implement the intent of the example … that is I want to invoke a cross method in an ancestor. How do I do it?
Here’s my sample code:
class Level1
def a_method
puts "Level1: This is fine"
end
def b_method
puts "Level 1: This is what I want"
end
end
class Level2 < Level1
end
class Level3 < Level2
def a_method
super
puts "Level3: This is fine"
b_method # Produces the "wrong" version of b_method
# I want to do something like the following line
# ancestor.b_method
end
def b_method
puts "Level 3: Not what I want."
end
end
l3 = Level3.new
l3.a_method
And here is the output:
irb(main):026:0* l3 = Level3.new
irb(main):027:0> l3.a_method
Level1: This is fine
Level3: This is fine
Level 3: Not what I want.
=> nil
Ralph