We can create a class and then create another class with the same name. That is not surprising.
[1] pry(main)> class A; end
=> nil
[2] pry(main)> a = A.new
=> #<A:0x0000000bd8a008>
[3] pry(main)> A = Class.new
(pry):3: warning: already initialized constant A
(pry):1: warning: previous definition of A was here
=> A
[4] pry(main)> new_a = A.new
=> #<A:0x0000000be001e0>
[5] pry(main)> a.class.name == new_a.class.name
=> true
[6] pry(main)> a.class == new_a.class
=> false
[7] pry(main)> a.class == A
=> false
[8] pry(main)> new_a.class == A
=> true
However, after redefining the constant we get what seems to be a collision: constant A
and new_a.class
method return the new class, while a.class
returns the original class. These classes are different, yet they have the same name. How can this be possible and what exactly is going on when this code is executed?