Forum: Ruby on Rails String concatenation + vs <<

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d04aa1009aa052cd3e71839a565e251?d=identicon&s=25 Brandon (Guest)
on 2014-03-16 05:48
(Received via mailing list)
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.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2014-03-16 15:23
(Received via mailing list)
On Sunday, 16 March 2014 00:47:45 UTC-4, Brandon wrote:
>
>
String interpolation only works with double (") not single (') quotes.


> 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.
>

If you're concatenating a LOT of strings, << is definitely preferable -
a +
b + c + d generates three intermediate String objects which need to be
GCed, while a << b << c << d doesn't.

--Matt Jones
5d04aa1009aa052cd3e71839a565e251?d=identicon&s=25 Brandon (Guest)
on 2014-03-16 16:02
(Received via mailing list)
Thanks Matt! It is the single quotes which I'm about to convert back to
double quotes!!
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-03-16 16:33
(Received via mailing list)
On 16 March 2014 14:21, Matt Jones <al2o3cr@gmail.com> wrote:
>>
>> Strangely inside this method, I can't seem to do string interpolation and
>> it prints '@order.seller.name'. So that is a strange issue.
>>
>
> String interpolation only works with double (") not single (') quotes.

But since @order.seller.name is not inside quotes that does not
explain why @order.seller.name is not interpreted.

Colin
This topic is locked and can not be replied to.