Private Classes in Ruby Last Updated : 17 Jan, 2023 Comments Improve Suggest changes 2 Likes Like Report The concept of private, protected and public methods in Ruby is a bit different than it other languages like Java. In Ruby, it is all about which class the person is calling, as classes are objects in ruby. Private Class When a constant is declared private in Ruby, it means this constant can never be called with an explicit receiver, a private constant will be called only with an implicit receiver. So, in ruby private classes can be defined inside a class as a sub-class and declaring them into private constants, here this private class can be only accessed through the outer-class. Syntax: private_constant: class Below is the example of Public class: Ruby # by default public class Marvel # by default public class Guardians def Quill puts "Legendary outlaw" end def Groot puts "I am Groot" end end # by default public class Avengers def Tony puts "I am Iron-man" end end end Marvel::Avengers.new.Tony Marvel::Guardians.new.Quill Output:I am Iron-man Legendary outlaw In the above example as the sub-classes Guardians and Avengers are public, both implicit and explicit users have access to it. Check out the private class Example : Ruby # Ruby program of accessing private class # public class Marvel # Private class Guardians def Quill puts "Legendary outlaw" end def Groot puts "I am Groot" end end # public class Avengers def Tony puts "I am Iron-man" end end # making Guardians class private private_constant :Guardians end Marvel::Avengers.new.Tony # throws an error(NameError) Marvel::Guardians.new.Quill Output:I am Iron-man main.rb:20:in `': private constant Marvel::Guardians referenced (NameError)So, here if we look at the code they aren't any access-specifier keywords to make a class as private or public, but they do exist in Ruby, but cannot deal with classes. To make a class as private we need to take the help of private_constant, here this makes any object, class or method as private which will be unavailable to access for explicit users.how can a private class can be accessed through the outer-class. Ruby # Ruby program a private class accessed through the outer-class. # public class Marvel # private class Guardians def Quill puts "Legendary outlaw" end def Groot puts "I am Groot" end end # private class Avengers def Tony puts "I am Iron-man" end end # outer-class method accessing private classes def fury Guardians.new.Groot Avengers.new.Tony end private_constant :Guardians private_constant :Avengers end # calls fury method in Marvel call. Marvel.new.fury # throws error as it is explicit accessing. Marvel::Avengers.new.Tony # throws error as it is explicit accessing. Marvel::Guardians.new.Quill Output:I am Groot I am Iron-man main.rb:26:in `': private constant Marvel::Avengers referenced (NameError)The only way to access the private class(inner-class) is by its outer-class. And private classes can only exist in the form of inner-class only. As we can see in the above code we accessed Groot and Tony (private class methods) with fury (outer class method), by creating Guardians.new and Avengers.new (private class objects) and calling the respective methods with the respective objects Guardians.new.Groot and Avengers.new.Tony (calling the methods) from fury (outer-class method). if the outer-class is private it will be inaccessible for both implicit and explicit users. Because creating an object for private class outside its class is not possible. Therefore no access. Note :Outer-class can never be private. Create Quiz Comment T Tejashwi5 Follow 2 Improve T Tejashwi5 Follow 2 Improve Article Tags : Ruby Ruby-OOP Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like