Read more

The "private" modifier does not apply to class methods or define_method

Henning Koch
August 20, 2013Software engineer at makandra GmbH

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

"private" does not apply to class methods defined on self

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This does not make anything private:

class Foo

  private

  def self.foo
    'foo'
  end
  
end

You need to use private_class_method instead:

class Foo

  def self.foo
    'foo'
  end
  
  private_class_method :foo
  
end

"private" does not apply to define_method

This does not make anything private:

class Foo

  private

  define_method :foo do
    'foo'
  end
  
end

You need to use private with an argument instead:

class Foo

  define_method :foo do
    'foo'
  end
  
  private :foo
  
end
Posted by Henning Koch to makandra dev (2013-08-20 17:43)