home PYTHONJAVA
 

Ruby Class Case

The following will make a Ruby class considered Customer that announces two methods:

  • display_details: This strategy is utilized to show the subtleties of the client.
  • total_no_of_customers: This technique is utilized to show the complete number of clients made in the framework.

Instance

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

display_details technique contains three puts proclamations that show the client ID, client name, and client address. Where the puts statement:

puts " Customer id #@cust_id"

The estimation of the content Customer id and the variable @cust_id will be shown on a solitary line.

When you need to show the content and estimations of a case variable on a solitary line, you have to put the image (#) before the variable name of the puts proclamation. Content and occurrence factors with an image (#) ought to be encased in twofold statements.

The second strategy, total_no_of_customers, contains the class variable @@no_of_customers. Articulation @@no_of_ Customers+=1 increases the variable no_of_customers by 1 each time the strategy total_no_of_customers is called. Along these lines, you will get the complete number of clients in the class variable.

Now make two clients as follows:

Here, we made two objects of the Customer class, cust1 and cust2, and passed the essential parameters to the new strategy. At the point when the instate strategy is called, the important properties of the article are introduced.

Once an item is made, you have to utilize two articles to call the strategies for the class. On the off chance that you need to call a technique or any information part, you can compose the code as follows:

cust1.display_details() cust1.total_no_of_customers()

The object name is constantly trailed by a dab, trailed by the strategy name or information part. We have perceived how to call two techniques utilizing the cust1 object. With the cust2 object, you can likewise call two techniques as follows:

cust2.display_details() cust2.total_no_of_customers()

Save and execute code

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

Instance

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





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.