ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.