Refinements
In Ruby 2.1 and later, you can require monkey patching to be explicitly active inside of a class/module by defining your method inside of a refine
block. You can activate the changes with the using
keyword.
​module Confusion
refine String do
def length
"Yes"
end
end
end
module BadString
using Confusion
def self.count(string)
string.length
end
end
module GoodString
def self.count(string)
string.length
end
end
BadString.count("four") # => "Yes"
GoodString.count("four") # => 4
Tweet