From: Daniel N Date: 2006-11-09T21:55:20+09:00 Subject: Re: thousand ways to rome ------=_Part_35608_16677805.1163076916054 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 11/9/06, Robert Dober wrote: > > On 11/9/06, spooq wrote: > > > > use map! instead > > > nope, > try to understand the difference between split(regexp), map and map! > than decide for yourself > Look at this for example > > "he, nice, guys".split(',').map!{|x| x.strip} > why would you use map! ? > try to put the above expression into a context > e.g. > x = ... > > Hint: using map! on unreferenced objects is quite useless. Robert, I don't really understand what you mean. My understanding is that these do different things. Can I walk through this in pseudo pseudo code to increase my understanding;) split the string into an unref'ed array take the unreffed array and strip each element in place, creating a new string at each element in the original array Total Arrays created 2 How does this not differ from "he, nice, guys".split(',').map{|x| x.strip} Where, my understanding would be split the string into an unref'ed array take the unref'ed array, and create a new array from the result of each element stripped. ie. a new string object for each element put into a new array Total Arrays created 3 What about > x= "he, nice".split(",") > x.map!{|x|x.strip} split the string and assign it to an array modify each element in place and strip the result. (creating a new string for each element) try > x.map!{|x|x.strip!} > would you like to use this? Nasty... Turns the first element (with no whitespace) into nil Have I understood the difference/similarities here or have I missed the ball? ------=_Part_35608_16677805.1163076916054--