Mo

Modules in Ruby

1 exercise

About Modules

Some times you don't need the overhead of creating an object with state. In these cases, a module can be used.

A module is very similar to a class (in fact, Module is Classes parent in the object hierarchy) - the main difference being that rather than using instance methods, we use class methods. Class methods start with self. and are directly called on a module. For example:

module Speaker
  def self.echo(something)
    "#{something} ... #{something}"
  end
end

Speaker.echo("Hello")   #=> "Hello ... Hello"
Edit via GitHub The link opens in a new window or tab

Learn Modules