That's an error in the book. The methods in Ex25
are class methods. include
adds instance methods to the "current space." Remove self
from the method definitions and it'll work:
module Ex25
def break_words(stuff)
stuff.split(' ')
end
end
include Ex25
break_words 'hi there' # => ["hi", "there"]
If you're curious, here's some more detail about what's going on: The place where the methods are included—the "current space"—is the Object class:
Object.included_modules # => [Ex25, Kernel]
All Object instances gain the included methods ...
Object.new.break_words 'how are you?' # => ["how", "are", "you?"]
... and the top-level is just an Object instance:
self.class # => Object
But wait. If the top-level is an Object instance, why does it respond to include
? Isn't include
an instance method of Module
(and its subclass, Class
)? The answer is that the top-level has a singleton method ...
singleton_methods.include? "include" # => true
... which we can assume forwards to the Object class.