From: Glen Holcomb Date: 2008-08-01T05:39:50+09:00 Subject: Re: Needing help with which to_s gets called ------=_Part_108542_6661930.1217536841408 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Thu, Jul 31, 2008 at 2:35 PM, Glen Holcomb wrote: > On Thu, Jul 31, 2008 at 2:31 PM, John Winters wrote: > > > Sebastian Hungerecker wrote: > > > John Winters wrote: > > >> Why when Able is a sub-class of Array does my to_s method get ignored? > > > > > > Because puts special cases arrays, not calling to_s on them at all. > > > If you compare puts [1,2,3] to puts [1,2,3].to_s you will see > > > that this is the case (puts [1,2,3] is just like puts 1,2,3 printing > > > each number on its own line instead of having it all in one line like > > > you get with to_s. > > > > Thanks for that. There certainly seems to be some sort of > > special-casing going on, but it's more widespread than just puts. > > > > I tried the test case which you suggested and see exactly what you said. > > However I also went back to my original code and changed the "puts" line > > from: > > > > puts item > > > > to: > > > > output = item.to_s > > puts output > > > > and I still get the same oddity so it's clearly not just puts doing it. > > > > Do you have a reference where I can read up more on this? > > > > TIA, > > John > > -- > > Posted via http://www.ruby-forum.com/. > > > > > output is an instance of Baker so Ruby will invoke Baker's to_s method. > > -- > "Hey brother Christian with your high and mighty errand, Your actions speak > so loud, I can't hear a word you're saying." > > -Greg Graffin (Bad Religion) > Actually now that I think about it a little more it appears that in the first case something a little odd is happening in that Ruby is just treating the Able data as a normal array but as soon as you assign the data to an instance variable is adds an instance of Able to the array rather than just passing the array values through. -- "Hey brother Christian with your high and mighty errand, Your actions speak so loud, I can't hear a word you're saying." -Greg Graffin (Bad Religion) ------=_Part_108542_6661930.1217536841408--