-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_modules.rb
63 lines (50 loc) · 1.14 KB
/
about_modules.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
class AboutModules < EdgeCase::Koan
module Nameable
def set_name(new_name)
@name = new_name
end
def here
:in_module
end
end
def test_cant_instantiate_modules
assert_raise(NoMethodError) do
Nameable.new
end
end
# ------------------------------------------------------------------
class Dog
include Nameable
attr_reader :name
def initialize
@name = "Fido"
end
def bark
"WOOF"
end
def here
:in_object
end
end
def test_normal_methods_are_available_in_the_object
fido = Dog.new
assert_equal "WOOF", fido.bark
end
def test_module_methods_are_also_available_in_the_object
fido = Dog.new
assert_nothing_raised(Exception) do
fido.set_name("Rover")
end
end
def test_module_methods_can_affect_instance_variables_in_the_object
fido = Dog.new
assert_equal "Fido", fido.name
fido.set_name("Rover")
assert_equal "Rover", fido.name
end
def test_classes_can_override_module_methods
fido = Dog.new
assert_equal :in_object, fido.here
end
end