Class Methods And Variables In Ruby On Rails

INTRODUCTION

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.

CONTENTS

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

CLASS VARIABLES

  • 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.

EXAMPLE

  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 =person.new                                    If we use only p1  
  11. P2 =person.new                                     means the output is 1.     
  12. Puts person.get_count  

Output

  • 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.

CLASS CONSTANTS

  • 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.

Example

  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 -

Example

  1. Puts calc :: PI  

Output

3.14

THE TO_S METHOD

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

Example

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

Output

#<person:0x0000000271e128>

  • 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.

Example

  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 = person.new(“David”, 28)  
  11. Puts P  

Output

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 obj.info).

CONCLUSION

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.


Similar Articles