method

each_with_object

Importance_1
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: Enumerable

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v3.2.13) is shown here.

each_with_object(memo) public

Iterates over a collection, passing the current element and the memo to the block. Handy for building up hashes or reducing collections down to one object. Examples:

%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
# => {'foo' => 'FOO', 'bar' => 'BAR'}

Note that you can’t use immutable objects like numbers, true or false as the memo. You would think the following returns 120, but since the memo is never changed, it does not.

(1..5).each_with_object(1) { |value, memo| memo *= value } # => 1
Show source
Register or log in to add new notes.