[Ruby] Concatenating strings

Post any examples or modules that you want to share here
Post Reply
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

[Ruby] Concatenating strings

Post 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 "+".
"There lies the dog buried" (German saying translated literally)
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: [Ruby] Concatenating strings

Post by RJHollins »

Thank-You Sir.
8-)
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: [Ruby] Concatenating strings

Post by adamszabo »

Interesting, thank you!
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Ruby] Concatenating strings

Post by tulamide »

You're welcome :)
"There lies the dog buried" (German saying translated literally)
User avatar
wlangfor@uoguelph.ca
Posts: 912
Joined: Tue Apr 03, 2018 5:50 pm
Location: North Bay, Ontario, Canada
Contact:

Re: [Ruby] Concatenating strings

Post 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 :)
My youtube channel: DSPplug
My Websites: www.dspplug.com KVRaudio flowstone products
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: [Ruby] Concatenating strings

Post by Spogg »

Very interesting tulamide.

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

So many thanks…

Spogg
Post Reply