Struct derivation

P

Philipp Kern

Hi Ruby folks.

It's impossible to me to inherit a class from Struct when the class does not
share its constructor with the Struct superclass.
Struct implements .new on its own, instead of properly implementing it by
initialize, so the following is impossible:
class X < Struct
def initialize; super:)foo, :bar); end
end

X.new # => ArgumentError: wrong number of arguments (0 for 1)
I think it should be implemented the normal Ruby way, Struct.new should inherit
from Class.new like all the other objects so that proper inheritation gets
possible.

Any comments on this? Or is this even intended behaviour?

Bye,
phil
 
G

gabriele renzi

Hi Ruby folks.

It's impossible to me to inherit a class from Struct when the class does not
share its constructor with the Struct superclass.
Struct implements .new on its own, instead of properly implementing it by
initialize, so the following is impossible:

then do the same thing :)?> end
=> nil=> 10
 
A

Alan Chen

Philipp Kern said:
Hi Ruby folks.

It's impossible to me to inherit a class from Struct when the class does not
share its constructor with the Struct superclass.
Struct implements .new on its own, instead of properly implementing it by
initialize, so the following is impossible:

class X < Struct
def initialize; super:)foo, :bar); end
end

I could be wrong, but I think you actually want:

class X < Struct.new:)foo,:bar)
def initialize; end
end

Or are you trying to create a new class X which can build new classes like struct?
 
P

Philipp Kern

I could be wrong, but I think you actually want:
class X < Struct.new:)foo,:bar)
def initialize; end
end

You're right. However it should be written class X < Struct.new("X", :foo, :bar)
or something like that. Like this you could get around the warnings Ruby issues
when you redefine the class:
(irb):6: warning: already initialized constant X
Or are you trying to create a new class X which can build new classes like
struct?

No, thank you Alan. That's what I searched. I misunderstood Struct a bit |:

Bye,
phil
 
R

Robert Klemme

Philipp Kern said:
You're right. However it should be written class X < Struct.new("X",
:foo, :bar)

I'd rather don't do that if you're just interested in inheriting from this
struct. Because Struct.new("X", :foo, :bar) creates a named struct:

12:09:03 [ContentReporter_BRANCH]: irbs=> ["X", "Tms"]
or something like that. Like this you could get around the warnings Ruby issues
when you redefine the class:
(irb):6: warning: already initialized constant X

Which code gives you that warning? I guess you did too much testing in
IRB and thus redefined the constant. It works perfectly for me:

12:07:42 [ContentReporter_BRANCH]: irbs
No, thank you Alan. That's what I searched. I misunderstood Struct a bit
|:

This can happen too easily. Maybe they should've created another method
instead of overloading #new. That can be really confusing in this
situation.

Regards

robert
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top