liveBook · Manning

Each_entry

As explained in The Well-Grounded Rubyist, Fourth Edition

The Ruby each method invokes a block once for every element in a collection or once for every value produced by an enumerator.

`each_entry` is listed among the methods provided by Ruby’s `Enumerable` module, but its distinct behavior is not shown here. The iteration contract beneath `Enumerable` is `each`: a class supplies an `each` method that yields items to a block, one at a time. section 10.1

The underlying each pattern

`[1, 2, 3].each { |n| puts n }` hands each number to the block. With a block, array `each` returns the original array; the block’s return values are discarded. Use `map` instead when you need a new array of block results. section 6.3.6 section 10.6.1

Listing 10.1 An Enumerable class and its deployment of the each method
class Rainbow
  include Enumerable
  def each
    yield "red"
    yield "orange"
    yield "yellow"
    yield "green"
    yield "blue"
    yield "indigo"
    yield "violet"
  end
end

A custom class can yield its own sequence through `each`. Including `Enumerable` then makes methods built on that iteration available; the listing does not demonstrate a call to `each_entry`. section 10.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Names `each_entry` without defining its distinct behavior; explains `each` through yielding examples and a simple implementation.
Examples
Shows array iteration and a custom `Enumerable` class yielding colors; provides no direct `each_entry` call.
What it assumes
Assumes basic Ruby classes and blocks while showing how a custom `each` yields values.
What you can do after
Evaluate/understand `each_entry` as a listed `Enumerable` method and build an `each` method that supplies Enumerable’s iteration contract.
How it connects
Connects `each` to code blocks, `Enumerable`, `Enumerator`, and `map`, without establishing how `each_entry` differs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.