From: Brian Candler Date: 2003-04-25T00:34:48+09:00 Subject: Re: How to call an object instance's method? On Fri, Apr 25, 2003 at 12:10:15AM +0900, Rene Tschirley wrote: > b.funB > > funB fills the list 'aList' with some instances of A: ... > if @aList.empty? > # in fact, i check if array size is big enough and > # do the append if it doesn't > @aList = @aList + Array.new(1, A) > end That's not what you've done; you have filled aList with the *class* A. Try instead: @aList = @aList + [A.new] or more Rubyesque, @aList << A.new and everything is fine. Useful debugging tip: try using 'p' in your code. e.g. @aList << A.new p @aList This is more or less the same as puts @aList.inspect With your version the output would be [A] <<<<< element is the class A with the modified version you will get something like: [#] <<<<< an instance of A > puts @aList.type ? Array > > I understand, so far. > > puts (@aList).type ? A > > This is confusing. Why the difference? Did I miss some special meaning of > "()"? Yes... it's the "poetry mode" ambiguity, and a bit nasty. Basically if you put an open bracket after a method call, it thinks you are bracketing the method parameters, and the rest of the expression is outside. So what you have there is: ( puts (@aList) ).type i.e. you are printing the array @aList itself, not its type. The return value of puts is 'nil', and you are then getting the type of it (which is NilClass), and then throwing that information away. Use the -w flag to Ruby: #!/usr/local/bin/ruby -w or ruby -w and a lot of this sort of bug will become apparent much more easily. I think Matz has hinted in the past that perhaps -w should be mandatory, and you would have a different flag to turn it off. I think I agree :-) Regards, Brian.