Iterators

Learn about iterators in Ruby and how they can be chainable.

Iterators are chainable

Methods on arrays and hashes that take a block are also called iterators. We say they iterate over the array, meaning that these methods take each element of the array and do something with it. In Ruby, iterators are chainable, adding functionality on top of each other.

That means that if we don’t pass a block to an iterator method, such as each, collect, or select, then we’ll get an iterator object back. We can then call more methods on these iterator objects and finally pass a block, like so:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy