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

Posted Over 10 years ago. Visible to the public.

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

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

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
Henning Koch
Last edit
7 months ago
Felix Eschey
Keywords
visibility
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-08-20 15:43)