New Discussion

Notifications

You’re not receiving notifications from this thread.

Module extend self Discussion

3
General

This is such a handy feature of Ruby. I was amazed when I first learned about it. Really makes modules useful for any situation, not just being included in another object.

It’s one of those things that makes the language feel really flexible and elegant. Modules aren’t just for mixing in functionality—they can completely change how you structure your code, making it cleaner and more reusable.

Personally I find it confusing that by extend self module methods are duplicated in singleton class. It's cleaner to just write this way:

module CurrencyFormatter
  class << self

    def format(*)
      # ...
    end

  end
end

Here the intent is clear – you have to just use this module as CurrencyFormatter.format(...), you cannot include it.

The point of extend self is you can use it both ways.

Join the discussion
Create an account Log in