From: Mark Hubbart Date: 2004-04-24T14:48:11+09:00 Subject: Re: Great answers! A few more questions On Apr 23, 2004, at 9:29 PM, Richard Lionheart wrote: > Hi Ara, > > Great answers. BTW, I had another typo in my original post, which I > corrected below. I've got a couple more questions, but if you don't > feel > like spending any more time on this question, please ignore this post. > >> join ';' > > That's a lot better than my procedural code. Works great > >> myA1 = MyArrayType['x1', 'y1'] > > (Q1) That worked, but where the heck did you come up with that? Is > that > documented somewhere? Since MyArrayType inherits from Array, you get all it's methods for free. One of those is Array[], which creates a new array from the arguments passed. Since you call it using MyArrayType rather than Array, it creates an instance of MyArrayType. It's documented in PickAxe with Array's other class methods. >> Object#puts behaves different if passed an Array, or sub-class of >> Array > > In my case, I think I was passing puts the the last expression > executed in > the to_s method of an Array-subclass object. So I really wasn't > passing an > Array nor a sub-class of Array. > (Q2) Do you agree? Correct. You were passing it a String. I think Ara was just making a related observation about how Kernel#puts works. > Below are several variants of code for this question. > (Q3) My last question is how can I make the MyArrayType3 invocation > work? Three reasons it won't work properly: - You are calling MyArrayType3(data), rather than the appropriate MyArrayType3[data] (note the square brackets). - Your initialize method is spelled wrong - You aren't calling MyArrayType3.new, which would call the initialize function. You are calling MyArrayType3() which is interpreted to be a method of Object; ie., Object#MyArrayType3() - You are assigning the array data to an instance variable, then joining it for to_s. What you have there is an empty MyArrayType instance with an instance variable that's holding an array. This will give you wrong behavior if you do anything with that instance other than call to_s: other method calls will use the non-existant internal array data. To make it work properly, you might do it like this: class MyArrayType3 < Array def initialize(ary) self.concat! ary end def to_s join "; " end end > > MyArrayType1 uses my original code (absent the typo) and is invoked > with an > array of explicitly quoted strings as you suggest. Works great. > > MyArrayType2 uses your join expresson and is invoked with an array of > explicitly quoted strings as you suggest. Works great. > > MyArrayType3 uses your join expresson but adds an initialize method so > it > can be invoked with an array object argument. Fails. > (Q4) Why? > > Again, thanks for your great response. > > Regards, > Richard > > > class MyArrayType1 < Array > def to_s > s = "" > self.each { |x| > s += "; " if s.length>0 > s += x.to_s > } > s > end > end > > class MyArrayType2 < Array > def to_s > join '; ' > end > end > > class MyArrayType3 < Array > def to_s > sOut = @s.join '; ' > end > def intialize(aIn) > @s = aIn > end > end > > myA1 = MyArrayType1['x1', 'y1'] > puts myA1.to_s # x1; y1 > > myA2 = MyArrayType2['x1', 'y1'] > puts myA2.to_s # x1; y1 > > a3 = %w[x1, y1] > myA3 = MyArrayType(a3) # undefined method `MyArrayType' for > main:Object > (NoMethodError) > puts myA3.to_s > > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.665 / Virus Database: 428 - Release Date: 4/22/2004 > > >