Ruby Tutorial
Ruby CGI
Ruby Advanced
Iterators in Ruby are methods that return successive members of a collection, such as an array or a hash. Here's a tutorial on how to use them:
1. each
The each
method is the most basic iterator, it simply executes a block of code for each element in a collection:
array = [1, 2, 3, 4, 5] array.each do |num| puts num end
This will output each number in the array on a new line.
2. map
The map
method returns a new array containing the values returned by the block for each element in the original array:
array = [1, 2, 3, 4, 5] new_array = array.map do |num| num * 2 end puts new_array # Outputs: [2, 4, 6, 8, 10]
This creates a new array where each number is twice the number in the original array.
3. select
The select
method returns a new array containing only the elements for which the block returns true
:
array = [1, 2, 3, 4, 5] even_numbers = array.select do |num| num.even? end puts even_numbers # Outputs: [2, 4]
This creates a new array that only contains the even numbers from the original array.
4. inject
The inject
method combines all elements of an array by applying a binary operation that you specify:
array = [1, 2, 3, 4, 5] sum = array.inject(0) do |result, num| result + num end puts sum # Outputs: 15
This calculates the sum of all numbers in the array. The argument to inject
(in this case, 0
) is the initial value for the result.
These are just a few examples of iterators in Ruby. There are many more, including reject
(the opposite of select
), all?
(checks if all elements satisfy a condition), any?
(checks if any element satisfies a condition), and none?
(checks if no element satisfies a condition), among others. The key thing to remember is that iterators are a powerful tool for working with collections of data in a concise and readable way.
Ruby each iterator:
The each
iterator is a fundamental method in Ruby for iterating over elements of a collection.
array = [1, 2, 3, 4] array.each do |element| puts element end
Using iterators with arrays in Ruby: Iterators work seamlessly with arrays, allowing you to perform actions on each element.
fruits = ['apple', 'banana', 'orange'] fruits.each { |fruit| puts "I love #{fruit}" }
Ruby times iterator:
The times
iterator is useful for executing a block of code a specified number of times.
3.times { |i| puts "Iteration #{i}" }
Custom iterators in Ruby:
You can create your own iterators using methods and the yield
keyword.
def my_custom_iterator yield('Hello') yield('World') end my_custom_iterator { |message| puts message }
Ruby map iterator example:
The map
iterator transforms each element of an array based on the block provided.
numbers = [1, 2, 3, 4] doubled = numbers.map { |num| num * 2 }
Filtering with iterators in Ruby:
Use iterators like select
to filter elements based on a condition.
numbers = [1, 2, 3, 4, 5] evens = numbers.select { |num| num.even? }
Ruby each_with_index iterator:
each_with_index
allows you to access both the element and its index.
fruits = ['apple', 'banana', 'orange'] fruits.each_with_index { |fruit, index| puts "#{index + 1}. #{fruit}" }
Nested iterators in Ruby: You can nest iterators to traverse through multi-dimensional collections.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] matrix.each do |row| row.each { |element| puts element } end
Ruby Enumerator class:
The Enumerator
class allows you to create custom iterators for any object.
enumerator = [1, 2, 3].each
Lazy evaluation with iterators in Ruby:
lazy
can be used to enable lazy evaluation, useful for large datasets.
numbers = (1..Float::INFINITY).lazy.select { |num| num.even? }
Ruby Iterator vs. Loop: Iterators are more idiomatic and expressive than loops, promoting cleaner code.
# Using iterator array.each { |element| puts element } # Equivalent loop for element in array puts element end
Chaining iterators in Ruby: You can chain iterators to perform complex operations in a concise manner.
numbers = [1, 2, 3, 4, 5] result = numbers.select(&:even?).map { |num| num * 2 }