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 for ActiveSupport that Array#uniq and #uniq! supports blocks

#1595

I found it useful when i wanted to ensure that array has no duplicate values while somehow transforming them, but not affecting original array so I wrote a patch that provides uniq and uniq! block support


# Same as Array#uniq but accepts block argument which result will 
# be used for determining which values are unique.
#
#   a = ['a', 'A', 'bb', 'bB', 'Bb', 'Cc', 'cc']
#   a.uniq { |i| i.downcase } # => ["a", "bb", "Cc"]

Github commit: http://github.com/arturaz/rails/...

The append features part is lame, I think it should be rewritten somehow :)

Reported by Artūras Šlajus · December 18th, 2008 @ 02:17 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Dmitry Polushkin
    Dmitry Polushkin

    Overheat, it could be done using:

    
    a.map(&:downcase).uniq
    

    December 19th, 2008 @ 11:20 AM

  2. Artūras Šlajus
    Artūras Šlajus

    But doing so destroys original values.

    For example my patch is very useful for tagging support where we want to keep original values.

    
    >> a = ["Drum and bass", "drum'and'bass"]
    => ["Drum and bass", "drum'and'bass"]
    >> a.map { |i| i.parameterize.to_s }.uniq
    => ["drum-and-bass"]
    >> a.uniq { |i| i.parameterize.to_s }
    => ["Drum and bass"]
    

    December 19th, 2008 @ 11:51 AM

  3. Pratik
    Pratik
    • State changed from new to wontfix

    I don't think this belongs in core.

    Thanks.

    December 19th, 2008 @ 01:46 PM

  4. Artūras Šlajus
    Artūras Šlajus

    And why does other stuff in activesupport belong in core? Not that I like being itchy but this patch doesn't hurt and perhaps it will be useful for someone?

    What's the motivation of dropping it out?

    December 19th, 2008 @ 11:06 PM