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 K. 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.
you can.
with the original except the last one.
foo.rstrip!
loop you might want to reduce garbage. Just pointing out a fact about
the non-bang methods.
Jesus.
Yeah, looking at the source code for ruby 1.9 reveals that
String.chomp is essentially implemented as String.sup.chomp!, so
you’re absolutely right about the non bang versions creating some
extra garbage. The same is true about (rstrip and downcase as well.)
How this affects the overall performance is really hard to say without
benchmarking though. But as Robert claimed
So… what’s wrong with:
The ! versions don’t work chained, this version does. You shouldn’t be worrying about the cost of creating 3 strings.
Robert K. wrote:
String.chomp is essentially implemented as String.sup.chomp!, so
/lasso
Argh…it was supposed to read “implemented as String.dup.chomp!”
/lasso
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.