FAQ: The Zen of Ruby - Being Pushy

This community-built FAQ covers the “Being Pushy” exercise from the lesson “The Zen of Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Being Pushy

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi everyone!

On line 6, does anyone know why <<= is used in the code instead of just << ?

<< is used in the example and removing = from line 6 still puts the correct answer.

Thanks.

In the screenshot, <<= doesn’t serve any purpose.
<< is sufficient to concatenate the string "weezards!" to the existing string in caption.

In Ruby, strings are objects and can be changed/mutated.
If we use the concat method or the concat/shovel operator <<, we aren’t creating a new string. We are modifying/mutating the existing string.

# -- Using concat method
caption = "A giraffe surrounded by "
puts caption //-- Output: "A giraffe surrounded by "
puts caption.object_id # -- An id will be printed
caption.concat("weezards!") 
puts caption //-- Output: "A giraffe surrounded by weezards!"
puts caption.object_id # -- Same id as before
                       # -- so caption was mutated

# -- Using shovel/concatenation operator <<
caption = "A giraffe surrounded by "
puts caption # -- Output: "A giraffe surrounded by "
puts caption.object_id # -- An id will be printed
caption << "weezards!" 
puts caption # -- Output: "A giraffe surrounded by weezards!"
puts caption.object_id # -- Same id as before
                       // -- so caption was mutated

Contrast this with the += operator.

caption = "A giraffe surrounded by "
puts caption # -- Output: "A giraffe surrounded by "
puts caption.object_id # -- An id will be printed
caption += "weezards!" # -- Shorthand for caption = caption + "weezards!"
puts caption # -- Output: "A giraffe surrounded by weezards!"
puts caption.object_id # -- Different id 
             // -- so new string object was assigned to caption

concat and << mutate the string object, whereas += creates a new string,
In the screenshot above, I assume that the original code was

caption += "weezards!"

After you used the << operator, you likely forgot to remove the = from += and instead wrote <<=.
But <<= doesn’t accomplish anything different than <<. The solution code also uses <<.

Many thanks for the clear explanation mtrtmk!

The reason I didn’t remove the = from line 6 is because the exercise told us to replace .push and + with <<. As I’d never encountered << before, it wasn’t clear that the = should be removed as well. One of the many little things littered throughout this course that make it difficult for a beginner to properly understand what’s going on with the code. Thankfully there are knowledgeable people such as yourself on the forum who are willing to help :slight_smile: