Module Methods
As we've learned so far, modules have primarily been used to add instance or class methods to other classes. You can add instance or class methods depending on whether you include
or extend
that module into your class. In both cases, though, the methods to be added are always defined as basic methods. This is in contrast to class methods in a class definition, which have the self.
prefix for their declaration.
However, we also saw that the module callbacks were declared differently using the self.
prefix. Let's see what happens if we define other methods using the self.
prefix on a module like so:
module BabelFish def self.the_answer return 42 end end
What we're doing here is defining static module methods. These are very similar to class methods but on a module. They don't contain any state. They are called straight onto the module constant itself:
irb(main):058:0> BabelFish.the_answer...