Ruby’s Static Method

To make a class method private, one would assume the folowing code:

class MyClass
  # omitted for brevity

  private

  def self.my_method
    # do something
  end
end

However this code does not work as expected. private method does not work on class methods, only instance methods.

To make a class methods private, one need to use the class << self idiom, which lets us operate on the class itself as an object.

class MyClass
  # omitted for brievety

  class << self
    private

    def my_method
      # do something
    end
  end
end

class << self needs to be closed with end. Also, methods inside it do not need to be prepended with self.

The method my_method in the code above is a class (or static) method.

Links to this page