# Problem when 'loading' a file containing a class derived fro

**URL:** <https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203>\
**Category:** Ruby\
**Created:** [July 20, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203 "2006-07-20T23:28:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 20, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203/1 "2006-07-20T23:28:15Z")

</div>

I actually spotted this in a rails app but traced it back to ruby. Is  
this a real problem/bug or am I missing something obvious?

$ cat \> person.rb  
class Person \< Struct.new(:forename, :surname)  
end  
$ irb  
irb\> load ‘person.rb’  
=\> true  
irb\> load ‘person.rb’  
TypeError: superclass mismatch for class Person  
from ./person.rb:1  
from (irb):2

This behaviour has been experienced with both ruby 1.8.2 and 1.8.4.

Chris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 20, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203/2 "2006-07-20T23:41:23Z")

</div>

On Fri, Jul 21, 2006 at 06:27:29AM +0900, Chris R. wrote:

> TypeError: superclass mismatch for class Person  
> from ./person.rb:1  
> from (irb):2
> 
> This behaviour has been experienced with both ruby 1.8.2 and 1.8.4.

Each time Struct.new(\*args) is evaluated, it creates a new class:

Struct.new(:a,:b) # =\> #Class:0xa7dd12f4  
Struct.new(:a,:b) # =\> #Class:0xa7dd10c4  
Struct.new(:foo) == Struct.new(:foo) # =\> false  
class X \< Struct.new(:a); end  
class Y \< Struct.new(:a); end  
X.superclass == Y.superclass # =\> false

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 20, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203/3 "2006-07-20T23:31:48Z")

</div>

Hi –

On Fri, 21 Jul 2006, Chris R. wrote:

> TypeError: superclass mismatch for class Person  
> from ./person.rb:1  
> from (irb):2
> 
> This behaviour has been experienced with both ruby 1.8.2 and 1.8.4.

It’s because you’re trying to give Person a new superclass: Struct.new  
gets called again when you reload the file.

David

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 21, 2006, 5:33am UTC](https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203/4 "2006-07-21T05:33:35Z")

</div>

On Jul 20, 2006, at 5:27 PM, Chris R. wrote:

> TypeError: superclass mismatch for class Person  
> from ./person.rb:1  
> from (irb):2
> 
> This behaviour has been experienced with both ruby 1.8.2 and 1.8.4.
> 
> Chris

Hence to use this with rails which will reload stuff like it’s going  
out of style:

Person = Struct.new(:forename, :surname)

class Person

# optionally add methods here

end

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 21, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/problem-when-loading-a-file-containing-a-class-derived-fro/99203/5 "2006-07-21T12:06:07Z")

</div>

Ahhh I see. Thanks for the explanation guys.

Chris
