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
✗ 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
AHi!
BHi
C!
DError
✗ Incorrect
The << operator appends "!" to str, so the output is "Hi!".
Which method creates a new string instead of modifying the original in Ruby?
A<<
Bappend!
Cconcat!
D+
✗ Incorrect
The + operator creates a new string by joining two strings, leaving originals unchanged.
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
✗ Incorrect
The << operator returns the modified string, so chaining is allowed.
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
✗ 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.
Practice
(1/5)
1. What is the main difference between + and << when used with strings in Ruby?
easy
A. + creates a new string, << modifies the original string.
B. + modifies the original string, << creates a new string.
C. Both + and << create new strings without modifying the original.
D. Both + and << modify the original string in place.
Solution
Step 1: Understand + behavior
The + operator joins two strings but returns a new string without changing the original.
Step 2: Understand << behavior
The << operator appends text directly to the original string, modifying it in place.
Final Answer:
+ creates a new string, << modifies the original string. -> Option A
Quick Check:
+ new string, << modifies original [OK]
Hint: Remember: + new string, << changes original [OK]
Common Mistakes:
Thinking + changes the original string
Confusing << with creating a new string
Assuming both operators behave the same
2. Which of the following is the correct syntax to append " world" to the string variable greeting using << in Ruby?
easy
A. greeting.add(" world")
B. greeting + " world"
C. greeting.append(" world")
D. greeting << " world"
Solution
Step 1: Identify the correct operator for appending
In Ruby, << is used to append text to an existing string variable.
Step 2: Check syntax correctness
greeting << " world" correctly appends " world" to greeting. Other options are either invalid methods or use + which does not modify in place.
Final Answer:
greeting << " world" -> Option D
Quick Check:
Use << to append text in place [OK]
Hint: Use << to add text directly to string [OK]
Common Mistakes:
Using + which creates a new string
Trying to use non-existent methods like add
Confusing method names like append which is not standard
3. What will be the output of the following Ruby code?
str = "Hi"
str << " there"
puts str
medium
A. Hi there
B. Hi
C. ereht iH
D. Error: undefined method << for String
Solution
Step 1: Analyze the initial string and operation
The variable str starts as "Hi". The << operator appends " there" to it, modifying str to "Hi there".
Step 2: Understand the output of puts
puts str prints the string with a newline, but the output shown is the string content without the newline character visible.
Final Answer:
Hi there -> Option A
Quick Check:
Appending with << changes string to "Hi there" [OK]
Hint: Remember << adds text inside original string [OK]
Common Mistakes:
Expecting str to remain "Hi"
Confusing puts output with return value
Thinking << causes an error
4. Identify the error in the following Ruby code snippet:
text = "Hello"
text +<< " World"
puts text
medium
A. No error; output is "Hello World"
B. SyntaxError due to incorrect use of +<<
C. RuntimeError because text is immutable
D. Output is "Hello" because + does not modify original
Solution
Step 1: Check the operator usage
The code uses +<< together, which is not a valid Ruby operator or syntax.
Step 2: Understand Ruby syntax rules
Ruby does not allow chaining + and << without spaces or parentheses; this causes a syntax error.
Final Answer:
SyntaxError due to incorrect use of +<< -> Option B
Quick Check:
Invalid operator +<< causes syntax error [OK]
Hint: Operators + and << cannot be combined directly [OK]
Common Mistakes:
Assuming +<< is valid syntax
Expecting + to modify original string
Ignoring syntax errors from invalid operators
5. You want to build a sentence step by step by appending words stored in an array words = ["I", "love", "Ruby"]. Which code snippet correctly uses << to create the sentence with spaces between words?
hard
A. sentence = ""; words.each { |w| sentence + w + " " }; sentence.strip!
B. sentence = ""; words.each { |w| sentence = w + " " }; sentence.strip!
C. sentence = ""; words.each { |w| sentence << w << " " }; sentence.strip!
D. sentence = ""; words.each { |w| sentence + w << " " }; sentence.strip!
Solution
Step 1: Understand how to append words with spaces
We want to add each word and a space to the same string sentence using << which modifies in place.
Step 2: Analyze each option
sentence = ""; words.each { |w| sentence + w + " " }; sentence.strip! computes a new concatenated string but doesn't assign it back.sentence = ""; words.each { |w| sentence = w + " " }; sentence.strip! overwrites sentence with the current word + space each time.sentence = ""; words.each { |w| sentence << w << " " }; sentence.strip! correctly chains << to append the word then the space in place.sentence = ""; words.each { |w| sentence + w << " " }; sentence.strip! creates and modifies a temporary string but discards the result.
Final Answer:
sentence = ""; words.each { |w| sentence << w << " " }; sentence.strip! -> Option C
Quick Check:
Use chained << to append multiple strings [OK]
Hint: Chain << to append multiple strings efficiently [OK]
Common Mistakes:
Using + which creates new strings inside loop
Mixing + and << incorrectly
Forgetting to remove trailing space with strip!
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.