String concatenation.. + vs <<

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.

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.

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

Thanks Matt! It is the single quotes which I’m about to convert back to double quotes!!

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.

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