Skip to content

Instantly share code, notes, and snippets.

@simonbaird
Forked from anonymous/foo.rb
Last active December 16, 2015 00:59
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 simonbaird/5352062 to your computer and use it in GitHub Desktop.
Save simonbaird/5352062 to your computer and use it in GitHub Desktop.
# 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 !1.234.send(:whole_number?)
assert !1.001.send(:whole_number?)
assert 1.000.send(:whole_number?)
assert_raise(Float::NoYouCantBecauseMaths) { 6.50.even? }
assert_raise(Float::NoYouCantBecauseMaths) { 6.50.odd? }
end
end
Loaded suite foo
Started
.
Finished in 0.000342 seconds.
1 tests, 13 assertions, 0 failures, 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment