From: "Jesús Gabriel y Galán" Date: 2010-09-11T06:11:04+09:00 Subject: Re: How to do foo.chomp!.rstrip!.downcase! ? On Fri, Sep 10, 2010 at 8:49 PM, Jason Roelofs wrote: > On Sep 10, 2010, at 1:29 PM, Jesús Gabriel y Galán wrote: > >> On Fri, Sep 10, 2010 at 6:26 PM, Jason Roelofs wrote: >>> On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote: >>> >>>> >>>> >>>> >>>>> >>>>> ...it seems like I should be able to do: >>>>> >>>>> foo.chomp!.rstrip!.downcase! >>>>> >>>> >>>> you can. >>>> >>>> %w( chomp! rstrip! downcase! ).each{|m| foo.send(m)} >>>> >>>> -a >>>> >>> >>> So... what's wrong with: >>> >>> foo = foo.chomp.rstrip.downcase >> >> Each call creates a copy of the string, and are all discarded along >> with the original except the last one. >> >> Jesus. >> > > I don't see any problem there, that's expected. Ruby has a GC, and those will get cleaned up appropriately. > > The ! versions don't work chained, this version does. You shouldn't be worrying about the cost of creating 3 strings. I know, regarding that, I don't see any problem either with foo.chomp! foo.rstrip! foo.downcase! Robert commented on this already (although his email was lost in some problem with SpamAssassin, but he resent): Robert Klemme wrote: > ... which usually is less efficient. I guess OP had a reason to > use bang versions. and I clarified. If it's three strings, no problem. If this is in a loop you might want to reduce garbage. Just pointing out a fact about the non-bang methods. Jesus.