English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Class Examples

Below, a Ruby class named Customer will be created, declaring two methods:

  • display_details: This method is used to display the detailed information of customers.

  • total_no_of_customers: This method is used to display the total number of customers created in the system.

Online Example

#!/usr/bin/ruby
 
class Customer
   @@no_of_customers=0
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
    end
    def total_no_of_customers()
       @@no_of_customers += 1
       puts "Total number of customers: #@@no_of_customers"
    end
end

display_details The method contains three puts statements, displaying the customer ID, customer name, and customer address. Among them, the puts statement:

puts "Customer id #@cust_id"

将在一个单行上显示文本 Customer id 和变量 @cust_id 的值。

will display the text Customer id and the value of the variable @cust_id on a single line.

When you want to display the text and value of a sample variable on a single line, you need to place the symbol (#) before the variable name in the puts statement. The text and the sample variable with the symbol (#) should be enclosed in double quotes. The second method, total_no_of_customers, contains the class variable @@no_of_customers. The expression @@no_+=1 customers 1In each call to the method total_no_of_customers, add the variable no_of_customers

. In this way, you will get the total number of customers in the class variable.

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

Now create two customers as follows:1 Here, we created two objects of the Customer class, cust2and cust

, and pass the necessary parameters to the new method. When the initialize method is called, the necessary properties of the object are initialized.

cust1.display_details()
cust1.total_no_of_customers()

The object name is always followed by a dot, then the method name or data member. We have seen how to use cust1 An object calls two methods. Use cust2 An object, you can also call two methods as follows:

cust2.display_details()
cust2.total_no_of_customers()

Save and execute the code

Now, put all the source code in the main.rb file as follows:

Online Example

#!/usr/bin/ruby
 
class Customer
   @@no_of_customers=0
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
   end
   def total_no_of_customers()
      @@no_of_customers += 1
      puts "Total number of customers: #@@no_of_customers"
   end
end
 
Create an object
cust1=Customer.new("1"John", "Wisdom Apartments, Ludhiya"
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
 
# Call method
cust1.display_details()
cust1.total_no_of_customers()
cust2.display_details()
cust2.total_no_of_customers()

Next, run the program as shown below:

$ ruby main.rb

This will produce the following result:

Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Total number of customers: 1
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
Total number of customers: 2