From: "Ara.T.Howard" Date: 2004-04-24T14:14:06+09:00 Subject: Re: Great answers! A few more questions On Sat, 24 Apr 2004, 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? I've got The Pragmatic Programmer/Thomas and The Ruby > Way/Fulton and didn't notice that in either work (though I certainly have > not read every word of either one.) frankly, i find that book amazing. it got me into ruby and yet i still find things in it almost weekly. to answer your question, it's in 'Built-in Classes and Methods'->'Array'->'[]' the html version of the book is very useful. note that '[]' is simply an alias for Array#new, which in turn will construct an Array object and call #initialize on it. > > 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? yes. just a warning. > Below are several variants of code for this question. > (Q3) My last question is how can I make the MyArrayType3 invocation work? myA3 = MyArrayType(a3) # undefined method `MyArrayType' for main:Object ^ ^ ^ ^ 3.new ^^^^^ ^^^^^ myA3 = MyArrayType3.new(a3) # undefined method `MyArrayType' for main:Object ^^^^^ typo? ;-) perhaps something like this (un-tested)? class MyArrayType < Array def initialize(*args, &block) if Array === args.first ary = args.shift super update ary else super end end def to_s;join '; ';end end again, be very careful extending/inheriting builtin classes - it's powerful because you get so much for free, but it can come back to bite you when you expect it to be just __like__ to built-ins, eg: ma = MyArrayType['42'] a = ['forty-two'] ma + a # => this will be an Array! ma << 42.0 # => MyArrayType a << 42.0 # => Array eg. alot of the methods you inherit will not return objects of your specialized class, but of the parent class (Array in this case). this isn't too much of a problem unless you expect these object to have you new methods (to_s for instance)... an aggregate class is often safer and less frustrating to debug... > 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? see above. code looks correct, just a typo. > Again, thanks for your great response. no worries, it's just karma - i've gotten plenty of great responses myself... > 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 cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================