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:

#define class with keyword 'class'

class ClassName

#code of the class

end

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

#define class with keyword 'class'

class ClassName2

#code of the class

end

#close class with keyword 'end'

# making the object of above class

# the rules for the name of the object

# are same as that for variable name

object = ClassName2.new

Methods inside class:

Methods can be inside the class.

# methods with classes

class MyClass

# methods inside the class

def print_name(name, age)

puts "your name is #{name} and age is #{age}"

end

end

o = MyClass.new

# calling the method

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.

# methods with classes

class MyClass

# methods inside the class

def print_name(name, age)

puts "your name is #{name} and age is #{age}"

end

# defining a class method

def MyClass.static

puts 'this is static method'

end

end

o = MyClass.new

# calling the method

o.print_name 'ha', 2

# calling static method

MyClass.static

Output:

your name is ha and age is 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.

class MyClass

def initialize

puts 'this is a constructor and is used to initialize the variables'

end

end

o = MyClass.new

Output:

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.

class ClassName

def initialize

@@class_variable = 12

@instance_variable = 12

end

def get_cls_var

@@class_variable+=1

return @@class_variable

end

def get_ins_var

@instance_variable+=1

return @instance_variable

end

end

obj1 = ClassName.new

obj2 = ClassName.new

puts obj1.get_ins_var

puts obj2.get_ins_var

puts obj1.get_ins_var

puts obj2.get_ins_var

# operating class varaible

puts obj1.get_cls_var

puts obj2.get_cls_var

puts obj1.get_cls_var

puts obj2.get_cls_var

Output:

13

13

14

14

13

14

15

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.

class MyClass

end

o = MyClass.new

# defining a object property

def o.method

puts 'this is general method from outside'

end

#defining class method

def MyClass.cls_method

puts 'this is class method'

end

# calling methods

o.method

MyClass.cls_method

Output:

this is general method from outside

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:

public: Accessible to anybody

private: Only accessible within the class

protected: Can be invoked only by objects of the defining class and its subclasses. We will read about it in inheritance.

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 harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.