Class Methods And Variables In Ruby On Rails


Today, we are going to see the class methods, variables, and the to_s method in Ruby programming language. The brief description of this concept is given below.


  • Class Variables.
  • Class Constants.
  • The to_s method.


  • Class variables are accessible to every object of a class.
  • A class variable belongs to the class, not the object.
  • You declare a class variable using two @signs for example, @@name.
  • We can, for example, keep count of all person objects created using a class variable.


  1. Class person  
  2.      @@count = 0  
  3.      def initialize  
  4.     @@count + =1   
  5.      End  
  6.      def self.get_count  
  7.     @@count  
  8.      End  
  9. End  
  10. P1                                    If we use only p1  
  11. P2                                     means the output is 1.     
  12. Puts person.get_count  


  • In the code above, @@count is a class variable.
  • Since the initialize method is called for every object that is created, incrementing the @@count variable will keep track of the number of objects created.
  • We also defined a class method called get_count to return the value of the class variable.
  • In the code above, we have created two objects of the person so the value of the @@count variable is 2.
  • A class variable is usually used when you need information about the class, not the individual objects.


  • A class can also contain constants.
  • Remember, constant variables do not change their value and start with a capital letter.
  • It is common to have uppercase names for constants.


  1. Class calc  
  2.       PI = 3.14  
  3. End  
You can access constants using the class name, followed by two colon symbols (::) and the constant name. For example -


  1. Puts calc :: PI  




  • The to_s method comes built-in with all classes.
  • It gets called when you output the object.


  1. Class person  
  2.       # some code  
  3. End  
  4. P =  
  5. Puts P  



  • When we call puts p, Ruby automatically calls the to_s method for the object p, so puts p is the same as puts p.to_s.
  • By default, the to_s method prints the object’s class and an encoding of the object id.
  • We can define our own to_s method for a class and add custom implementation to it.
  • For example, we can generate an informative, formatted output for our person class.


  1. Class person  
  2.     def initialize(n,a)  
  3.     @name = n  
  4.     @age = a  
  5.     End  
  6.     def to_s  
  7.     #{@name} is # {@age} years old.  
  8.         End  
  9. End  
  10. P =“David”, 28)  
  11. Puts P  


David is 28 years old.

  • The to_s method also gets called when the object is used as a value in a string, like”#{P}”.
  • Defining the to_s method makes it easier and shorter to output the information of an object in the format needed, as opposed to defining a custom method and calling it from an object.
  • When you define the to_s method, you call puts on your object (puts obj) where with a custom method, you have to explicitly call it from the object (puts


In this write-up, we learned some new concepts in Ruby. I hope it will help you gain more knowledge. In the future, we will go deeper into the concepts of Ruby programming.