Recall & Review
beginner
What does the << operator do with strings in Ruby?
The << operator appends the right-hand string to the left-hand string, modifying the original string in place.
Click to reveal answer
beginner
How is string concatenation using + different from using << in Ruby?
Using + creates a new string by joining two strings, while << modifies the original string by appending to it.
Click to reveal answer
beginner
What will be the output of this Ruby code?
str = "Hello" str << " World" puts str
The output will be:
Hello WorldBecause << appends " World" to the original string str.
Click to reveal answer
intermediate
Can you chain multiple << operations in Ruby? For example: str << "a" << "b"
Yes, you can chain << operations because << returns the modified string, allowing multiple appends in one statement.
Click to reveal answer
intermediate
Why might you prefer << over + for concatenating many strings in Ruby?
Using << is more efficient because it modifies the original string without creating new string objects each time, saving memory and time.
Click to reveal answer
What does the << operator do when used with strings in Ruby?
✗ Incorrect
The << operator appends the right string to the left string and changes the original string.
What will this code output?
str = "Hi" str << "!" puts str
✗ Incorrect
The << operator appends "!" to str, so the output is "Hi!".
Which method creates a new string instead of modifying the original in Ruby?
✗ Incorrect
The + operator creates a new string by joining two strings, leaving originals unchanged.
Can you chain multiple << operations like this?
str << "a" << "b"
✗ Incorrect
The << operator returns the modified string, so chaining is allowed.
Why might << be preferred over + for many concatenations?
✗ Incorrect
<< modifies the original string in place, which is more memory efficient than +.
Explain how the << operator works for string concatenation in Ruby and how it differs from using +.
Think about whether the original string changes or not.
You got /4 concepts.
Describe a situation where using << for string concatenation is better than + in Ruby.
Consider what happens when you join many strings in a loop.
You got /4 concepts.