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.

attr_accessor_with_default with collections

#6496

I started digging into this when I stumbled across this SO question: http://stackoverflow.com/questions/3481406
To give an example of the unexpected behavior:

class Container
  attr_accessor_with_default :foo, [0, 0]
end

# This works as expected
c = Container.new
c.foo = ["foo", "bar"]  # => ["foo", "bar"]
Container.new.foo       # => [0, 0]
c.foo[0] = 0            # => [0, "bar"]
Container.new.foo       # => [0, 0]

# This changes the class default value
c2 = Container.new
c2.foo                  # => [0, 0]
c2.foo[0] = "problem!"  # => ["problem!", 0]
Container.new.foo       # => ["problem!", 0]

I'll be submitting a patch shortly.

Reported by parrish · March 1st, 2011 @ 07:14 PM

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

Activity

  1. parrish
    parrish

    I'm sure there is a more elegant approach to this, but I couldn't think of a simpler one that maintained scopes correctly.
    Refactoring suggestions are welcome :)

    Discarding the block option would definitely clarify the intent and code in this method, but that's just my two cents.

    March 1st, 2011 @ 07:24 PM

  2. parrish
    parrish
    • Tag changed from activesupport, core_ext, module to activesupport, core_ext, module, patch

    March 1st, 2011 @ 07:28 PM