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

C

Chris Roos

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
 
D

dblack

Hi --

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.

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


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 
M

Mauricio Fernandez

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
 
L

Logan Capaldo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,970
Messages
2,570,162
Members
46,710
Latest member
bernietqt

Latest Threads

Top