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
- Static Method
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 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.
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.
Total Number of Geeks = 4
In the above program, incrementGeek() method is the static 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.
- Ruby | Struct members() function
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static variables in Java
- Difference between static and non-static method in Java
- Scala | Abstract Type members
- Different ways to Initialize all members of an array to the same value in C
- Static Function in PHP
- When to use static vs instantiated classes in PHP?
- Static vs Dynamic Website
- Difference between Static and Shared libraries
- “static const” vs “#define” vs “enum”
- Build a Simple static file web server in Node
- Ruby | BigDecimal * value
- Recursion in Ruby
- Ruby | Operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.