I have read quite a few answers here in this regards, but I still cant figure out why the following doesn't work
module A
def a
puts 'hi'
end
end
module B
extend A
end
class C
extend B
def b
a
end
end
C.new.b # undefined local variable or method `a' for #<C:...
I've also tried with:
module B
def self.included(recipient)
recipient.extend A
end
end
Hoping C will get extended (but I guess the hierarchy is then wrong)
Important: The Problem is that I have a module that requires to be extended memoist, and I want to add some functionality to it.
How can I achieve that and why is the first example not working?
extends adds the methods from the module passed as argument as 'class methods'. What you're looking for is include, which adds methods from (in this case) A module as instance methods, just like you want it to:
module A
def a
puts 'hi'
end
end
module B
include A
end
class C
include B
def b
a
end
end
C.new.b
# hi
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