Forum: Ruby on Rails String concatetenation + vs <<

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.
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-03-16 09:55
(Received via mailing list)
On 16 March 2014 04:46, Brandon <wongwf82@gmail.com> 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
B2ea0545dc3b54e877f4a36be4fcde89?d=identicon&s=25 Gustavo Caso (Guest)
on 2014-03-18 11:10
(Received via mailing list)
El domingo, 16 de marzo de 2014 09:54:06 UTC+1, Colin Law 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.
C33311e3f2c1f2bae39a4c5736db6ee9?d=identicon&s=25 Rafael Belo (Guest)
on 2014-03-19 14:19
(Received via mailing list)
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} )"
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.