You can refine your class with
module RefinedString
refine String do
def to_boolean(text)
!!(text =~ /^(true|t|yes|y|1)$/i)
end
end
end
but how to refine module method? This:
module RefinedMath
refine Math do
def PI
22/7
end
end
end
raises: TypeError: wrong argument type Module (expected Class)
module Math
def self.pi
puts 'original method'
end
end
module RefinementsInside
refine Math.singleton_class do
def pi
puts 'refined method'
end
end
end
module Main
using RefinementsInside
Math.pi #=> refined method
end
Math.pi #=> original method
Defining a module #method is equivalent to defining an instance method on its #singleton_class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With