Ruby Static Members Last Updated : 18 Jul, 2022 Comments Improve Suggest changes 3 Likes Like Report In Programming, static keywords are primarily used for memory management. The static keyword is used to share the same method or variable of a class across the objects of that class. There are various members of a class in Ruby. Once an object is created in Ruby, the methods and variables for that object are within the object of that class. Methods may be public, private, or protected, but there is no concept of a static method or variable in Ruby. Ruby doesn’t have a static keyword that denotes that a particular method belongs to the class level. However static variable can be implemented in ruby using class variable and a static method can be implemented in ruby using a class variable in one of the methods of that class. In Ruby, there are two implementations for the static keyword: Static Variable: A Class can have variables that are common to all instances of the class. Such variables are called static variables. A static variable is implemented in ruby using a class variable. When a variable is declared as static, space for it gets allocated for the lifetime of the program. The name of the class variable always begins with the @@ symbol. Example : Ruby # Ruby program to demonstrate Static Variable class Geeks # class variable @@geek_count = 0 def initialize @@geek_count += 1 puts "Number of Geeks = #{@@geek_count}" end end # creating objects of class Geeks g1 = Geeks.new g2 = Geeks.new g3 = Geeks.new g4 = Geeks.new Output: Number of Geeks = 1 Number of Geeks = 2 Number of Geeks = 3 Number of Geeks = 4 In the above program, the Geeks class has a class variable geek_count. This geek_count variable can be shared among all the objects of class Geeks. the static variables are shared by the objects Static Method: A Class can have a method that is common to all instances of the class. Such methods are called static methods.Static methods can be implemented in ruby using class variables in the methods of that class. Example : Ruby # Ruby program to demonstrate Static Method class Geeks #class method @@geek_count = 0 # defining instance method def incrementGeek @@geek_count += 1 end # defining class method def self.getCount return @@geek_count end end # creating objects of class Geeks g1 = Geeks.new # calling instance method g1.incrementGeek() g2 = Geeks.new # calling instance method g2.incrementGeek() g3 = Geeks.new # calling instance method g3.incrementGeek() g4 = Geeks.new # calling instance method g4.incrementGeek() # calling class method puts "Total Number of Geeks = #{Geeks.getCount()}" Output: Total Number of Geeks = 4 In the above program, incrementGeek() the getCount() method is the static (class) method of the class Geeks which can be shared among all the objects of class Geeks. Static member functions are allowed to access only the static data members or other static member functions, they can not access the non-static data members or member functions. Create Quiz Comment R riturajsaha Follow 3 Improve R riturajsaha Follow 3 Improve Article Tags : Ruby Ruby-Basics 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