Image for post
Image for post
Photo by Todd Diemer

Easy Ruby: Plus-Equals (+=) vs. Shovel (<<)

Understanding how plus-equals and shovel affect strings in Ruby

Adam Lombard
Dec 24, 2016 · 4 min read

Confusion at the North Pole

If we say…

santa = 'Saint'claus = santasanta += ' Nick'

… then santa will yield the string ‘Saint Nick’, while claus will yield the string ‘Saint’.

Makes sense, right? Changing santa doesn’t affect claus.

Now let’s ruin Christmas.

If we say…

santa = 'Saint'claus = santasanta << ' Nick'

…then santa will yield the string ‘Saint Nick’, while claus will also yield the string ‘Saint Nick’.

WTF, Santa?!

Simple Explanation

  1. Variables point to objects
  2. The = (assignment operator) affects variables
  3. The << (append operator) affects objects

Detailed Explanation

  • Saint

But this object is largely useless to us, since we can’t reference it in a consistent way.

To do so, we must initialize a variable to point to it. We do this using the assignment operator, =. In Ruby, a variable is initialized the first time an object is assigned to it:

santa = 'Saint'

The code above says, “Computer, I need you to store an object for me — the string ‘Saint’. I may need to use this object later, or even change it a little! Things could get crazy! But, no matter how it changes, I want to make sure you and I are always talking about the same object. So, let’s have a codeword for the address in memory where ‘Saint’ is stored. Let’s use the codeword santa. Go team!"

So, if we say…

santa = ‘Saint’santa = ‘Saint Nick’

… we are not changing the ‘Saint’ object by adding ‘ Nick’ to it. We’re storing a different object at a different address in memory, and re-pointing the santa variable to this other address.

We aren’t changing the object, even if we concatenate and re-assign. It’s still a re-assignment of the variable:

santa = 'Saint'santa = santa + ' Nick'

Which means we aren’t changing the object, even if we use some fancy syntactic sugar (or, in Santa’s case, syntactic sugarplums) to concatenate and re-assign :

santa = 'Saint'santa += ' Nick'

The object didn’t change. The variable changed.

But, what if we want the opposite — to keep the variable pointing to the same spot in memory, but change the string located there?

(Hint: the append operator (<<) is about to come through in the clutch and save Christmas.)

santa = 'Saint'santa << ' Nick'

Here we are saying “Computer, please create a string object ‘Saint’, store it at some address, and we’ll talk about that address later using the codeword santa." Then, using the <<, we are saying, “Computer, remember that object you are saving for me at the santa address? Please add ‘ Nick’ to the end of that object. Thanks, computer!”

The object changes, while the variable does not. The variable continues to point to the same address in memory.

This means something quite subtle when writing Ruby.

If we say…

santa = 'Saint'claus = santasanta << ‘ Nick’

… we will have…

  • santa yielding 'Saint Nick', and
  • claus also yielding ‘Saint Nick’!

We pointed santa to a string object, and then pointed claus to santa. Since santa is just a reference to a memory address, claus is also a reference to the same address, and thus to the exact same object modified by the statement santa << ‘ Nick’.

To bring the concept full circle, if we say…

santa = 'Saint'claus = santasanta += ' Nick'

… we will have…

  • santa yielding 'Saint Nick', and
  • claus still yielding just ‘Saint’, because although santa and claus both pointed to the same address when claus was initialized, the += re-assigned santa to a different address.

The << affects objects, while += affects variables.


A note for future Rubyists :


Was this helpful? Likes, shares, and comments are appreciated!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store