Latest web development tutorials

Ruby classes and objects

Ruby is a perfect object-oriented programming language. Object-oriented programming language features include:

  • Data encapsulation
  • Data Abstraction
  • Polymorphism
  • inherit

These features will be object-oriented Ruby were under discussion.

An object-oriented programming, classes and objects involved. Class is a blueprint for individual objects created. In object-oriented terms, your bicycle is an instance of the class bike.

In the vehicle, for example, it includes a wheel (wheels), HP (horsepower), oil or gas tank capacity (fuel or gas tank capacity). These properties formed the data members of the vehicle (Vehicle) class. With these attributes that you can distinguish one vehicle from another vehicle.

Vehicles can also contain a specific function, such as pause (halting), driving (driving), speeding (speeding). These functions forming the data members of the vehicle (Vehicle) class. Therefore, you can define the class as a combination of properties and functions.

Vehicle class defined as follows:

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

By giving these data members assigned different values, you can create different instances of the class Vehicle. For example, an airplane with three wheels, 1,000 horsepower, fuel tank capacity of 100 liters. In the same way, a car with four wheels, 200 horsepower, a gas tank capacity of 25 liters.

Defining a class in Ruby

In order to use Ruby object-oriented programming, you'll need to learn how to create objects and classes in Ruby.

In Ruby, class always begins with the keywordclass,followed by the name of the class. The first letter of the class name should be capitalized.Customerclass as follows:

class Customer
end

You can use the keywordendto terminate a class. All the data members ofthe classare between the class definition and theendkeywords.

Ruby class variables

Ruby provides four types of variables:

  • Local variables: Local variables are variables defined in the method.Local variables outside the method is not available. In subsequent chapters, you will see more details about the method. Local variables start with a lowercase letter or _.
  • Examples of variables: instance variables across any particular instance or object methods.This means that instance variables can be changed from object to object. Place an instance variable symbol (@) before the variable name.
  • Class variables: class variables across different objects.Class variables belong to the class, and is a property of the class. Class variables to place the symbol (@@) before the variable name.
  • Global variables: class variables can not span class use.If you want to have an inter-class variables, you need to define global variables. Global variables always start with a dollar sign ($).

Examples

Use a class variable @@ no_of_customers, you can determine the number of objects to be created, so you can determine the number of customers.

class Customer
   @@ No_of_customers = 0
end

Creating objects usingnewmethods in Ruby

Objects are instances of classes. You will now learn how to create objects of the class in Ruby. In Ruby, you can use the class method to create anewobject.

Newmethod is a unique approach, the predefined library in Ruby. new methods belong to theclassmethods.

The following example creates two objects cust1 class Customer and cust2:

cust1 = Customer. new
cust2 = Customer. new

Here, cust1 and cust2 is the name of two objects. After the object name followed by an equal sign (=) after the equal sign followed by the class name, then the dot operator and the keywordnew.

Custom method to create Ruby objects

You can pass parameters to thenewmethod, these parameters can be used to initialize the class variables.

When you wantnewmethod declaration with parameters, you need to create a class at the same time declare a methodinitialize.

initializemethod is a special type of method will be executed when thenewmethod is called with a parameter class.

The following example creates initialize 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 example, you can declare withid, name, addr as theinitializemethod local variables.Here,def andendfor defining Ruby methodinitialize.In later chapters, you'll learn more details about the method.

In theinitializemethod, the values of these local variables to the instance variable @ cust_id, @ cust_name and @cust_addr. Here, the value of the local variable with the new method of delivery.

Now, you can create an object, as follows:

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

Ruby class member functions

In Ruby, functions are called methods. Eachclassmethod is based on the keyworddef,followed by a method name.

Method name is always lowercase letter.In Ruby, you can use the keywordendto end one way.

The following example defines a Ruby method:

class Sample
   def function
      statement 1
      statement 2
   end
end

Here,statement 1 andstatement 2is part of a class Samplefunctionmethod within the body. These statements can be any valid Ruby statement. For example, we can use the methodputsto outputHello Ruby,as follows:

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

The following example creates an object of class Sample and callhellomethod:

#! / Usr / bin / ruby

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

# Use the above class to create an object object = Sample. New
object.hello

This will produce the following results:

Hello Ruby!

Simple Case Study

If you want to do more exercises about classes and objects, there is a case study:

Ruby Class Case