Latest web development tutorials

Ruby Class Case

The following will create a Ruby class named Customer, declares two methods:

  • display_details:This method is used to display detailed information about customers.
  • total_no_of_customers:The method used to create the total number of customers in the system display.
#!/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_detailsmethod consists of three puts statement, showing the customer ID, customer name and customer address. Wherein, puts statement:

puts "Customer id #@cust_id"

Customer id displayed text and variables @cust_id value on a single line.

When you want to display text and values ​​instance variables on a single line, you need to place the symbol (#) in front of the statement puts the variable name. Text and instance variables with the symbol (#) should use the double quotation marks.

The second method, total_no_of_customers, contains a class variable @@ no_of_customers. Expression @@ no_of_ customers + = 1 at the time of each call method total_no_of_customers, plus the variable no_of_customers 1. In this way, you will get class variables total number of customers.

Now create two customers, as follows:

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

Here, we create two classes of objects Customer, cust1 and cust2, and pass the necessary parameters new methods. When the initialize method is invoked, the necessary attributes of the object is initialized.

Once an object is created, you need to use two objects to call the methods of the class. If you want to invoke any method or data member, you can write code as follows:

cust1.display_details()
cust1.total_no_of_customers()

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

cust2.display_details()
cust2.total_no_of_customers()

Save and execute the code

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

#!/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

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

# 调用方法
cust1.display_details()
cust1.total_no_of_customers()
cust2.display_details()
cust2.total_no_of_customers()

Then, run the program as follows:

$ Ruby main.rb

This produces the following results:

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