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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)