M
MaggotChild
I'm monkey patching 2 methods of an existing module: some_method() and
another_method()
Calling some_method() on an instance of class that includes the
original module works fine (calls the monkey patched version), but
when I call another_method() the original (not the monkey patched)
method is called.
There is a lot of code, and I've tried to recreate the series of
includes with the below example, yet the example works!
Given this, I hope that someone could still provide me with some
insight as to why this would happen
#original_module.rb
module ToBePatched
module A
def some_method
p 'original some'
end
def another_method
p 'original anotehr'
end
end
end
#original_class.rb
module ToBePatched
class Foo
include ToBePatched::A
end
end
#my_override.rb
module MyModule
module MyNestedModule
def bar
p 'Bar'
end
end
end
module ToBePatched
module A
alias_method ld_some_method, :some_method
def some_method
p 'override some'
old_some_method
end
alias_method ld_another_method, :another_method
def another_method
p 'override another'
old_another_method
end
#override other methods
end
end
#my_main.rb
module MyModule
module Main
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def i_want_that_great_feature
class_eval do
include MyModule::MyNestedModule
end
end
end
end
end
#test program
ToBePatched::Foo.class_eval do
include MyModule::Main
end
class Bs < ToBePatched::Foo
i_want_that_great_feature
end
bs = Bs.new
bs.bar
bs.some_method
bs.another_method
another_method()
Calling some_method() on an instance of class that includes the
original module works fine (calls the monkey patched version), but
when I call another_method() the original (not the monkey patched)
method is called.
There is a lot of code, and I've tried to recreate the series of
includes with the below example, yet the example works!
Given this, I hope that someone could still provide me with some
insight as to why this would happen
#original_module.rb
module ToBePatched
module A
def some_method
p 'original some'
end
def another_method
p 'original anotehr'
end
end
end
#original_class.rb
module ToBePatched
class Foo
include ToBePatched::A
end
end
#my_override.rb
module MyModule
module MyNestedModule
def bar
p 'Bar'
end
end
end
module ToBePatched
module A
alias_method ld_some_method, :some_method
def some_method
p 'override some'
old_some_method
end
alias_method ld_another_method, :another_method
def another_method
p 'override another'
old_another_method
end
#override other methods
end
end
#my_main.rb
module MyModule
module Main
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def i_want_that_great_feature
class_eval do
include MyModule::MyNestedModule
end
end
end
end
end
#test program
ToBePatched::Foo.class_eval do
include MyModule::Main
end
class Bs < ToBePatched::Foo
i_want_that_great_feature
end
bs = Bs.new
bs.bar
bs.some_method
bs.another_method