6.12. Ruby class case

发布时间 : 2025-10-25 13:34:03 UTC      

Page Views: 10 views

The following will create a file named Customer to declare two methods:the Ruby class:

  • display_details :This method is used to display the details of the customer

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

6.12.1. Example #

#!/usr/bin/rubyclassCustomer@@no_of_customers=0definitialize(id,name,addr)@cust_id=id@cust_name=name@cust_addr=addrenddefdisplay_details()puts"Customer id #@cust_id"puts"Customer name #@cust_name"puts"Customer address #@cust_addr"enddeftotal_no_of_customers()@@no_of_customers+=1puts"Total number of customers: #@@no_of_customers"endend 

display_details method contains three puts statement that displays the customer ID, customer name, and customer address. Among them puts statement:

puts"Customer id #@cust_id" 

Text will be displayed on a single line Customer id and variables @cust_id value.

When you want to display the text and value of an instance variable on a single line, you need to use the puts the variable name of the statement is preceded by a symbol (#). Text and instance variables with symbols (#) should be marked in double quotation marks.

The second method total_no_of_customers which contains class variables @@no_of_customers expression. @@no_of\_ customers+=1 eachtime the method is called total_no_of_customers when you put the variable no_of_customers add one. In this way, you will 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 created the Customer two objects of a class cust1 and cust2 and to new method to pass the necessary parameters. When initialize method is called, the necessary properties of the object are initialized.

Once the object is created, you need to use two objects to call the class’s methods. If you want to call a method or any data member, you can write codeas follows:

cust1.display_details()cust1.total_no_of_customers() 

The object name is always followed by a period, followed by the method name or data member. We have seen how to use the cust1 object calls two methods. Use cust2 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 main.rb in the file, as follows:

6.12.2. Example #

#!/usr/bin/rubyclassCustomer@@no_of_customers=0definitialize(id,name,addr)@cust_id=id@cust_name=name@cust_addr=addrenddefdisplay_details()puts"Customer id #@cust_id"puts"Customer name #@cust_name"puts"Customer address #@cust_addr"enddeftotal_no_of_customers()@@no_of_customers+=1puts"Total number of customers: #@@no_of_customers"endend#create objectcust1=Customer.new("1","John","Wisdom Apartments, Ludhiya")cust2=Customer.new("2","Poul","New Empire road, Khandala")#calling methodcust1.display_details()cust1.total_no_of_customers()cust2.display_details()cust2.total_no_of_customers() 

Next, run the program, as follows:

$ ruby main.rb 

This will produce 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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。