3

I am reading this explanation of module methods for Ruby and how they are different from instance methods for classes. Here is the explanation I am reading:

Remember that unlike an instance method, a module method needs to be defined on the module itself. How do you access the module? Recall that inside a module definition, self refers to the module being defined. So you'll need to define the method using the form self.xxx.

I don't totally get it. When we defined methods inside Classes, we didn't have to define it on the class itself. We merely called it on the instantiated objects of the classes.

Why do we need to define module methods on the module itself using the term "self"? What's the purpose of this? Why can't we just define module methods without using the term self? Here is how my module skeleton looks:

module GameTurn

    def self.take_turn(player)

    end
Jwan622
  • 11,015
  • 21
  • 88
  • 181

1 Answers1

7

There's two kinds of module methods:

  • Those that are intended to be mixed in to other modules or classes: "Mixins"
  • Those that are intended to be used directly: "Exposed Methods"

For example:

module Example
  def self.exposed_method
    # This method is called as Example.exposed_Method
  end

  def mixin_method
    # This method must be imported somewhere else with include or extend
    # or it cannot be used.
  end
end

You have two on a class as well:

  • Those that are called only on instances of the class: "Instance methods"
  • Those that are called directly on the class: "Class methods"
    • These are also called "static methods" in other languages.

Example:

class ExampleClass
  def self.class_method
    # This can be called as ExampleClass.class_method
  end

   def instance_method
     # This can only be called on an instance: ExampleClass.new.instance_method
   end
end
tadman
  • 208,517
  • 23
  • 234
  • 262
  • Okay so what's confusing is that my take_turn method is going to be used on an object and so it doesn't seem to be used directly? It seems to be mixed in with other classes/objects. So why do I need to define it using self? – Jwan622 Oct 22 '14 at 18:50
  • It's a matter of how you're using it. If you're using proper [object-oriented design patterns](http://en.wikipedia.org/wiki/Design_Patterns) then you might organize your code differently. You'd define it with `self` if you want that method to be used stand-alone, not after being imported into another context. – tadman Oct 22 '14 at 19:01
  • 2
    At the risk of muddling tadman's nice, clean answer, if you add `include Example`, in `ExampleClass` (or invoke `ExampleClass.include Example` after defining the class) you have use of the instance method `Example#mixin_method`, just as though you had defined it in the class, but you don't "get" the module method `Example.exposed_method` i.e., you cannot invoke `ExampleClass#.exposed_method`. You must use `Example.exposed_method`. There is a way to bring module mixin methods into the class as class methods, but you needn't be concerned with that at this stage of your Ruby education. – Cary Swoveland Oct 22 '14 at 20:08