This morning I've got a questions from a friend and really couldn't give him a good answer. It's about a rather simple Ruby structure.
module Test
def self.included(base)
class << base
def tester
puts 'some message' + "\n"
end
end
end
end
class Test2
include Test
tester
end
Speaking about the following block:
def self.included(base)
class << base
def tester
puts 'some message' + "\n"
end
end
end
So what does really happen here? We're overriding the self.included which is called whenever Test module is included by other class or module (Test2) and Test2 is passed as the single argument to it.
Moving forward, whenever we're getting a callback to self.included, we're extending the class (Test2) with tester method which does something.
What I was wondering about is why tester is called there. Because if we miss including Test there we'll get NameError: undefined local variable or method `tester' for Test2:Class exception.
So basically, my real question is what make sense of using such a structure of the code? I bet the example I was given is not complete enough to understand this.
What do you think?