# String Concatenation and Implicit Object Creation

**URL:** <https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603>\
**Category:** Ruby\
**Created:** [February 20, 2008, 12:31am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603 "2008-02-20T00:31:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 12:31am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/1 "2008-02-20T00:31:17Z")

</div>

Do adjacent strings or strings separated by backslash and a newline  
create multiple String objects?

For example:

s1 = ‘asdf’ ‘qwer’

s2 = ‘asdf’  
‘qwer’

I don’t know how to figure this out, but I ran benchmarks and compared  
against ‘\<\<’ and ‘+’ and it looks like each example above only creates  
a single String object. What’s really happening?

-pachl

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 12:48am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/2 "2008-02-20T00:48:19Z")

</div>

pachl wrote:

> I don’t know how to figure this out, but I ran benchmarks and compared  
> against ‘\<\<’ and ‘+’ and it looks like each example above only creates  
> a single String object. What’s really happening?
> 
> -pachl

The string concatenation method is “+”

s1 = “asdf” + “qwer”

This creates 3 string objects, “asdf”, “qwer”, and “asdfqwer”.

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 1:39am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/3 "2008-02-20T01:39:22Z")

</div>

On Feb 19, 4:45 pm, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This creates 3 string objects, “asdf”, “qwer”, and “asdfqwer”.  
> Of course it does, that’s not my question.

Anyway, going off your reply, how many strings does the following  
create?

s1 = ‘asdf’ ‘qwer’

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 1:48am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/4 "2008-02-20T01:48:12Z")

</div>

On Feb 19, 2008 4:35 PM, pachl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [snip] how many strings does the following create?
> 
> s1 = ‘asdf’ ‘qwer’

One. Adjacent strings are concatenated by the parser.

~ j.

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 2:05am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/5 "2008-02-20T02:05:11Z")

</div>

pachl wrote:

> Of course it does, that’s not my question.
> 
> Anyway, going off your reply, how many strings does the following  
> create?
> 
> s1 = ‘asdf’ ‘qwer’

Son-of-a-gun! Ruby _does_ concatenate adjacent string literals. I learn  
something every day.

My new copy of _The\_Ruby\_Programming\_Language_ says that Ruby  
concatenates adjacent string literals during the parsing process. That  
makes me think that there’s only 1 string created, “asdfqwer”.

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 5:00am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/6 "2008-02-20T05:00:30Z")

</div>

On Feb 19, 6:26 pm, botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> s1 = ‘asdf’ ‘qwer’ s2 # this one errs, ruby expect all literals  
> s1 = ‘asdf’ ‘qwer’ + s2  
> from :0
> 
> s1 = (‘asdf’ ‘qwer’) + s2  
> #=\> “asdfqwerasdfqwer”

Thanks everyone. This answered my question. It makes total sense that  
the parser would take care of this adjacent string concatenation.

I was creating long SQL strings and to make the code readable, I was  
splitting the query string among multiple lines and joining them using  
the ‘\<\<’ operator.

sql = 'select \* from table ’ \<\<  
'where id=3 ’ \<\<  
‘order by id’

Now I can go refactor my code and avoid the unnecessary overhead of  
string appending/concatenating.

-pachl

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [February 20, 2008, 2:27am UTC](https://www.ruby-forum.com/t/string-concatenation-and-implicit-object-creation/129603/7 "2008-02-20T02:27:28Z")

</div>

From: pachl [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Do adjacent strings or strings separated by backslash and a newline

# create multiple String objects?

iianm, no. you create just one string literal, ergo one string object.  
it’s part of ruby syntax on string literals. very useful if you want  
to emphasize a string part, similar to numeric literals where you can  
put underscore to separate some digits…

# For example:

s1 = ‘asdf’ ‘qwer’  
#=\> “asdfqwer” # here string literal processing continues

s2 = ‘asdf’  
‘qwer’  
#=\> “asdfqwer” # here too

s1 = ‘asdf’ ‘qwer’ “#{s2}” # still inline works  
#=\> “asdfqwerasdfqwer”

s1 = ‘asdf’ ‘qwer’ s2 # this one errs, ruby expect all literals  
SyntaxError: compile error  
(irb):13: syntax error, unexpected tIDENTIFIER, expecting $end  
from (irb):13  
from :0

s1 = ‘asdf’ + ‘qwer’ + s2  
#=\> “asdfqwerasdfqwer”

it may look like concat op,

s1 = ‘asdf’ ‘qwer’ + s2  
#=\> “asdfqwerasdfqwer”

but…

s1 = ‘asdf’ (‘qwer’ + s2)  
SyntaxError: compile error  
(irb):17: syntax error, unexpected ‘(’, expecting $end  
s1 = ‘asdf’ (‘qwer’ + s2)  
^  
from (irb):17  
from :0

s1 = (‘asdf’ ‘qwer’) + s2  
#=\> “asdfqwerasdfqwer”

kind regards -botp
