Basics

In Object oriented programming whole program is created with objects and the messages that pass between them. Now objects are entities they can communicate with each other similar to real world objects. Objects are created using class. So, class is a definition of the object which determines the behavior of the objects and the data that it will contain. In other words class is the blueprint of the object.

Definition

In ruby we use class keyword to define a class such as

class Person
    
    def initialize(name)
      @name = name
    end
    
    def greet
        puts "Hello #{@name}!"
    end
end

The above example we are creating a class Person and also defining a method called greet. Another name for functions in Object oriented programming is ‘method’. Objects are created using the matz = Person.new('matz') syntax and then we can call greet method on the sourav object using matz.greet(). The output will be Hello matz!. The matz is the object of the Person class it has attribute name which for matz instance is ‘matz’.

The initialize method is a special method of the class also know as constructor method. The purpose of this method is to give initial values in the class. This method is called under the hood when we call the new method on the class. There can be cases where we want to skip the constructor we can do so that by using allocate method.

Variables

To hold data we use the variables in classes. There are four type of variable in Ruby.

  • Instance variable: Instance variable hold the attributes of the objects. For example in the above example we can already see on example of the variable. @name This is an example of the instance variable.
  • Class varibable: Class variable starts with @@ symbol such as @@varibale and must be initialized before the can be put to use. The value of class variable is same for all of the objects
  • Global variable: Global variable starts with $ symbol such as $ENVIRONMENT this remains constant across all classes and objects.
  • Local variable: Local varible does not require any special symbol but sometimes denoted with a leading underscore. The scope of the variable is onlly within method of the class.

Getter and Setter

Suppose in the above definition of the Person class we want to get the name of the person only. In the current implementation we will not be able to do that. Let us define a special getter method through which we can get value of the name.

class Person
    
    def initialize(name)
      @name = name
    end
    
    def name
        @name
    end
end

Now, if we want to get the value of name we can do so by:

matz = Person.new('Yukihiro Matsumoto')
matz.name
=> "Yukihiro Matsumoto" 

Similary we can define setter to set the value of the instance variable.

class Person
    
    def initialize(name)
      @name = name
    end
    
    def name
        @name
    end

    def name=(name)
        @name = name
    end
end

matz = Person.new('Matz')
matz.name=('Yukihiro Matsumoto')
matz.name

In this example we are initially creating a Person instance called matz with name as ‘Matz’ then we are setting a new value of the name using the setter method.

There is another concise way of defing getter and setters in Ruby using attr_reader, attr_writer and attr_accessor. Let us create the above class using attr

class Person
    
    attr_reader :name
    attr_writer :name
    
end

matz = Person.new
matz.name=('Yukihiro Matsumoto')
matz.name

The output is same as the above example. attr_reader and attr_writer. In this example we have not defined the constructor and we have not set name value initially. The attr_reader and attr_writer defines the @name variable or any parameters which is passed as a symbol. Ruby also has shortcut for defining reader and writer using attr_accessor

class Person
    attr_accessor :name
end

matz = Person.new
matz.name=('Yukihiro Matsumoto')
matz.name

In the next article we will check access control and inheritance.

Leave a Reply

Your email address will not be published. Required fields are marked *