Last Updated: February 25, 2016
·
2.177K
· mangar

Concatenating String in Ruby....

The complete question and answer is here:
http://stackoverflow.com/questions/377768/string-concatenation-and-ruby/11802633#11802633

My point is:

Let me show to you all my experience with that.

I had an query that returned 32k of records, for each record I called a method to format that database record into a formated string and than concatenate that into a String that at the end of all this process wil turn into a file in disk.

My problem was that by the record goes, around 24k, the process of concatenating the String turned on a pain.

I was doing that using the regular '+' operator.

When I changed to the '<<' was like magic. Was really fast.

So, I remembered my old times - sort of 1998 - when I was using Java and concatenating String using '+' and changed from String to StringBuffer (and now we, Java developer have the StringBuilder).

I believe that the process of + / << in Ruby world is the same as + / StringBuilder.append in the Java world.

The first reallocate the entire object in memory and the other just point to a new address.