I need Math.radians() function and cannot find it.
radians=(angle/180)* Math::PI
You probably want to create a file called "mymath.rb" in the lib/ directory, and monkey patch Math, like this:
require 'mathn'
module Math
def self.to_rad angle
angle / 180.0 * Math::PI
end
end
or you can do what @MBO said in his comment. The link seems to be down, but the Google archives yield this informative little sentence, which indicates a solution that may be cleaner than mine (though I prefer keeping math stuff inside of math):
The simplest solution is to define a conversion method in Numeric that will convert a number of degrees into radians.
As a note, Ruby 2.0 has a feature called "Refinement" that basically lets you do local monkey-patching. It works this way (lifted from this blog post :
module RadiansConversion
refine Math do
def to_rad angle
angle / 180.0 * Math::PI
end
end
end
And then .... It's available inside another module or anything such like that.
module MyApp
using RadiansConversion
p Math.to_rad 180 #=> 3.14159265358979
p Math.to_rad 235 #=> 4.10152374218667
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