Self Improvement

One of the never-ending style battles in Ruby land is module_function vs extend self.

Both enable you to define module methods, which can be called not only from instance level, but also from class level. This enables you to make modules that can optionally be include'd into your current scope, which makes sense if the module contains non-state changing methods ("functions"). Not having to prepend the module name every time you use the functions saves time and looks good:

# class level
Mathematics.calc # => 42

# instance level
include Mathematics
calc # => 42

module_function

You can achieve this kind of functionality using:

module Mathematics
  module_function

  def calc
    42
  end
end

Which is very similar to writing this:

module Mathematics
  def self.calc
    42
  end

  private

  def calc
    42
  end
end

Reflection Observations

Mathematics.instance_method(:calc).owner #=> Mathematics
Mathematics.public_method_defined?(:calc) #=> false
Mathematics.private_method_defined?(:calc) #=> true

Mathematics.method(:calc).owner #=> #<Class:Mathematics>
Mathematics.method(:calc).owner.singleton_class? #=> true

Two things to take away from this:

extend self

There is another way to get something very similar:

module Mathematics
  extend self

  def calc
    42
  end
end

Using extend, the module will add its instance methods to the module's very own inheritance chain.

Reflection Observations

Mathematics.instance_method(:calc).owner #=> Mathematics
Mathematics.public_method_defined?(:calc) #=> true
Mathematics.private_method_defined?(:calc) #=> false

Mathematics.method(:calc).owner #=> Mathematics
Mathematics.method(:calc).owner.singleton_class? #=> false

The differences to module_function are:

Which One to Use?

Advantages of extend self

Adavantages of module_function

More Idiosyncratic Ruby