Private Doesn't Apply to Class Method and Define Method

Ruby - Check for nil Without Using an Explicit if

Ruby’s private keyword might do a lot less than you think.

The “private” does not apply to class methods defined on self

This does not make anything private:

1
2
3
4
5
6
class Klass
  private
  def self.print
    'Hello'
  end
end

You need to use private_class_method instead:

1
2
3
4
5
6
7
class Klass
  def self.print
    'Hello'
  end

  private_class_method :print
end

The “private” does not apply to define_method

This does not make anything private:

1
2
3
4
5
6
class Klass
  private
  define_method :print do
    'Hello'
  end
end

You need to use private with an argument instead:

1
2
3
4
5
6
7
class Klass
  define_method :print do
    'Hello'
  end

  private :print
end