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.

Objects

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 = ClassName2.new

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 = MyClass.new
  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 = MyClass.new
  13. # calling the method
  14. o.print_name 'ha', 2
  15. # calling static method
  16. MyClass.static

Output:

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

NOTE:

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

Constructor:

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 = MyClass.new

Output:

  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 = ClassName.new
  16. obj2 = ClassName.new
  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

Output:

  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 = MyClass.new
  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

Output:

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

Modifiers:

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. # place 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 = MyClass.new
  22. o.for_all
  23. # can not call other two
  24. # o.secret
  25. # o.family



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel