ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* subclassing Structs
@ 2003-08-13 15:16 Eugene Scripnik
  2003-08-13 16:53 ` Yukihiro Matsumoto
  0 siblings, 1 reply; 5+ messages in thread
From: Eugene Scripnik @ 2003-08-13 15:16 UTC (permalink / raw
  To: ruby-core

I'm trying to create class which behaves as struct (almost) and has some 
other useful methods. But subclassing Struct seems to work differently 
from other builtin classes. Here is the sample:

class SubStruct < Struct
     def initialize
         super( nil, :name, :surname )
         puts 'in initialize'
     end
end

class SubArray < Array
     def initialize
         super( 10 ) { 'default value' }
     end
end

a = SubArray.new
p a.size
p a[5]

s = SubStruct.new

SubArray works as I expected but SubStruct fails in new method. Is this 
a bug or I should reorganize my code?

-- 
Eugene Scripnik
IT Group
Software Architect
Tel./Fax +380 (372) 58-43-10
email: Eugene.Scripnik@itgrp.net
http://www.itgrp.net/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: subclassing Structs
  2003-08-13 15:16 subclassing Structs Eugene Scripnik
@ 2003-08-13 16:53 ` Yukihiro Matsumoto
  2003-08-14  9:57   ` Eugene Scripnik
  0 siblings, 1 reply; 5+ messages in thread
From: Yukihiro Matsumoto @ 2003-08-13 16:53 UTC (permalink / raw
  To: ruby-core

Hi,

In message "subclassing Structs"
    on 03/08/14, Eugene Scripnik <Eugene.Scripnik@itgrp.net> writes:

|I'm trying to create class which behaves as struct (almost) and has some 
|other useful methods. But subclassing Struct seems to work differently 
|from other builtin classes. Here is the sample:

That's because Struct class is a abstract factory for each Struct
(e.g. Struct.new does not return instance of Struct, but subclass of
it).  I'm not sure what you want to achieve by subclassing, though.

							matz.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: subclassing Structs
  2003-08-13 16:53 ` Yukihiro Matsumoto
@ 2003-08-14  9:57   ` Eugene Scripnik
  2003-08-14 17:04     ` Yukihiro Matsumoto
  0 siblings, 1 reply; 5+ messages in thread
From: Eugene Scripnik @ 2003-08-14  9:57 UTC (permalink / raw
  To: ruby-core

Yukihiro Matsumoto wrote:

> |I'm trying to create class which behaves as struct (almost) and has some 
> |other useful methods. But subclassing Struct seems to work differently 
> |from other builtin classes. Here is the sample:
> 
> That's because Struct class is a abstract factory for each Struct
> (e.g. Struct.new does not return instance of Struct, but subclass of
> it).  I'm not sure what you want to achieve by subclassing, though.
My example didn't show that but I wanted to create struct-class with 
dynamic attributes and additional constructors.

class SubStruct < Struct
   def new2
     new( 'attr1_value', 'attr2_value' )
   end
end

new2 - instance method of SubStruct (Struct) and I thought it has to be 
class method of SubStruct.new( ... ) class.

-- 
Eugene Scripnik
IT Group
Software Architect
Tel./Fax +380 (372) 58-43-10
email: Eugene.Scripnik@itgrp.net
http://www.itgrp.net/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: subclassing Structs
  2003-08-14  9:57   ` Eugene Scripnik
@ 2003-08-14 17:04     ` Yukihiro Matsumoto
  2003-08-15 10:35       ` Eugene Scripnik
  0 siblings, 1 reply; 5+ messages in thread
From: Yukihiro Matsumoto @ 2003-08-14 17:04 UTC (permalink / raw
  To: ruby-core

Hi,

In message "Re: subclassing Structs"
    on 03/08/14, Eugene Scripnik <Eugene.Scripnik@itgrp.net> writes:

|My example didn't show that but I wanted to create struct-class with 
|dynamic attributes and additional constructors.
|
|class SubStruct < Struct
|   def new2
|     new( 'attr1_value', 'attr2_value' )
|   end
|end
|
|new2 - instance method of SubStruct (Struct) and I thought it has to be 
|class method of SubStruct.new( ... ) class.

Unlike other classes, Struct.new returns its subclass, not instance.
This is very exceptional behavior.

new2 is an instance method of SubStruct.

  class SubStruct < Struct
    def new2
       new(:foo, :bar)
    end
  end

  C = MyStruct.new(:foo)
  p C.ancestors         # => [C, MyStruct, Struct, Enumerable, Object, Kernel]
  p C.new.new2          # "new2" here.  but not "new"

							matz.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: subclassing Structs
  2003-08-14 17:04     ` Yukihiro Matsumoto
@ 2003-08-15 10:35       ` Eugene Scripnik
  0 siblings, 0 replies; 5+ messages in thread
From: Eugene Scripnik @ 2003-08-15 10:35 UTC (permalink / raw
  To: ruby-core

Yukihiro Matsumoto wrote:
> Hi,
> 
> In message "Re: subclassing Structs"
>     on 03/08/14, Eugene Scripnik <Eugene.Scripnik@itgrp.net> writes:
> 
> |My example didn't show that but I wanted to create struct-class with 
> |dynamic attributes and additional constructors.
> |
> |class SubStruct < Struct
> |   def new2
> |     new( 'attr1_value', 'attr2_value' )
> |   end
> |end
> |
> |new2 - instance method of SubStruct (Struct) and I thought it has to be 
> |class method of SubStruct.new( ... ) class.
> 
> Unlike other classes, Struct.new returns its subclass, not instance.
> This is very exceptional behavior.
> 
> new2 is an instance method of SubStruct.
> 
>   class SubStruct < Struct
>     def new2
>        new(:foo, :bar)
>     end
>   end
> 
>   C = MyStruct.new(:foo)
>   p C.ancestors         # => [C, MyStruct, Struct, Enumerable, Object, Kernel]
>   p C.new.new2          # "new2" here.  but not "new"
OK, thank you for explanations. Problem solved by changing def new2 to 
def self.new2

-- 
Eugene Scripnik
IT Group
Software Architect
Tel./Fax +380 (372) 58-43-10
email: Eugene.Scripnik@itgrp.net
http://www.itgrp.net/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-08-15 10:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-08-13 15:16 subclassing Structs Eugene Scripnik
2003-08-13 16:53 ` Yukihiro Matsumoto
2003-08-14  9:57   ` Eugene Scripnik
2003-08-14 17:04     ` Yukihiro Matsumoto
2003-08-15 10:35       ` Eugene Scripnik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).