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.

Subclasses of HashWithIndifferentAccess dup the wrong class

#5724

There are two instances of HashWithIndifferentAccess being named explicitly in its own class. No bug there, but if a new class is created that inherits from HashWithIndifferentAccess, the new_from_hash_copying_default class method and the dup instance method incorrectly return an instance of HashWithIndifferentAccess rather than the newly-created class.

class FooHash < HashWithIndifferentAccess
end

foo = FooHash.new # => {}
foo.class         # => FooHash
bar = foo.dup     # => {}
bar.class         # => HashWithIndifferentAccess

Reported by Steve Richert · September 28th, 2010 @ 03:56 PM

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

Activity

  1. Steve Richert
    Steve Richert

    Patched, with no tests as it seems pretty straightforward.

    September 28th, 2010 @ 04:00 PM

  2. laserlemon
    laserlemon
    • Tag changed from duplicate, hashwithindifferentaccess to hashwithindifferentaccess

    September 29th, 2010 @ 05:20 PM

  3. David Trasbo
    David Trasbo
    • State changed from new to incomplete
    • Importance changed from to Low

    +1, but:

    Rule no. 1: Always add tests. This is no exception. :)

    And I'd replace self.class.new with just class.new.

    October 10th, 2010 @ 04:51 PM

  4. laserlemon
    laserlemon

    Here's a new patch including tests. I also discovered that HashWithIndifferentAccess duplication doesn't preserve a default value so I resolved that issue as well. Thank you!

    October 11th, 2010 @ 12:10 PM

  5. laserlemon
    laserlemon

    I should also mention that class.new isn't valid Ruby, so I stuck with self.class.new.

    October 11th, 2010 @ 12:14 PM

  6. David Trasbo
    David Trasbo

    Ah, ok. Silly me, didn't think about that!

    Anyways: +1

    Patch applies cleanly and tests pass. Please do unify all the commits in the patch, though.

    October 11th, 2010 @ 06:57 PM

  7. laserlemon
    laserlemon

    Thanks again for the advice. Here's a new, single-commit diff with a message resolving the ticket.

    October 12th, 2010 @ 02:25 AM

  8. ara.t.howard
    ara.t.howard

    some people might be interested in this:

    http://github.com/ahoward/map

    i've fought long and hard with HashWithIndiffentAccess and finally broke down and merged it with OrderedHash - Map is the result. it fixes the issue in this thread too.

    October 12th, 2010 @ 03:39 AM

  9. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:26 AM

  10. Repository
    Repository
    • State changed from incomplete to resolved

    (from [02039e9752bcb214c2ad85859ecf47dcbb892225]) Ensure that HashWithIndifferentAccess duplication preserves class (for sublclasses) and default value [#5724 Subclasses of HashWithIndifferentAccess dup the wrong class state:resolved]

    Signed-off-by: Santiago Pastorino santiago@wyeworks.com
    https://github.com/rails/rails/commit/02039e9752bcb214c2ad85859ecf4...

    November 7th, 2010 @ 12:21 PM