On the concept _ruby topic of the class object in Ruby

Source: Internet
Author: User
Tags at sign class definition lowercase

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

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

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

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 examples of such vehicles can be formed. For example, the aircraft has three wheels, 1,000 horsepower, different fuel tanks and a capacity of 100 litres. In the same way, a car has four wheels, 200 horsepower, gas as a different tank and a capacity of 25 liters.
A class is defined in Ruby:

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

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

Class Customer End


The end of the class definition ends by using the keyword end. All data members in a class are defined between classes, and end keywords are used as terminators.
Variables in a Ruby class:

Ruby provides four kinds of variables:

Local variables: Local variables are variables defined in a method. Local variables are not available outside of the method. More details are presented in the methods in the following chapters. Local variables generally begin with lowercase letters or _.

Instance variables: Instance variables are methods that can span any particular instance or object. This means that the instance variable from object to object is changed. The instance variable is preceded by an 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 feature of the class. The symbol in front of them @@ 跟着 by the variable name.

Global variables: Class variables cannot span classes. If you want a single variable to cross classes, you need to define a global variable. The global variable is always preceded by a dollar sign ($).

Example:

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

Class Customer
  @ @no_of_customers =0
End

Ruby creates an object using the new method:
An object is an instance of a class. Now you'll learn how to create an object in Ruby as a class object. Ruby creates an object by using the new method.
The new method is a unique method, which is predefined in the Ruby library. The new method belongs to the method of the class.
The following example is the creation of two object class customers Cust1 and Cust2:

Cust1 = Customer. New
Cust2 = Customer. New

Here, Cust1 and Cust2 are the names of two objects. After the equals sign (=), the class name is followed by the object name. Then the dot operator and the keyword new are in the back.
Custom method to create a Ruby object:

The parameters of the new method can be used to initialize the class variable.

When the new method you intend to declare has arguments, the method that needs to be declared is initialized when the class is created.

The Initialize method is a special type of method in which the class that executes the new method is called a parameter.

The following example is the creation of the Initialize method:

Class Customer
  @ @no_of_customers =0
  def initialize (ID, name, addr)
   @cust_id =id
   @cust_name =name
   @cust_addr =addr
  End


In this example, you can declare the initialization method ID, name, and addr of a local variable. Where the def end is used to define a ruby?? method is initialized. These will be read more in the relevant follow-up chapters.

In the Initialize method, the values of these local variables are passed to the instance variable @cust_id, @cust_name and @cust_addr. The values held here by the local variable are passed together by the new method.

You can now create the object as follows:

Cust1=customer.new ("1", "John", "Wisdom apartments, Ludhiya")
cust2=customer.new ("2", "Poul", "New Empire Road, Khandala ")

member functions for classes in Ruby:

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

Method names are always best in lowercase letters. Your final method 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 statments can be any valid ruby statements. 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 sample class, create an object and call the Hello method to see the result:

#!/usr/bin/ruby

class Sample
  def hello
   puts "Hello ruby!"
  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.