josh
November 23, 2007, 12:21am
1
Hi all
module MyModule
def my_method
return “my module rocks!”
end
end
class MyClass
include(MyModule)
def my_method
return “my class rocks!”
end
end
Is there a way to call MyModule::my_method from within MyClass? “super”
sadly doesn’t work…
Thanks a lot
Josh
josh
November 23, 2007, 2:33am
2
On Nov 22, 2007, at 6:21 PM, Joshua M. wrote:
def my_method
return “my class rocks!”
end
end
Is there a way to call MyModule::my_method from within MyClass?
“super”
sadly doesn’t work…
Perhaps this will do:
module MyModule
def my_method
"my module rocks!"
end
end
class MyClass
include MyModule
alias module_method my_method
def my_method
“#{module_method}\nmy class rocks!”
end
end
puts MyClass.new.my_method
my module rocks!
my class rocks!
Regards, Morton
josh
November 23, 2007, 3:30am
3
return "my class rocks!"
end
end
Is there a way to call MyModule::my_method from within
MyClass? “super”
sadly doesn’t work…
You’d need to alias the old method:
class MyClass
include(MyModule)
alias :old_my_method :my_method
def my_method
old_my_method
end
end
(If you’re using rails/activesupport, you’d probably use
alias_method_chain to simplify things)
Dan.
josh
November 23, 2007, 1:22pm
4
(If you’re using rails/activesupport, you’d probably use
alias_method_chain to simplify things)
Dan.
Thanks! Especially for the alias_method_chain hint!
josh
November 23, 2007, 1:31pm
5
On Nov 23, 2007 12:21 AM, Joshua M. [email protected] wrote:
def my_method
return “my class rocks!”
end
end
Is there a way to call MyModule::my_method from within MyClass? “super”
sadly doesn’t work…
super shall work and indeed does
module MyModule
def my_method
return “my module rocks!”
end
end
class MyClass
include(MyModule)
def my_method
return [ super, "my class rocks!" ].join("\n")
end
end
puts MyClass.new.my_method
HTH
Robert