Ruby Programming – An Overview on Classes

As we discussed in our previous Articles, Ruby is a pure object-oriented language. That means, everything is an object of a class in Ruby. Non-object things are NOT exists in Ruby. Unlike C++ where you can write the program without classes or objects; in Ruby we CAN NOT write programs without classes or objects. Hence it is a pure object-oriented programming language.

Defining a Ruby class

In Ruby, the classes are defined using the keyword class. Classes can contain attributes or properties and the methods. Once the class definition is over this should be ended with the end keyword.

Below is an example of a simple class without any attributes and methods defined.

class Sample
end

Attributes of a class

There are two types of attributes or variables or properties Ruby supports. An instance attributes and the class attributes.

Instance variables or attributes always starts with at sign (“@”). These are the attributes can be accessible ONLY through instances or objects of the class.

Class attributes are global to the class and these can be accessible through the class itself; NOT through instances or objects of the class. Class attributes can be defined using two at symbols (“@@”).

For example, below are the types of attributes defined for the class Sample.

class Sample
   @instance_variable = 10
   @@class_variable = 'Hello!'
end

By default, Ruby doesn’t allows to access the variables directly either by using the class name or through it’s instances. Attributes of the classes can be accessible through the methods of the class only. If you attempt to access above attributes of the class, directly; you will see below Error messages.

instance_variable: undefined method `instance_variable' for Sample
class_variable: undefined method `class_variable' for Sample

Methods of a class

Like attributes of the class; methods of the class also has two types. Instance methods and class methods. Instance methods can be accessible through class instances or objects; whereas class methods are accessible ONLY through the classes itself.

Both these types of methods can be created using the def keyword and the method definition is ended with end keyword. A method name must be required to define a class method.

By default, the methods are instance methods. Here is the simple example of an instance method; this can be accessible through the instance of the class.

def SayHello
   puts "Hello!"
end

Different ways we can create the class methods. Usually we create them as; prefixing the name of the class with a dot operator. Here is an example of a class method; this can be accessible through the class name ONLY.

def Sample.SayHi
   puts "Hi!"
end

Create class object or an instance of a class

Class objects can be created using the new method. Does it new method? Yes. Every class has new method defined and this class method is used to create an instance of that class. Here is an example of an instance of our Sample class.

obj = Sample.new

Another important method you need to know is initialize method. This is like a constructor and is useful to initialize the variables of the class during the creation of class instances. Ruby will call this method when an instance is created using the new method.

def initialize
   #member declaration will go here
end

Here is the complete working example of the code; putting altogether what we discussed so far through this Article;

class Sample
  @instance_variable
  @@class_variable = 'Hello!'
  
  def initialize
    @instance_variable = 10
  end
  
  def GetInstanceVariable
    return @instance_variable
  end
  
  def Sample.GetClassVariable
    return @@class_variable
  end
end

obj = Sample.new

puts obj.GetInstanceVariable
puts Sample.GetClassVariable

| Nick |

2 Comments

Add a Comment

Your email address will not be published.