-
-
Save danielsdeleo/c350a1d1b68a96af0f17 to your computer and use it in GitHub Desktop.
fdiv for ruby 1.8.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless 0.respond_to?(:fdiv) | |
class Numeric | |
def fdiv(numeric) | |
raise TypeError, "#{numeric.inspect} can't be coerced into a Numeric type" unless numeric.respond_to?(:to_f) | |
self.to_f / numeric | |
end | |
end | |
end | |
describe Numeric do | |
it "divides floats with fdiv" do | |
3.0.fdiv(3).should be_close(1.0, 0.001) | |
end | |
it "divides ints with fdiv" do | |
3.fdiv(3).should be_close(1.0, 0.001) | |
end | |
it "raises a TypeError if you try to fdiv a non-numeric object" do | |
lambda {3.fdiv(:nan)}.should raise_error(TypeError, /:nan can't be coerced into (Float|Fixnum|a Numeric type)/) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment