Overloaded Args

The Other Way To Write Class Methods

There is another way to declare class methods, which isn’t as obvious if you come across it somewhere in the wild. Normally you declare a class method like:

class Rotor
  def self.signalify
    
  end
end

and obviously you use it like Rotor.signalify

However the other way is to open the class and declare your methods there e.g.

class Rotor
  class << self
    def signalify

    end
  end
end

Then you can use it in exactly the same way, it’s probably a preference thing but it can make it slightly easier to read if you have a lot of class methods.

It can also help you understand a bit more about how Ruby works, it turns out that each class has an Eigenclass, which stores instance level information. Ruby uses Eigenclasses to implement class methods as each of these class methods are defined on the Eigenclass.

This means that the method is directly invoked on the class itself instead of the instance of the class.

More information in this post:

Eigenclass Class Methods

This project is maintained by overloadedargs