This project is archived and is in readonly mode.

#6510 new
Marc-André Lafortune

[PATCH] core_ext/enumerable multiple fixes

Reported by Marc-André Lafortune | March 3rd, 2011 @ 05:11 AM

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é

Comments and changes to this ticket

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer March 6th, 2011 @ 09:16 AM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages