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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)