Skip to content
Browse files

Add #coerce

  • Loading branch information...
1 parent b456ea3 commit 4f6d9ac06b5f0f02bce3e73603547cbf94d76bbf @drbrain committed Sep 3, 2009
Showing with 17 additions and 0 deletions.
  1. +10 −0 lib/ffmpeg/rational.rb
  2. +7 −0 test/test_ffmpeg_rational.rb
View
10 lib/ffmpeg/rational.rb
@@ -83,6 +83,16 @@ def <=>(other)
to_f <=> other.to_f
end
+ ##
+ # Allows Rational to be used as a Numeric
+
+ def coerce(other)
+ case other
+ when self.class then [other, self]
+ else [other.to_f, to_f]
+ end
+ end
+
def inspect
"#<%d/%d>" % [ num, den ]
end
View
7 test/test_ffmpeg_rational.rb
@@ -28,6 +28,13 @@ def test_class_rescale_rnd
assert_equal 8, rescaled
end
+ def test_coerce
+ assert_equal [5.0, 25.0], @rational.coerce(5)
+
+ other = @R.new 26, 1
+ assert_equal [other, @rational], @rational.coerce(other)
+ end
+
def test_inspect
assert_equal '#<25/1>', @rational.inspect
end

0 comments on commit 4f6d9ac

Please sign in to comment.
Something went wrong with that request. Please try again.