# Superclass mismatches

**URL:** <https://www.ruby-forum.com/t/superclass-mismatches/101635>\
**Category:** Ruby\
**Created:** [August 7, 2006, 12:02am UTC](https://www.ruby-forum.com/t/superclass-mismatches/101635 "2006-08-07T00:02:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [August 7, 2006, 12:02am UTC](https://www.ruby-forum.com/t/superclass-mismatches/101635/1 "2006-08-07T00:02:35Z")

</div>

I’m sometimes seeing an error “superclass mismatch”. I have no idea  
what this error is. could anyone either explain it or point me to the  
docs (or src code)?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [August 7, 2006, 12:14am UTC](https://www.ruby-forum.com/t/superclass-mismatches/101635/2 "2006-08-07T00:14:36Z")

</div>

On Mon, Aug 07, 2006 at 07:00:06AM +0900, removed\_email\_address@domain.invalid wrote:

> I’m sometimes seeing an error “superclass mismatch”. I have no idea  
> what this error is. could anyone either explain it or point me to the  
> docs (or src code)?

X = Class.new  
Y = Class.new  
class Foo \< X; end  
class Foo \< Y; end

# ~\> -:4: superclass mismatch for class Foo (TypeError)

In the above situation it’s obvious that you’re saying that Foo is  
derived  
from X at first and then Y (!= X) the second time, but you might have  
run into  
this problem with something like

class Foo \< Struct.new(:a, :b, :c)

# …

end

if you load()ed that file more than once (this can happen if you  
require()  
the file under two different names), since each time Struct.new is  
evaluated  
a new class will be created.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [August 7, 2006, 10:12am UTC](https://www.ruby-forum.com/t/superclass-mismatches/101635/3 "2006-08-07T10:12:07Z")

</div>

Thanks for the explanation. I’m still stumped - it seems like I have  
two classes with the same name:

irb(main):015:0\> ResellerCategory  
=\> Taxonomy::ResellerCategory  
irb(main):016:0\> ResellerCategory.superclass  
=\> Taxonomy::Category  
irb(main):017:0\> ResellerCategory.superclass == Taxonomy::Category  
=\> false

Huh?

irb(main):018:0\> ResellerCategory.superclass.object\_id  
=\> 46413760  
irb(main):019:0\> Taxonomy::Category.object\_id  
=\> 47673490

How did that happen?

irb(main):020:0\> module Taxonomy; class ResellerCategory \< Category;  
end ; end  
TypeError: superclass mismatch for class ResellerCategory  
irb(main):023:0\> module Taxonomy; class ResellerCategory \<  
ResellerCategory.superclass; end ; end  
=\> nil

My questions are:

1. How did that happen? It seems like 1 != 1
2. More importantly, how can I stop this error from happening? (It’s  
caused when the file defining ResellerCategory is loaded a second time)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [August 7, 2006, 11:24am UTC](https://www.ruby-forum.com/t/superclass-mismatches/101635/4 "2006-08-07T11:24:13Z")

</div>

On Mon, Aug 07, 2006 at 05:10:11PM +0900, removed\_email\_address@domain.invalid wrote:

> irb(main):015:0\> ResellerCategory  
> =\> Taxonomy::ResellerCategory  
> irb(main):016:0\> ResellerCategory.superclass  
> =\> Taxonomy::Category  
> irb(main):017:0\> ResellerCategory.superclass == Taxonomy::Category  
> =\> false  
> […]  
> irb(main):018:0\> ResellerCategory.superclass.object\_id  
> =\> 46413760  
> irb(main):019:0\> Taxonomy::Category.object\_id  
> =\> 47673490  
> […]  
> irb(main):020:0\> module Taxonomy; class ResellerCategory \< Category;  
> end ; end  
> TypeError: superclass mismatch for class ResellerCategory  
> irb(main):023:0\> module Taxonomy; class ResellerCategory \<  
> ResellerCategory.superclass; end ; end  
> =\> nil
> 
> My questions are:
> 
> 1. How did that happen? It seems like 1 != 1

Taxonomy::Category has been redefined, as if you were doing  
module Taxonomy; Category = Class.new { … } end  
or maybe  
module Taxonomy; remove\_const :Category; class Category; … end end  
and that were being evaluated more than once.  
Can you show the code?

> 1. More importantly, how can I stop this error from happening? (It’s  
> caused when the file defining ResellerCategory is loaded a second time)

You have to make sure that Taxonomy::Category references the same class  
all  
the time. If you can’t find why it’s changing, you can get by with  
module Taxonomy; class ResellerCategory \< Category; end end  
which must be eval()ed only once, and then replacing in the file to be  
loaded  
repeatedly  
class ResellerCategory \< Category  
with  
class ResellerCategory

HTH
