Struct derivation

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:

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

X.new # => ArgumentError: wrong number of arguments (0 for 1)

snip >>>

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

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

class MyStruct < Struct
def self.new() Struct.new(‘Foo’,:bar) end

optionally define initialize and call it in new()'s result

?> end
=> nil

Foo=MyStruct.new
=> Struct::Foo
f=Foo.new
=> #
f.bar=10
=> 10

···

il 26 May 2004 23:49:03 GMT, Philipp Kern trash@philkern.de ha scritto::

Philipp Kern trash@philkern.de wrote in message news:slrncbab7l.2n5.trash@o2.net.philkern.de

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:

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

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

···

On 2004-05-27, Alan Chen aero6dof@yahoo.com wrote:

“Philipp Kern” trash@philkern.de schrieb im Newsbeitrag
news:slrncbbbls.adl.trash@o2.net.philkern.de

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)

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 = Struct.new(“X”, :foo, :bar)
=> Struct::X
x
=> Struct::X
Struct.constants
=> [“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

class X < Struct.new(:foo,:bar)
def initialize; end
end
=> nil
X
=> X
X.ancestors
=> [X, #Class:0x101865f0, Struct, Enumerable, Object, Kernel]

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
:

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

On 2004-05-27, Alan Chen aero6dof@yahoo.com wrote: