-
Notifications
You must be signed in to change notification settings - Fork 4
Koans AboutModules
Patrick T. Nielsen edited this page Jun 6, 2015
·
1 revision
class AboutModules < Neo::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 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
Modules are used to group together methods, classes, and constants. These modules can be embedded in a class by using include ModuleName
which allows that class to reference methods expressed in the module. As with class methods, you can call a module method by preceding its name with the module's name and a .
ex. ModuleName.methodname
.