Page 1 of 1

[Ruby] Concatenating strings

Posted: Sat May 11, 2019 1:11 am
by tulamide
I thought sharing this little insight wouldnt hurt.

You may know that you can concatenate strings with a plus sign.

Code: Select all

"were" + "wolf" # => "werewolf"


But there's another way, using the sign, you also use to add objects to an array: <<

Code: Select all

"were" << "wolf" # => "werewolf"


At first glance there seems to be no difference. But there is a big one. Let's expand the example.

Code: Select all

s1 = "were"
s2 = "wolf"
s3 = s1 + s2 # => "werewolf"

Code: Select all

s1 = "were"
s2 = "wolf"
s3 = s1 << s2 # => "werewolf"


You still see no difference, but you will, as soon as you inspect s1

Code: Select all

s1 = "were"
s2 = "wolf"
s3 = s1 + s2 # => "werewolf"
s1 # => "were"

Code: Select all

s1 = "were"
s2 = "wolf"
s3 = s1 << s2 # => "werewolf"
s1 # => "werewolf"

:shock:
So what exactly has happened? s1 seems to be the same as s3. Well, if you do an equality check, the result is true.

Code: Select all

s3.equal?(s1) # => true

#equal? is comparable to what other languages might know as comparing the pointers of both variables. They both point to the very same object! s3 and s1 now are one object, referenced two times! How did this happen.

When you concatenate string with the plus sign, behind the scenes a new string is created and s1 and s2 put in there. Then the resulting string object is referenced by s3. But << works in place! It actually extends the object it visually points to. So, s1 << s2 is actually an instruction. Ruby will extend s1 by the content of s2. At that point s1 already reads "werewolf". Then it is referenced by s3. The following code would have had the same effect.

Code: Select all

s1 = "were"
s2 = "wolf"
s1 << s2 # => "werewolf"
s3 = s1 # => "werewolf"


This is possible because Ruby allows instructions to be executed right where their result would be referenced.

Code: Select all

n = 0
s = "were" << n == 1 ? "wolf" : "cat" # => "werecat"

n = rand(2)
s = "were" << case n
  when 0
    "goose"
  when 1
    "wolf"
  when 2
    "cat"
  end


So, when to use "+" and when to use "<<"? Everytime you just want to extend an existing string, it saves you some memory and time to use "<<" (not much, but if you do that with some hundred strings, say, the paths to samples on the harddrive, it is already worth it). When you're constructing a new string, use "+".

Re: [Ruby] Concatenating strings

Posted: Sat May 11, 2019 3:47 am
by RJHollins
Thank-You Sir.
8-)

Re: [Ruby] Concatenating strings

Posted: Sat May 11, 2019 10:20 am
by adamszabo
Interesting, thank you!

Re: [Ruby] Concatenating strings

Posted: Sat May 11, 2019 4:42 pm
by tulamide
You're welcome :)

Re: [Ruby] Concatenating strings

Posted: Sat May 11, 2019 7:11 pm
by wlangfor@uoguelph.ca
I'd read about this in the tutorial but never gave it much thought because
I was working on a different style of project.

Printed the page as a pdf thanks :)

Re: [Ruby] Concatenating strings

Posted: Sun May 12, 2019 7:53 am
by Spogg
Very interesting tulamide.

I do find your occasional mini-tutorials really useful, and so well explained.

So many thanks…

Spogg