×

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 return the values from the block

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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