In Metaprogramming Ruby 2 in Chapter 2 at the "Refinements" section I found the following piece of Ruby code:
class MyClass
def my_method
"original my_method()"
end
def another_method
my_method
end
end
module MyClassRefinement
refine MyClass do
def my_method
"refined my_method()"
end
end
end
using MyClassRefinement
MyClass.new.my_method # => "refined my_method()"
MyClass.new.another_method # => "original my_method()" - How is this possible?
According to the author:
However, the call to
another_methodcould catch you off guard: even if you callanother_methodafterusing, the call tomy_methoditself happens beforeusing— so it calls the original, unrefined version of the method.
This totally trips me up.
Why MyClass.new.another_method prints "original my_method()" since its used after using MyClassRefinement and what is the author trying to say here?
Could anyone provide a more intuitive/better explanation?
Thanks.