I'm using ruby's refinements to monkey-patch a method into the Math class like so:
module Radians
refine Math do
def radians(degrees)
degrees * Math::PI / 180
end
end
end
I then try to call my monkey-patched method in a class like so:
class Foo
using Radians
def bar
Math.radians(180)
end
end
Foo.new.bar
This throws an undefined method `radians' for Math:Module (NoMethodError)
What am I doing wrong? My ruby version is 2.5.1p57. I've consulted the documentation and I think what I've done is correct, but obviously not.
You're defining an instance method, but calling a singleton method.
You need to refine the Math module's singleton class instead:
module Radians
refine Math.singleton_class do
def radians(degrees)
degrees * Math::PI / 180
end
end
end
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