×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to demonstrate the coerce() function

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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