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] Add Block Support to ActiveSupport OrderedHash merge

#4838

ActiveSupport's OrderedHash's implementation of merge and merge! does not support passing a block as per the standard library (c.f. http://ruby-doc.org/core/classes/Hash.html#M002880 )

The attached patch adds support to match the standard library's method signature:

hsh.merge(other_hash){|key, oldval, newval| block} → a_hash

Tests are also included.

This makes it possible to effectively map over an OrderedHash without turning it into an array by merging it with itself:

a = ActiveSupport::OrderedHash.new
a["age"] = 1
a["height"] = 15
a.merge(a) { |k, v| v * 2 }
# => #<OrderedHash {"height"=>30, "age"=>2}>

Reported by Paul Mucur · June 12th, 2010 @ 12:15 AM

State: committed
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Paul Mucur
    Paul Mucur

    This would resolve ticket #3590.

    June 12th, 2010 @ 12:24 AM

  2. Paul Barry
    Paul Barry

    This looks good. I've attached a patch that re-organizes the test for merge into a few smaller tests.

    June 12th, 2010 @ 04:29 AM

  3. Repository
    Repository
    • State changed from new to committed

    (from [58e21a4a0d4eefc395139e88c1f184b9eaf0b4c4]) Support passing a block to ActiveSupport::OrderedHash's merge and merge! [#4838 state:committed]

    For better consistency with Ruby's own Hash implementation.

    Signed-off-by: Xavier Noria fxn@hashref.com
    http://github.com/rails/rails/commit/58e21a4a0d4eefc395139e88c1f184...

    June 13th, 2010 @ 03:19 AM