Skip to content

Instantly share code, notes, and snippets.

@danielsdeleo
Created November 29, 2010 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsdeleo/c350a1d1b68a96af0f17 to your computer and use it in GitHub Desktop.
Save danielsdeleo/c350a1d1b68a96af0f17 to your computer and use it in GitHub Desktop.
fdiv for ruby 1.8.6
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