[PATCH] core_ext/enumerable multiple fixes #893

Closed
lighthouse-import opened this Issue May 16, 2011 · 2 comments

1 participant

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6510
Created by Marc-André Lafortune - 2011-03-03 05:11:39 UTC

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é

@lighthouse-import

Imported from Lighthouse.
Comment by Jeff Kreeftmeijer - 2011-03-06 09:16:05 UTC

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment