Classes in Ruby

In programming, security and reusability are the big challenges in programming. The classes help to resolve these challenges. The classes are the collection of the statements (code). Classes can be called from outside, so we can use code inside the class again and again. The classes can define who can access its code. It helps in restricting unwanted people to access important code.

The simple syntax of the class is:

  1. #define class with keyword 'class'
  2. class ClassName
  3. #code of the class
  4. end
  5. #close class with keyword 'end'

Class name can be anything including only alphabets, numbers and underscore. For good coding practice start class name with capital letter.


The code of the class can not be executed directly. We need to make some reference to that class. This reference is called object. We call (execute) a class by using this object.

  1. #define class with keyword 'class'
  2. class ClassName2
  3. #code of the class
  4. end
  5. #close class with keyword 'end'
  6. # making the object of above class
  7. # the rules for the name of the object
  8. # are same as that for variable name
  9. object =

Methods inside class:

Methods can be inside the class.

  1. # methods with classes
  2. class MyClass
  3. # methods inside the class
  4. def print_name(name, age)
  5. puts "your name is #{name} and age is #{age}"
  6. end
  7. end
  8. o =
  9. # calling the method
  10. o.print_name 'ha', 2

Class methods:

The class methods are special methods which are independent of objects and only depend on the class. These are equivalent to static methods/ functions in other programming languages.

  1. # methods with classes
  2. class MyClass
  3. # methods inside the class
  4. def print_name(name, age)
  5. puts "your name is #{name} and age is #{age}"
  6. end
  7. # defining a class method
  8. def MyClass.static
  9. puts 'this is static method'
  10. end
  11. end
  12. o =
  13. # calling the method
  14. o.print_name 'ha', 2
  15. # calling static method
  16. MyClass.static


  1. your name is ha and age is 2
  2. this is static method


  • The variables which are defined in general methods are not available inside the class methods.
  • The class methods can not be called directly even inside the same class. These methods can only be called using class name. Class method can be called directly in other class methods.
  • General methods also can not be called directly in the class methods. For calling general methods, class object must be created in class methods.
  • We can also use keyword self instead of class name to define a class method.


It is special type of the method which runs automatically when we make the object of the class. This method is used to initialize variables within a class.

  1. class MyClass
  2. def initialize
  3. puts 'this is a constructor and is used to initialize the variables'
  4. end
  5. end
  6. o =


  1. this is a constructor and is used to initialize the variables

Class variables:

Class variables are accessible only in the class. All objects of the same class share class variables.

  1. class ClassName
  2. def initialize
  3. @@class_variable = 12
  4. @instance_variable = 12
  5. end
  6. def get_cls_var
  7. @@class_variable+=1
  8. return @@class_variable
  9. end
  10. def get_ins_var
  11. @instance_variable+=1
  12. return @instance_variable
  13. end
  14. end
  15. obj1 =
  16. obj2 =
  17. puts obj1.get_ins_var
  18. puts obj2.get_ins_var
  19. puts obj1.get_ins_var
  20. puts obj2.get_ins_var
  21. # operating class varaible
  22. puts obj1.get_cls_var
  23. puts obj2.get_cls_var
  24. puts obj1.get_cls_var
  25. puts obj2.get_cls_var


  1. 13
  2. 13
  3. 14
  4. 14
  5. 13
  6. 14
  7. 15
  8. 16

Note that both object have different instance variables but both objects are operating on same class variable.

Object properties:

Methods can be defined from outside the class.

  1. class MyClass
  2. end
  3. o =
  4. # defining a object property
  5. def o.method
  6. puts 'this is general method from outside'
  7. end
  8. #defining class method
  9. def MyClass.cls_method
  10. puts 'this is class method'
  11. end
  12. # calling methods
  13. o.method
  14. MyClass.cls_method


  1. this is general method from outside
  2. this is class method


In the staring of this article we had discussed that classes help in security. We can restrict the accessibility of methods and objects. Ruby defines three access controls:

  1. public: Accessible to anybody
  2. private: Only accessible within the class
  3. protected: Can be invoked only by objects of the defining class and its subclasses. We will read about it in inheritance.
  1. class MyClass
  2. # public methods
  3. public
  4. def for_all
  5. puts 'this is for all'
  6. end
  7. # plae more public methods here
  8. # private methods
  9. private
  10. def secret
  11. puts 'this is secret method'
  12. end
  13. # write more private methods here
  14. #protected
  15. protected
  16. def family
  17. puts 'this is inheritable method, not for all'
  18. end
  19. # place more protected methods here
  20. end
  21. o =
  22. o.for_all
  23. # can not call other two
  24. # o.secret
  25. #


About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Ruby is a pure object oriented, general purpose,interpreted and server side scripting and programming language. Ruby ...
Introduction to Ruby
In this step by step free tutorials we will learn about printing, comments, puts, print, begin, end and EOF methods f...
Getting Started with Ruby
In this tutorial of free step by step free tutorials series we read about variables, constant, printing in staring an...
Variables in Ruby

Login or Sign up to leave comment.