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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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 <<.
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