Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Class Example

Ruby Class Example

Below we'll create a Ruby class named Customer and declare two methods:

Example

!/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 The method contains three puts statements that display the customer ID, customer name, and customer address. Among them, the puts statement:

puts

"

Customer id #@cust_id

"

displays the text Customer id and the value of the variable @cust_id on a single line.

When you want to display an instance variable's text and value on a single line, you need to place the symbol (#) before the variable name in the puts statement. The text and the instance variable with the symbol (#) should be enclosed in double quotes.

The second method, total_no_of_customers, contains the class variable @@no_of_customers. The expression @@no_of_ customers+=1 increments the variable no_of_customers by 1 each time the method total_no_of_customers is called. In this way, you get the total number of customers in the class variable.

Now create two customers, as shown below:

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 arguments to the new method. When the initialize method is called, the object's necessary attributes are initialized.

Once the objects are created, you need to use the two objects to call the class's methods. 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

(

)

An object name is always followed by a dot, then the method name or data member. We've seen how to call the two methods using the cust1 object. Using the cust2 object, you can call the two methods as well, as shown below:

cust2

.

display_details

(

)

cust2

.

total_no_of_customers

(

)

Save and execute the code

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

Example

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

Creating objects

cust1

=

Customer

.

new

(

"

1

"

,

"

John

"

,

"

Wisdom Apartments, Ludhiya

"

)

cust2

=

Customer

.

new

(

"

2

"

,

"

Poul

"

,

"

New Empire road, Khandala

"

)

Call the method

cust1

.

display_details

(

)

cust1

.

total_no_of_customers

(

)

cust2

.

display_details

(

)

cust2

.

total_no_of_customers

(

)

Try it »

Then, run the program as follows:


$ ruby main.rb

This will produce the following result:


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