From: Fabian Streitel Date: 2009-09-23T01:46:28+09:00 Subject: Re: Getting to one attribute of my marshalled array of objects --00504502d057c6e53804742d56ac Content-Type: text/plain; charset=ISO-8859-1 > > > Your to_s method would end up returning an array of Car objects and it > > might > > have been pure accident that it looked ok in your command loop. > > Hmmm...ok, I am confused on this one. I thought the whole point of > .each was to iterate through the elements of arrays and hashes and > perform actions on each one...like a for or while loop without the fuss > and muss of counters, etc. > That is correct. But unlike a C-style for or while loop, a call to #each() does have a return value. And again unlike C, a function always returns its last value, e.g: def foo 5 end foo() # returns 5 And since #each() returns an array and the call to #each() was the last thing in the #to_s() method, #to_s() would just return what #each() returned -- an array. What you wanted was probably #inject(). It's a very powerful method that let's you do all kinds of neat things. See [1] and [2] Greetz! [1]: http://www.ruby-doc.org/core/classes/Enumerable.html#M003140 [2]: http://blog.jayfields.com/2008/03/ruby-inject.html --00504502d057c6e53804742d56ac--