class Foo
def initialize(a)
puts "Hello #{a}"
end
end
module Bar
def initialize(b)
puts "#{b} World"
end
end
class Sample < Foo
include Bar
def initialize(c)
super
end
end
Sample.new('qux') #=> qux World
Why output is not 'Hello qux' ? credit for code
When you include a module into a class, it acts as those you've inserted a new superclass in the class hierarchy, just between Sample and Foo. Calls to super() hunt through included modules before falling back to the real superclass (Foo).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With