# String concatetenation + vs \<\<

**URL:** <https://www.ruby-forum.com/t/string-concatetenation-vs/239830>\
**Category:** Rails\
**Created:** [March 16, 2014, 5:48am UTC](https://www.ruby-forum.com/t/string-concatetenation-vs/239830 "2014-03-16T05:48:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [March 16, 2014, 5:48am UTC](https://www.ruby-forum.com/t/string-concatetenation-vs/239830/1 "2014-03-16T05:48:13Z")

</div>

So I have the following:

```
flash[:success] = 'Your payment has completed. Please contact ' +
                         @order.seller.name + ' (mobile: ' +
                         @order.seller.mobile_number + ', email: ' +
                         @order.seller.email + ')'

```

Strangely inside this method, I can’t seem to do string interpolation  
and  
it prints ‘@order.seller.name’. So that is a strange issue.

But the main thing that puzzles me is should I be replacing + with \<\<  
here?  
I read somewhere the performance is better but I really hate seeing \<\<  
in  
my code. It just seems ugly and raises my blood pressure for some  
reason.

---

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [March 16, 2014, 9:55am UTC](https://www.ruby-forum.com/t/string-concatetenation-vs/239830/2 "2014-03-16T09:55:54Z")

</div>

On 16 March 2014 04:46, Brandon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But the main thing that puzzles me is should I be replacing + with \<\< here?  
> I read somewhere the performance is better but I really hate seeing \<\< in my  
> code. It just seems ugly and raises my blood pressure for some reason.

I think this would be much more readable  
flash[:success] = “Your payment has completed. Please contact  
#{@order.seller.name} (mobile: #{@order.seller.mobile\_number}, email:  
#{@order.seller.email} )”

Colin

---

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [March 18, 2014, 11:10am UTC](https://www.ruby-forum.com/t/string-concatetenation-vs/239830/3 "2014-03-18T11:10:25Z")

</div>

El domingo, 16 de marzo de 2014 09:54:06 UTC+1, Colin L. escribió:

> and it  
> #{@order.seller.name} (mobile: #{@order.seller.mobile\_number}, email:  
> #{@order.seller.email} )"
> 
> Colin

That s really odd, I think that writing like: flash[:success] = “Your  
payment has completed. Please contact #{@order.seller.name} (mobile:  
#{@order.seller.mobile\_number}, email:  
#{@order.seller.email} )” should interpolate it.  
The only rule I know is that double quotes are the ones to use in string  
interpolation.  
I hope you found the solution.  
And please if you find it share it.

---

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [March 19, 2014, 2:19pm UTC](https://www.ruby-forum.com/t/string-concatetenation-vs/239830/4 "2014-03-19T14:19:00Z")

</div>

The operator _\<\<_ is better to add info into a variable.  
For example:

name = “”  
name.object\_id  
=\> 70341910843080

name \<\< "Jhon "  
name.object\_id  
=\> 70341910843080

name \<\< "Doe "  
name.object\_id  
=\> 70341910843080

Note that the object\_id is always the same.  
It’s not create a new object.

_Now, let see this:_  
name = “”  
name.object\_id  
=\> 703419105_94080_

name += “John”  
name.object\_id  
=\> 703419105_49640_

name += " Doe"  
name.object\_id  
=\> 703419105_14060_

Note that always a new object will be created.

But if you wanna put a String with interpolation into a variable, how  
this:  
flash[:success] = “Your payment has completed. Please contact #{@  
order.seller.name} (mobile: #{@order.seller.mobile\_number}, email:  
#{@order.seller.email} )”

I’ll do the same way that you, because I lost just a little performance,  
but my code would be cleaner.

Or you prefer:  
flash[:success] = "Your payment has completed. Please contact "  
flash[:success] \<\< @order.seller.name  
flash[:success] \<\< "(mobile: "  
flash[:success] \<\< @order.seller.mobile\_number  
flash[:success] \<\< ", email: "  
flash[:success] \<\< @order.seller.email  
flash[:success] \<\< “)”

flash[:success]

rsrs… Ruby is clean man. Your interpolation is the better way.  
flash[:success] = “Your payment has completed. Please contact #{@  
order.seller.name} (mobile: #{@order.seller.mobile\_number}, email:  
#{@order.seller.email} )”
