May 12, 2021 Ruby
Here's a Ruby class called Customer, and you'll declare two methods:
#!/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 method contains three puts statements that show the customer ID, customer name, and customer address. Where the puts statement:
puts "Customer id #@cust_id"
The text Customer id is displayed on a single line, followed by the variable @cust_id value of .
When you want to display the text and values of an instance variable on a single line, you need to place the symbol before the variable name of the puts statement. Text and instance variables with symbols should be marked with double quotes.
The second method, total_no_of_customers, contains the class @@no_of_customers. E xpression @@no_of_ customers plus 1 each time total_no_of_customers method is called no_of_customers 1. In this way, you get the total number of customers in the class variable.
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 objects of the Customer class, cust1 and cust2, and pass the necessary parameters to the new method. When the initialize method is called, the necessary properties of the object are initialized.
Once an object is created, you need to use two objects to call the method of the class. If you want to call a method or any data member, you can write code as follows:
cust1.display_details() cust1.total_no_of_customers()
The object name is always followed by a dot, followed by a method name or data member. W e've seen how to call two methods using the cust1 object. With the cust2 object, you can also call two methods, as follows:
cust2.display_details() cust2.total_no_of_customers()
Now, put all the source code in the 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()Try it out . . .
Next, run the program as follows:
$ ruby main.rb
This results in the following:
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