Ruby class case


Release date:2023-10-26 Update date:2023-10-27 Editor:admin View counts:225

Label:

Ruby class case

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.

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:

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

Powered by TorCMS (https://github.com/bukun/TorCMS).