On Thu, Aug 29, 2013 at 11:08 PM, Leslie Viljoen <lists / ruby-forum.com> wrote:
> Robert Klemme wrote in post #1119939:
>> On Thu, Aug 29, 2013 at 12:20 PM, Leslie Viljoen <lists / ruby-forum.com>
>> wrote:
>>> I know which code causes the problem - it's some manual loading that
>>> should probably not be there. But you've helped me understand why the
>>> error occurs so thanks a lot.
>>
>> #remove_const is not needed - overwriting is sufficient
>>
>> irb(main):001:0> class A;end
>> => nil
>> irb(main):002:0> class B < A; end
>> => nil
>> irb(main):003:0> B.superclass
>> => A
>> irb(main):004:0> B.superclass.equal? A
>> => true
>> irb(main):005:0> A = Class.new
>> (irb):5: warning: already initialized constant A
>> => A
>> irb(main):006:0> B.superclass.equal? A
>> => false
>> irb(main):007:0> B.superclass
>> => A
>> irb(main):008:0> A
>> => A
>
> Yes but "Class.new" will always produce a class with a new object_id -
> that's not the same as reopening the class, which leaves the object_id
> intact.

Yes, obviously. But what is your point? With reopening the class you
cannot produce the error you showed initially, can you?

irb(main):001:0> class A;end
=> nil
irb(main):002:0> class B < A; end
=> nil
irb(main):003:0> class B < A; def x; end end
=> nil
irb(main):004:0> class B; def y; end end
=> nil
irb(main):005:0> class A; def z; end end
=> nil
irb(main):006:0> class B < A; def a; end end
=> nil
irb(main):007:0> class B; def b; end end
=> nil
irb(main):008:0> A = Class.new
(irb):8: warning: already initialized constant A
=> A
irb(main):009:0> class B; def c; end end
=> nil
irb(main):010:0> class B < A; def e; end end
TypeError: superclass mismatch for class B
        from (irb):10
        from /usr/bin/irb:12:in `<main>'

You said earlier:

> So the only way this can happen is if the constant is deleted and
> recreated? In my tests, modifying the class doesn't change its ID.

And to that I replied that deleting the constant (i.e. #remove_const)
is not necessary (see quote at the beginning).

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/