10.4. Element-wise enumerable operations
Collections are born to be traversed, but they also contain special-status individual objects: the first or last in the collection, and the greatest (largest) or least (smallest). Enumerable objects come with several tools for element handling along these lines.
10.4.1. The first method
Enumerable#first, as the name suggests, returns the first item encountered when iterating over the enumerable:
>> [1,2,3,4].first => 1 >> (1..10).first => 1 >> {1 => 2, "one" => "two"}.first => [1, 2]
The object returned by first is the same as the first object you get when you iterate through the parent object. In other words, it’s the first thing yielded by each. In keeping with the fact that hashes yield key/value pairs ...
Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.