This project is archived and is in readonly mode.

#4625 ✓resolved
Thomas LECAVELIER

class_inheritable_accessor should let subclasses inherit from the superclass's attribute

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

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?

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh May 17th, 2010 @ 03:33 PM

    • 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.

  • Neeraj Singh

    Neeraj Singh May 19th, 2010 @ 06:31 AM

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

  • Rizwan Reza

    Rizwan Reza May 19th, 2010 @ 12:19 PM

    • no changes were found...
  • Rizwan Reza

    Rizwan Reza May 19th, 2010 @ 12:28 PM

    • State changed from “new” to “resolved”
  • Thomas LECAVELIER

    Thomas LECAVELIER May 31st, 2010 @ 09:57 AM

    • 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?

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages