Bird
Raised Fist0
Rubyprogramming~10 mins

String concatenation and << in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String concatenation and <<
Start with string s = "Hello"
↓
Use << to add " World"
↓
s now "Hello World"
↓
Use + to add "!"
↓
s + "!" returns new string "Hello World!" but s unchanged
↓
Print s and s + "!"
↓
END
Start with a string, add more text using << which changes the original string, and + which creates a new string without changing the original.
Execution Sample
Ruby
s = "Hello"
s << " World"
puts s
puts s + "!"
This code adds " World" to s using <<, then prints s and s with "!" added using +.
Execution Table
StepCode executeds valueResult/Output
1s = "Hello""Hello"No output
2s << " World""Hello World"No output
3puts s"Hello World"Hello World
4puts s + "!""Hello World"Hello World!
💡 All code executed; s was mutated by << but not by +; program ends.
Variable Tracker
VariableStartAfter Step 2After Step 4 (final)
s"Hello""Hello World""Hello World"
Key Moments - 2 Insights
Why does s change after using << but not after using +?
Because << modifies the original string s (see Step 2 in execution_table), while + creates a new string without changing s (see Step 4).
What does puts s + "!" print if s is not changed?
It prints the new string "Hello World!" created by +, but s itself remains "Hello World" (see Step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the value of s after s << " World"?
A" World"
B"Hello"
C"Hello World"
D"Hello World!"
💡 Hint
Check the 's value' column at Step 2 in execution_table.
At which step does the program output "Hello World!"?
AStep 2
BStep 4
CStep 3
DNo step outputs "Hello World!"
💡 Hint
Look at the 'Result/Output' column in execution_table for each step.
If we replaced s << " World" with s + " World", what would be the value of s after Step 2?
A"Hello"
B"Hello World"
C" World"
D"Hello World!"
💡 Hint
Refer to variable_tracker and remember + does not change s, it returns a new string.
Concept Snapshot
String concatenation in Ruby:
- << adds text to the original string (mutates it)
- + creates a new string without changing the original
- Use << to change the string in place
- Use + to combine strings without mutation
Full Transcript
This example starts with a string s set to "Hello". Using << adds " World" to s, changing s to "Hello World". When printing s, it shows "Hello World". Using + to add "!" creates a new string "Hello World!" but does not change s. Printing s + "!" shows "Hello World!" but s remains "Hello World". This shows << changes the original string, while + returns a new string without changing the original.

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

  1. Step 1: Understand + behavior

    The + operator joins two strings but returns a new string without changing the original.
  2. Step 2: Understand << behavior

    The << operator appends text directly to the original string, modifying it in place.
  3. Final Answer:

    + creates a new string, << modifies the original string. -> Option A
  4. 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

  1. Step 1: Identify the correct operator for appending

    In Ruby, << is used to append text to an existing string variable.
  2. 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.
  3. Final Answer:

    greeting << " world" -> Option D
  4. 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

  1. 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".
  2. 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.
  3. Final Answer:

    Hi there -> Option A
  4. 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

  1. Step 1: Check the operator usage

    The code uses +<< together, which is not a valid Ruby operator or syntax.
  2. Step 2: Understand Ruby syntax rules

    Ruby does not allow chaining + and << without spaces or parentheses; this causes a syntax error.
  3. Final Answer:

    SyntaxError due to incorrect use of +<< -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    sentence = ""; words.each { |w| sentence << w << " " }; sentence.strip! -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard