Ruby program to demonstrate the coerce() function

Ruby Example: Write a program to demonstrate the coerce() function.
Submitted by Nidhi, on December 15, 2021

Problem Solution:

In this program, we will create two variables and initialize them with floating-point numbers. Then we will use coerce() function to combine two floating-point numbers and return an array.

Program/Source Code:

The source code to demonstrate the coerce() function is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate 
# the coerce() function

num1 = 10.23;
num2 = 20.24;

arr = num1.coerce(num2);

print "Returned array: ",arr;

Output:

Returned array: [20.24, 10.23]

Explanation:

In the above program, we created two variables num1, num2 and initialized 10.23, 20.24 respectively. Then we called coerce() library function to return the array of given floating-point numbers and printed the returned array.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.