mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
moving to a new repo
This commit is contained in:
45
about_open_classes.rb
Normal file
45
about_open_classes.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
require 'edgecase'
|
||||
|
||||
class AboutOpenClasses < EdgeCase::Koan
|
||||
class Dog
|
||||
def bark
|
||||
"WOOF"
|
||||
end
|
||||
end
|
||||
|
||||
def test_as_defined_dogs_do_bark
|
||||
fido = Dog.new
|
||||
assert_equal __, fido.bark
|
||||
end
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
# Open the existing Dog class and add a new method.
|
||||
class Dog
|
||||
def wag
|
||||
"HAPPY"
|
||||
end
|
||||
end
|
||||
|
||||
def test_after_reopening_dogs_can_both_wag_and_bark
|
||||
fido = Dog.new
|
||||
assert_equal __, fido.wag
|
||||
assert_equal __, fido.bark
|
||||
end
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
class ::Integer
|
||||
def even?
|
||||
(self % 2) == 0
|
||||
end
|
||||
end
|
||||
|
||||
def test_even_existing_built_in_classes_can_be_reopened
|
||||
assert_equal __, 1.even?
|
||||
assert_equal __, 2.even?
|
||||
end
|
||||
|
||||
# NOTE: To understand why we need the :: before Integer, you need to
|
||||
# become enlightened about scope.
|
||||
end
|
||||
Reference in New Issue
Block a user