Given a module with a singleton method like this:
module Foo
class << self
def bar
puts "method bar from Foo"
end
end
end
How can I override Foo.bar using another module?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Extend and alias
My problem was that I forgot to think through the inheritance chain. I was
looking for a way to override the method by modifying the inheritance chain, but
that’s not possible.
The reason is that
baris defined onFooitself, so it never looks up its inheritance chain for the method. Therefore, to changebar, I have to change it onFooitself.While I could just re-open Foo, like this:
… I prefer a way to be able to wrap the original
barmethod, as though Iwere subclassing and could call
super. I can achieve that by setting up analias for the old method.