Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] core_ext/enumerable multiple fixes

#6510

I got burned by the fact that Enumerable#index_by does not return an Enumerator if called without block, so I took a moment to fix the issues I could find in the enumerable extension, namely:

#index_by, #group_by and #each_with_object now return Enumerators as they should.

#many? iterates only as much as needed to determine the result. It also doesn't rely on #size (which not all Enuemrables respond to).

The patch also improves the robustness of the tests. #each_with_object is also optimized by not capturing the block.

Problems not addressed:
* most extensions assume that the Enumerable yields only one argument. * the definition of #many? can be confusing because the form without block doesn't test for truthiness. Thus foo.many? is not necessarily equal to foo.many?{|x| x}, and foo.any? can be false while foo.many? can be true (e.g. foo = [nil, nil]). Discounting nil/false would be ideal, except that it could break compatibility, so I leave this up to the powers that be.

Thanks!

Marc-André

Reported by Marc-AndrĂ© Lafortune · March 3rd, 2011 @ 05:11 AM

State: new
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Importance changed from to Low

    Hi Marc-André,

    I cleaned up your ticket a bit. Please be sure to wrap inline code in backticks, since starting a line with a # will turn it into a header. :)

    March 6th, 2011 @ 09:16 AM