Reputation: 9939
Sample code:
module B
def f2
puts "B::f2"
end
end
class C
def initialize
extend B
end
def f2
puts "C::f2"
end
end
c = C.new
c.f2
Above sample code is an abstraction of my problem. Class C
extends module B
on fly (B
is actually extended to instance of C
). Method f2
from B
doesn't meet my needs, so I want to overwrite f2
. How to implement that?
Upvotes: 2
Views: 179
Reputation: 2877
I actually don't like extending in initialize
. There are many reasons to implement "plugin" in different way.
But if you want to "shoot in the foot" this way, ok, just do one more extend:
module B
def f2
puts 'B::f2'
end
end
class C
attr_reader :parent_state
def initialize
extend B
extend BOverrides
@parent_state = 'Parent State'
end
module BOverrides
def f2
puts 'C::f2'
puts 'Yes, I have access to %s' % parent_state
end
end
end
c = C.new
c.f2
Upvotes: 2