Ruby, with its elegance and expressiveness, offers powerful tools called Enumerables and Iterators. Let's delve into these concepts and understand how they can streamline your Ruby coding experience.
Enumerables
Are modules in Ruby that provide collection classes with a set of methods for traversing and manipulating
(Imagine Enumerables as your trusty sidekick, armed with superpowers to handle lists effortlessly.)
each : method iterates over each element in a collection and executes a block of code for each element.
example:
fruits = ["apple", "banana", "orange"]
fruits.each do |fruit|
puts "I love #{fruit}s!"
end
#output
#I love apples!
#I love bananas!
#I love oranges!
map : method applies a transformation to each element in a collection and returns a new array containing the transformed elements.
example:
numbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.map { |n| n * n }
puts squared_numbers.inspect
#output
#[1, 4, 9, 16, 25]
reject : method returns a new array containing elements for which the given block returns false.
example:
scores = [85, 92, 78, 65, 97]
failed_scores = scores.reject { |score| score >= 80 }
puts failed_scores.inspect
#output
#[78, 65]
reduce : method combines all elements of a collection by applying a binary operation specified by a block or a symbol.
example:
numbers = [1, 2, 3, 4, 5]
sum = numbers.reduce(:+)
puts "The sum of numbers is: #{sum}"
#output
#The sum of numbers is: 15
find : method returns the first element for which the given block returns true.
example:
numbers = [1, 2, 3, 4, 5]
even_number = numbers.find { |num| num.even? }
puts "The first even number is: #{even_number}"
#output
#The first even number is: 2
sort : method returns a new array containing the elements of the collection sorted.
example:
fruits = ["banana", "apple", "orange"]
sorted_fruits = fruits.sort
puts sorted_fruits.inspect
#output
#["apple", "banana", "orange"]
select : method returns a new array containing elements for which the given block returns true.
example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = numbers.select { |num| num.even? }
puts even_numbers.inspect
#output
#[2, 4, 6, 8, 10]
Iterators
Are methods that allow you to traverse through elements in a collection one by one. Many Enumerable methods are implemented using iterators internally.
(Think of Iterators as your personal GPS for navigating through lists.)
each : It allows you to iterate over each element of a collection (like an array or a hash) and execute a block of code for each element.
example:
names = ["Alice", "Bob", "Charlie", "David"]
names.each_with_index do |name, index|
puts "Index #{index}: #{name}"
end
#output
#Index 0: Alice
#Index 1: Bob
#Index 2: Charlie
#Index 3: David
collect : iterator is synonymous with map
in Ruby. It iterates over each element of a collection, applies a transformation to each element according to a block, and returns a new array containing the transformed elements.
example:
numbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.collect { |num| num * num }
puts squared_numbers.inspect
#output
#[1, 4, 9, 16, 25]
upto : iterator iterates from the current number up to a specified number and executes a block of code for each iteration.
example:
1.upto(3) do |num|
puts num
end
#output
#1
#2
#3
downto : iterator iterates from the current number down to a specified number and executes a block of code for each iteration.
example:
3.downto(1) do |num|
puts num
end
#output
#3
#2
#1
step : iterator iterates from a starting value up to a specified value, stepping by a specified increment, and executes a block of code for each iteration.
example:
1.step(5, 2) do |num|
puts num
end
#output
#1
#3
#5
times : iterator allows you to execute a block of code a specified number of times. It starts from 0 and increments by 1 each time.
example:
5.times do |num|
puts num + 1
end
#output
#1
#2
#3
#4
#5
be careful :
While some Enumerable methods are also iterators (e.g.,each,each_with_index), not all Enumerable methods fit this description. For example,map and reduce are Enumerable methods but are not considered iterators because they don't directly involve iterating over elements in the same way each does. Similarly, while some iterators are commonly used with Enumerable methods, not all iterators are part of the Enumerable module.