Open In App

Ruby Static Members

Last Updated : 18 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
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.



Previous Article
Next Article

Similar Reads

Ruby | Struct members() function
The members() is an inbuilt method in Ruby that returns an array of symbols containing the struct members. Syntax: struct_name.members() Parameters: The function does not accepts any parameter. Return Value: It returns the array with struct members. Example 1: # Ruby program for members method in struct # Include struct Student = Struct.new(:name,
1 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Private Classes in Ruby
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 cons
3 min read
Ruby | Matrix cofactor() function
The cofactor() is an inbuilt method in Ruby returns the cofactor of the given index. Syntax: mat1.cofactor(i, j) Parameters: The function needs two index i and j, whose cofactor is to be returned. Return Value: It returns the cofactor of the given index. Example 1: # Ruby program for cofactor() method in Matrix # Include matrix require "matrix
1 min read
Ruby Integer odd? function with example
The odd? function in Ruby returns a boolean value. It returns true if the number is odd, else it returns false. Syntax: number.odd? Parameter: The function takes the integer which is to be checked for odd or not. Return Value: The function returns a boolean value which determines if the value is odd or not. Example #1: # Ruby program of integer odd
1 min read
Ruby Integer div() function with example
The div() function in Ruby returns the integer division of two numbers. Syntax: (number1).div(number2) Parameter: The function needs two numbers number1 and number2, where number1 is the dividend and number2 is the divisor. Return Value: The function returns the integer division of two numbers. Example #1: # Ruby program of Integer div() function #
1 min read
Ruby | Symbol to_s function
Symbol#to_s() : to_s() is a Symbol class method which returns string representation of the symbol object. Syntax: Symbol.to_s() Parameter: Symbol values Return: string representation of the symbol object. Example #1 : # Ruby code for Symbol.to_s() method # declaring Symbol a = :aBcDeF # declaring Symbol b = :"\u{e4 f6 fc}" # declaring Sym
1 min read
Ruby | Set replace() function
The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Syntax: s1.replace(enum) Parameters: The function accepts an enumerable object which to be replaced in the set. Return Value: It returns the self object which contains the contents of the set after rep
1 min read
Ruby | Set reset() function
The reset() is an inbuilt method in Ruby resets the internal state after modification to existing elements and returns self. The elements will be reindexed and deduplicated. Syntax: s1.reset() Parameters: The function does not accepts any parameter. Return Value: It returns self . Example 1: # Ruby program to illustrate # the reset() method # requi
1 min read
Ruby | Array Concatenation using (+) function
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array. Syntax: Array.+() Parameter: Arrays for performing the concatenation operation. Return: New arrays by combining two arrays. Example #1 : # Ruby code for +() method # showing concatenate operation # declaring array a = [18, 22,
2 min read
Article Tags :