From: Robert Klemme Date: 2004-04-24T18:39:05+09:00 Subject: Re: Problem assigning an Array object to an Array-subclass object "Richard Lionheart" schrieb im Newsbeitrag news:iLSdne99T7Y0rhTdRVn-jw@comcast.com... > [ I apologize if this is a second post. My earlier one seems to have gotten > lost in the ethernet :-) ] > > notes =< I didn't like to_s' output for an Array of strings. > Array#to_s simply concatenated them > or inserted a comma separator. > So I wrote my own. > But as the last line indicates, I failed. > > Apparently, array assignment to myA1 > changed the reference from a MyArrayType object > to an Array object. > > Two questions: > 1. How can I set myA1 to an array value > and preserve it's type? > 2. Isn't there a "Ruby way" to set the separator > (I thought there was some special symbol a la Perl > but I couldn't find any in Thomas' or Fulton's > books.) > NOTES > > class MyArrayType < Array > def to_s > print "Starting MyArrayType#to_s" > s = "" > s.each { |x| s += "; " if s.length>0; s += x.to_s } > s > end > end > > myA1 = MyArrayType.new > myA1 = %w[x1, y1] There seems to be a misunderstanding on your side about variables, objects and types. Variables do not have a type, they just hold references to any object. So your code does not declare a variable "myA1" of type MyArrayType, it merely first assigns the result of MyArrayType.new (a new instance of class MyArrayType) and then assigns the result of %w[x1, y1] to myA1, loosing the reference to the MyArrayType instance. Simply use Array#join: irb(main):014:0> %w[x1, x2].join( '; ' ) => "x1,; x2" Note: the ',' is part of the first word. You probably wanted irb(main):015:0> %w[x1 x2].join( '; ' ) => "x1; x2" Regards robert