Defining a method inside a module in ruby (NoMethodError) Defining a method inside a module in ruby (NoMethodError) ruby-on-rails ruby-on-rails

Defining a method inside a module in ruby (NoMethodError)


The Ruby documentation on Module answers this in its introduction text.

This form:

module Familiar  def ask_age    return "How old are you?"  endend

defines #ask_age as an instance method on Familiar. However, you can't instantiate Modules, so you can't get to their instance methods directly; you mix them into other classes. Instance methods in modules are more or less unreachable directly.

This form, by comparison:

module Familiar  def self.ask_age    return "What's up?"  endend

defines ::ask_age as a module function. It is directly callable, and does not appear on included classes when the module is mixed into another class.