How to prepend classmethods
This question directly relates to this one. But I tried to break it down
to the base problem and I didn't want to enter even more text into the
other question box. So here goes:
I know that I can include classmethods by extending the module
ClassMethods and including it via the Module#include hook. But can I do
the same with prepend? Here is my example:
class Foo:
class Foo
def self.bar
'Base Bar!'
end
end
class Extensions:
module Extensions
module ClassMethods
def bar
'Extended Bar!'
end
end
def self.prepended(base)
base.extend(ClassMethods)
end
end
# prepend the extension
Foo.send(:prepend, Extensions)
class FooE:
require './Foo'
class FooE < Foo
end
and a simple startscript:
require 'pry'
require './FooE'
require './Extensions'
puts FooE.bar
When I start the script I don't get Extended Bar! like I expect but rather
Base Bar!. What do I need to change in order to work properly?
No comments:
Post a Comment