Ruby Classes and Objects

Ruby is a perfect object-oriented programming language. Features of object-oriented programming languages include:

  • Data Encapsulation
  • Data abstraction
  • polymorphism
  • inherit
An item arranged program that includes classes and articles. A class is a plan made by an individual article. In article situated wording, your bicycle is a case of a bike class.

In the instance of a vehicle, it incorporates wheels, torque, fuel or gas tank limit. These characteristics structure the information individuals from the Vehicle class. With these characteristics you can recognize a vehicle from different vehicles.

Vehicles can likewise contain explicit capacities, for example, ending, driving, and speeding. These capacities structure the information individuals from the Vehicle class. Consequently, you can characterize a class as a blend of properties and capacities.

Class Vehicle is characterized as follows:


Class Vehicle { Number no_of_wheels Number horsepower Characters type_of_tank Number Capacity Function speeding { } Function driving { } Function halting { } }

By doling out various qualities to these information individuals, you can make distinctive occasions of the class Vehicle. For instance, a plane has three wheels, 1,000 pull, and a fuel tank limit of 100 liters. Similarly, a vehicle has four wheels, a torque of 200, and a gas tank limit of 25 liters.

Defining classes in Ruby

To execute object-situated programming with Ruby, you have to initially figure out how to make items and classes in Ruby.

In Ruby, classes dependably begin with the watchword class pursued by the name of the class. The principal letter of the class name ought to be promoted. The class Customer looks like this:

class Customer end

You can terminate a class with the keyword end. All data members in the class are between the class definition and the end keyword.

Variables in the Ruby class

Ruby provides four types of variables:

  • Local variables: Local factors are factors characterized in a technique. Nearby factors are not accessible outside of the strategy. In the accompanying sections, you will see more insights regarding the strategy. Nearby factors begin with a lowercase letter or _.
  • Instance Variables: Instance factors can be utilized crosswise over techniques in a specific case or article. This implies example factors can change from article to question. The occasion variable places an image (@) before the variable name.
  • Class variables: Class factors can be utilized crosswise over various items. A class variable has a place with a class and is a property of the class. Class factors place images (@@) before the variable name.
  • Global Variables: Class factors can't be utilized crosswise over classes. On the off chance that you need to have a variable that can be utilized crosswise over classes, you have to characterize a worldwide variable. Worldwide factors dependably begin with a dollar sign ($).


Using the class variable @@no_of_customers, you can decide the quantity of articles being made so you can decide the quantity of clients.


class Customer @@no_of_customers=0 end

Create questions in Ruby utilizing the new method

An object is an occasion of a class. Presently you will figure out how to make objects of a class in Ruby. In Ruby, you can make objects utilizing the class' strategy new .

Methods new is a one of a kind strategy that is predefined in the Ruby library. The new strategy has a place with the class technique.

The following precedent makes two objects of class Customer cust1 and cust2:

cust1 = Customer. new cust2= Customer. new

Here, cust1 and cust2 are the names of the two items. The article name is trailed by an equivalent sign (=) trailed by the class name, trailed by the dab administrator and the watchword new.

Custom strategies to make Ruby objects

You can pass contentions to strategy new , which can be utilized to instate class factors.

When you need to pronounce a new technique with parameters, you have to proclaim the strategy initialize while making the class.


initialize strategy is an uncommon kind of technique that will be executed when the new technique for a class with parameters is called.

The following precedent makes the instate method:


class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end end

In this model, you can proclaim a initialize technique with id, name, addr as a neighborhood variable. Here, def and end are utilized to characterize the Ruby strategy initialize. In the accompanying sections, you will study the strategies.

In the initialize technique, pass the estimations of these nearby factors to the occurrence factors @cust_id, @cust_name, and @cust_addr. Here, the estimation of the neighborhood variable is passed alongside the new strategy.

Now, you can make an item like this:

cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire street, Khandala")

Member works in the Ruby class

In Ruby, capacities are called strategies. Every technique in the class begins with the watchword def pursued by the strategy name.

Method names dependably start with lowercase letters. In Ruby, you can end a technique with the watchword end.

The following model characterizes a Ruby method:

class Sample def function statement 1 statement 2 end end
Here, statement 1 and statement 2 are a piece of the body of the technique function in the class Sample . These announcements can be any substantial Ruby articulation. For instance, we can utilize the strategy puts to yield Hello Ruby as follows:

class Sample def hello puts "Hello Ruby!" end end

The following precedent makes an object of class Sample and calls the hello method:

#!/usr/container/ruby class Sample def hello puts "Hello Ruby!" end end # Use the above class to make an object object = Sample. new object.hello

This will create the accompanying results:

Hello Ruby!

Simple case study

If you need to accomplish more activities on classes and items, here is a case study:

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.