Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2013 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5352058 to your computer and use it in GitHub Desktop.
Save anonymous/5352058 to your computer and use it in GitHub Desktop.
Float#even? and Float#odd?
# For @echorand :)
require 'test/unit'
class Float
class NoYouCantBecauseMaths < RuntimeError; end
def even?
raise NoYouCantBecauseMaths unless whole_number?
to_i.even?
end
def odd?
raise NoYouCantBecauseMaths unless whole_number?
to_i.odd?
end
private
def whole_number?
self == to_i
end
end
class FloatEvenOddTest < Test::Unit::TestCase
def test_the_madness
assert !10.odd?
assert 10.even?
assert 11.odd?
assert !11.even?
assert !10.0.odd?
assert 10.0.even?
assert !11.0.even?
assert 11.0.odd?
assert_raise(Float::NoYouCantBecauseMaths) { 6.50.even? }
assert_raise(Float::NoYouCantBecauseMaths) { 6.50.odd? }
end
end
Loaded suite foo
Started
.
Finished in 0.000288 seconds.
1 tests, 10 assertions, 0 failures, 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment