[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem when 'loading' a file containing a class derived from a struct

chrisjroos@gmail.com

7/20/2006 9:27:00 PM

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

4 Answers

dblack

7/20/2006 9:31:00 PM

0

Mauricio Fernández

7/20/2006 9:38:00 PM

0

On Fri, Jul 21, 2006 at 06:27:29AM +0900, Chris Roos wrote:
> 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.

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


--
Mauricio Fernandez - http://eige... - singular Ruby

Logan Capaldo

7/21/2006 2:59:00 AM

0


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

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

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



chrisjroos@gmail.com

7/21/2006 10:03:00 AM

0

Ahhh I see. Thanks for the explanation guys.

Chris

On 7/21/06, Logan Capaldo <logancapaldo@gmail.com> wrote:
>
> On Jul 20, 2006, at 5:27 PM, Chris Roos wrote:
>
> > 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
> >
>
> 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
>
>
>
>