0
0
Rubyprogramming~30 mins

Why Enumerable is Ruby's most powerful module - See It in Action

Choose your learning style9 modes available
Why Enumerable is Ruby's Most Powerful Module
📖 Scenario: Imagine you have a list of your favorite fruits and you want to do different things with this list, like finding the longest fruit name, selecting fruits that start with a certain letter, or making a new list with all fruit names in uppercase. Ruby's Enumerable module helps you do all these tasks easily.
🎯 Goal: You will create a simple Ruby program that uses the Enumerable module to work with a list of fruits. You will learn how to use some of its most useful methods to find, select, and transform items in the list.
📋 What You'll Learn
Create an array called fruits with exact values: 'apple', 'banana', 'cherry', 'date', 'elderberry'
Create a variable called min_length and set it to 6
Use select method on fruits to get fruits with length greater than or equal to min_length and store in long_fruits
Use map method on fruits to create a new array upcase_fruits with all fruit names in uppercase
Print the long_fruits and upcase_fruits arrays
💡 Why This Matters
🌍 Real World
Working with lists of data is very common in programming. The Enumerable module helps you easily filter, find, and change items in lists like names, products, or any collection.
💼 Career
Understanding Enumerable methods is essential for Ruby developers because it makes code simpler, cleaner, and more efficient when handling collections.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact string values: 'apple', 'banana', 'cherry', 'date', 'elderberry'
Ruby
Need a hint?

Use square brackets [] to create an array and separate the fruit names with commas.

2
Set the minimum length variable
Create a variable called min_length and set it to the integer 6
Ruby
Need a hint?

Just assign the number 6 to the variable min_length.

3
Select fruits longer than min_length
Use the select method on fruits with a block that keeps fruits whose length is greater than or equal to min_length. Store the result in a variable called long_fruits
Ruby
Need a hint?

Use select with a block that checks fruit.length >= min_length.

4
Create uppercase fruits and print results
Use the map method on fruits to create a new array called upcase_fruits with all fruit names in uppercase. Then print long_fruits and upcase_fruits using p.
Ruby
Need a hint?

Use map with fruit.upcase inside the block. Use p to print both long_fruits and upcase_fruits.