0
0
Rubyprogramming~5 mins

String concatenation and << in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 World
Because << 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?
ARemoves the last character from the string
BCreates a new string by joining two strings without modifying originals
CAppends the right string to the left string, modifying the original string
DConverts the string to uppercase
What will this code output?
str = "Hi"
str << "!"
puts str
AHi!
BHi
C!
DError
Which method creates a new string instead of modifying the original in Ruby?
A<<
Bappend!
Cconcat!
D+
Can you chain multiple << operations like this?
str << "a" << "b"
AYes, because << returns the modified string
BNo, it causes an error
COnly if str is an array
DOnly if str is frozen
Why might << be preferred over + for many concatenations?
ABecause + is faster
BBecause << modifies the original string, saving memory
CBecause + modifies the original string
DBecause << creates new strings each time
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.