belongs to collection: Ruby Blocks Programs
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.
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Program/Source Code:
The source code to return values from the block is given below. The given program is compiled and executed successfully.
Output:
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