Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

[Ruby] Concatenating strings

Post any examples or modules that you want to share here

[Ruby] Concatenating strings

Postby tulamide » Sat May 11, 2019 1:11 am

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Ruby] Concatenating strings

Postby RJHollins » Sat May 11, 2019 3:47 am

Thank-You Sir.
8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: [Ruby] Concatenating strings

Postby adamszabo » Sat May 11, 2019 10:20 am

Interesting, thank you!
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: [Ruby] Concatenating strings

Postby tulamide » Sat May 11, 2019 4:42 pm

You're welcome :)
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Ruby] Concatenating strings

Postby wlangfor@uoguelph.ca » Sat May 11, 2019 7:11 pm

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
wlangfor@uoguelph.ca
 
Posts: 912
Joined: Tue Apr 03, 2018 5:50 pm
Location: North Bay, Ontario, Canada

Re: [Ruby] Concatenating strings

Postby Spogg » Sun May 12, 2019 7:53 am

Very interesting tulamide.

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

So many thanks…

Spogg
User avatar
Spogg
 
Posts: 3358
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England


Return to User Examples

Who is online

Users browsing this forum: No registered users and 7 guests