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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

class_inheritable_accessor should let subclasses inherit from the superclass's attribute

#4625

First a normal case, where things behave as expected:

class A
  class_inheritable_accessor :foo
end

A.foo = 'foo'

class B < A
end

B.foo # => 'foo', as expected

Now, a rather disturbing scenario:

class A
  class_inheritable_accessor :foo
end

class B < A
end

A.foo = 'foo'
B.foo # => nil, 'foo' expected

Should we consider that a bug?

Reported by Thomas LECAVELIER · May 17th, 2010 @ 12:31 PM

State: resolved
Milestone: none
Assigned to: Rizwan Reza Rizwan Reza
Importance: none

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Tag set to activesupport, patch, rails3

    This is expected behavior. I found following test.

      def test_inheritance
        @klass.class_inheritable_accessor :a
        @klass.a = 'a'
    
        @sub = eval("class FlogMe < @klass; end; FlogMe")
    
        @klass.class_inheritable_accessor :b
    
        assert_respond_to @sub, :a
        assert_respond_to @sub, :b
        assert_equal @klass.a, @sub.a
        assert_equal @klass.b, @sub.b
        assert_equal 'a', @sub.a
        assert_nil @sub.b
    
        @klass.b = 'b'
        assert_not_equal @klass.b, @sub.b
        assert_equal 'b', @klass.b
        assert_nil @sub.b
    
        @sub.a = 'A'
        assert_not_equal @klass.a, @sub.a
        assert_equal 'a', @klass.a
        assert_equal 'A', @sub.a
    
        @sub.b = 'B'
        assert_not_equal @klass.b, @sub.b
        assert_equal 'b', @klass.b
        assert_equal 'B', @sub.b
      end
    

    As you can see

    @klass.b = 'b'
    assert_not_equal @klass.b, @sub.b
    

    I am adding two lines to the doc to make it clear that child copies attributes from parents when subclass is created.

    May 17th, 2010 @ 03:33 PM

  2. Neeraj Singh
    Neeraj Singh

    This ticket can be closed. I have update the doc.

    May 19th, 2010 @ 06:31 AM

  3. Rizwan Reza
    Rizwan Reza
    • No changes were found…

    May 19th, 2010 @ 12:19 PM

  4. Rizwan Reza
    Rizwan Reza
    • State changed from new to resolved

    May 19th, 2010 @ 12:28 PM

  5. Thomas LECAVELIER
    Thomas LECAVELIER
    • Assigned user set to Rizwan Reza

    Thanks for your documentation update. It states very clearly the implementation rationale.

    However, I believe that implementation violates the Principle of Least Surprise, and that the behavior, despite being documented and tested, is not expected.

    What do you think?

    May 31st, 2010 @ 09:57 AM