Q:

Ruby program to return the values from the block

belongs to collection: Ruby Blocks Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby program to demonstrate the yield statement... >>
<< Ruby program to demonstrate the argument passing t...