Ruby program to return the values from the block

Ruby Example: Write a program to return the values from the block.
Submitted by Nidhi, on December 28, 2021

Problem Solution:

In this program, we will create a block to return values from the block. Here we will return even numbers stored in the created array.

Program/Source Code:

The source code to return values from the block is given below. The given program is compiled and executed successfully.

# Ruby program to return values from block

arr = Array(1..10);

puts "Even numbers from array:";
puts arr.select { |num| num.even? }

Output:

Even numbers from array:
2
4
6
8
10

Explanation:

In the above program, we created an array of 5 integers. Then we created a block to return even numbers from the created array and printed the result.

Ruby Blocks Programs »




Comments and Discussions!

Load comments ↻





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