------ art_52043_6073773.1163081939183 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 11/9/06, Daniel N <has.sox / gmail.com> wrote: > > On 11/9/06, Robert Dober <robert.dober / gmail.com> wrote: > > > > On 11/9/06, spooq <spoooq / gmail.com> 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} "he, nice, guys".split(",") > tmp1 <-- ["he", " nice", " guys" ] tmp1.map!{|x| x.strip} > tmp2 <-- ["he", "nice", "guys"] ** and modifies the object referenced by tmp1 in place *** > 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. The idea was to work on the differences between xxx and xxx! 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;) Excellent idea. 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 which will get lost, the only thing you use is the result of the expression, s Total Arrays created 2 I have no idea ;) How does this not differ from > "he, nice, guys".split(',').map{|x| x.strip} "he, nice, guys".split(",") > tmp1 <-- ["he", " nice", " guys" ] tmp1.map!{|x| x.strip} > tmp2 <-- ["he", "nice", "guys"] *** without modifiying tmp1 inplace *** Performance is not an issue, but I guess it is important to understand why one would apply map! (ignoring the existance of map would not be a good reason) 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 exactly (this is done too above, it would not work else) Total Arrays created 3 Again I have no idea ;) 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 No for the same reason as above why use x.strip! modifiying x when x will be discarded immediately? I thaught this would be the ice breaker example :( Have I understood the difference/similarities here or have I missed the > ball? Baseball? BTW Sometimes I get caught in the urge to explain, forgetting that experience has shown to me that I am quite a bad teacher :( Cheers Robert -- The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man. - George Bernard Shaw ------ art_52043_6073773.1163081939183--