The concept of class objects in Ruby and the concept of ruby objects

Source: Internet
Author: User
Tags at sign
Analysis of the concept of class objects in Ruby, analysis of the concept of ruby objects

 Object-oriented programs involve classes and objects. A class is a blueprint that is created from individual objects. In object-oriented terminology, we say that Xiaoming's bicycle is an instance of an object called a bicycle class.

Any vehicle example. It includes wheels, horsepower, fuel or gas tank capacity. These characteristics form the data members of the class of vehicles. These characteristics can be distinguished from other vehicles.

The vehicle also has certain functions such as stopping, driving, and speeding. Even these functions form data members of the class vehicle. Therefore, you can define a class as a combination of features and functions.

The vehicle class can be defined as:

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

By assigning different values to these data members, several instances of a vehicle-like class can be formed. For example, the aircraft has three wheels, 1,000 horsepower, different fuel tanks and a capacity of 100 liters. In the same way, a car has four wheels, 200 horsepower, gas as different tanks and a capacity of 25 liters.
Define a class in Ruby:

To implement object-oriented programming by using Ruby, you first need to learn how to create objects and classes in Ruby.

A class in Ruby always starts with the name of the keyword class. The name should always be capitalized. For example, the Customer class can be displayed as:

class Customer
end

The class definition ends by using the end keyword. All data members in a class are defined between classes, with the end keyword as the terminator.
Variables in Ruby classes:

Ruby provides four types of variables:

    Local variables: Local variables are variables defined in a method. Local variables are not available outside the method. More details are covered in the methods in subsequent chapters. Local variables usually start with a lowercase letter or _.

    Instance variables: Instance variables are methods that can span any particular instance or object. This means that instance variables change from object to object. Instance variables are preceded by the at sign (@), followed by the variable name.

    Class variables: Class variables are available in a variety of different objects. A class variable belongs to a class and is a characteristic of the class. The variable name followed by the symbol @@.

    Global variables: Class variables are not allowed across classes. If you want a single variable to span classes, you need to define a global variable. Global variables are always preceded by a dollar sign ($).

example:

Use the @@ no_of_customers class variable to determine the number of objects created. This makes the number of exported customers.

class Customer
  @@ no_of_customers = 0
end

Ruby uses the new method to create objects:
An object is an instance of a class. Now, you will learn how to create a class object in Ruby. Objects are created in Ruby by using the new method.
The new method is a unique method, which is predefined in the Ruby library. The new method belongs to a class method.
The following example creates two object class clients cust1 and cust2:

cust1 = Customer. new
cust2 = Customer. new

Here, cust1 and cust2 are the names of the two objects. After the equal sign (=), the class name will follow the object name. Then, the dot operator and the keyword new follow.
Custom method to create Ruby objects:

You can pass parameters to the new method, which can be used to initialize class variables.

When the new method to be declared has parameters, the method to be declared is initialized when the class is created.

The initialize method is a special type of method. The class that will execute the new method is called a parameter.

The following example creates the 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 the initialization methods id, name, and addr for local variables. Here the def end is used to define a Ruby method initialization. These will be learned more in the relevant subsequent chapters.

In the initialize method, the values of these local variables are passed to the instance variables @cust_id, @cust_name and @cust_addr. The value held by the local variable is passed along with the new method.

You can now create objects as follows:

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

Member functions of classes in Ruby:

In Ruby, the method by which a function is called. The method name of each method in a class starts with the keyword def.

Method names are always best in lowercase letters. Your final method in Ruby ends by using the keyword end.

The following example is a way to define a Ruby:

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

Here statement1 and statement2 are part of the function body. These statistics can be any valid Ruby statement. For example, we can print Hello Ruby in the method as follows:

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

Now, in the following example, the Sample class creates an object and calls the hello method to see the result:

#! / usr / bin / ruby

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

# Now using above class to create objects
object = Sample. new
object.hello

This will produce the following results:

Hello Ruby!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.