# Ruby Private Class Methods

DevFeed: [Ruby Private Class Methods](<https://devfeed.tech/articles/ruby-private-class-methods-21060.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/01/24/ruby-private-class-methods/>)

Published: 2016-01-24T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Code](<https://devfeed.tech/topics/code.md>), [Programming language](<https://devfeed.tech/topics/programming-language.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-language](<https://devfeed.tech/tags/programming-language.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-programming-language](<https://devfeed.tech/tags/ruby-programming-language.md>), [syntax](<https://devfeed.tech/tags/syntax.md>)

## AI overview

This tutorial explains Ruby method visibility, focusing on how to define private class methods. It contrasts instance methods, class methods, public, private, and protected visibility, then shows why a direct private declaration can fail for a class method and introduces eigenclass syntax as an alternative.

## Source excerpt

In the Ruby programming language, defined methods come in two variants: instance methods and class methods. Instance methods are available after an object has been initialized, creating an instance. Class methods, on the other hand, are available without creating an instance of the class they are defined upon. Ruby methods can vary in visibility. Public methods are available in any context, while private methods' availability is restricted within the instance of a class and its descendants. A third visibility scope, protected, behaves similarly to private methods, but protected methods can be called by other instances of the same class. For a quick refresher, public and private instance methods look like this: class Dog def do_trick bark end private def bark puts 'woof woof' end end When the public method is called: > dog = Dog.new > dog.do_trick # => woof woof And the private method: > dog = Dog.new > dog.bark # => NoMethodError: private method `bark' called for <Dog> Private class methods might not be as common as private instance methods, but they still have their place. For instance, a class method may require internal helper methods to complete its function. Whatever the reason, defining private class methods has value but is not always intuitive. This example Dog class needs to maintain a list of tricks that will be used within the other public class methods. This list should not be accessible to any callers outside the Dog class. The wrong way A first pass at writing the private tricks method could look like: class Dog private def self.tricks [:bark, :roll_over, :fetch] end end However, when testing the visibility of the tricks method: > Dog.tricks # => [:bark, :roll_over, :fetch] Uh oh, no error was thrown indicating a that a private method was called, this method is completely public. Why? The reason that the above code did not produce a private method has to do with Ruby's object hierarchy, interactions amongst internal classes, instances of those classes,