The sub method replaces the first occurrence of a pattern in a string with a specified replacement.
Click to reveal answer
beginner
How is gsub different from sub in Ruby?
gsub replaces all occurrences of a pattern in a string, while sub replaces only the first occurrence.
Click to reveal answer
beginner
Example: What is the result of "hello world".sub("l", "x")?
It returns "hexlo world" because only the first l is replaced by x.
Click to reveal answer
beginner
Example: What is the result of "hello world".gsub("l", "x")?
It returns "hexxo worxd" because all l characters are replaced by x.
Click to reveal answer
intermediate
Can sub and gsub accept a block for dynamic replacement?
Yes, both methods can take a block to compute the replacement dynamically for each match.
Click to reveal answer
Which Ruby method replaces only the first match in a string?
Areplace
Bgsub
Csub
Dmatch
✗ Incorrect
sub replaces only the first occurrence, while gsub replaces all.
What will "banana".gsub("a", "o") return?
A"bonono"
B"banana"
C"bonana"
D"banano"
✗ Incorrect
All a characters are replaced by o, so the result is "bonono".
Which method can take a block to customize replacements?
Agsub only
Bboth sub and gsub
Csub only
Dneither
✗ Incorrect
Both sub and gsub can accept a block for dynamic replacement.
What does "abcabc".sub(/a/, "x") return?
A"xbcxbc"
B"xxcabc"
C"abcabc"
D"xbcabc"
✗ Incorrect
sub replaces only the first a, so the result is "xbcabc".
If you want to replace all digits in a string with "#", which method do you use?
Agsub
Bscan
Cmatch
Dsub
✗ Incorrect
gsub replaces all matches, so it is used to replace all digits.
Explain the difference between sub and gsub in Ruby.
Think about how many matches each method changes.
You got /3 concepts.
Describe how you can use a block with gsub to replace matches dynamically.
Blocks let you decide replacement based on the match.
You got /3 concepts.
Practice
(1/5)
1. What is the main difference between sub and gsub methods in Ruby?
easy
A. sub replaces all matches, gsub replaces only the first match.
B. sub replaces only the first match, gsub replaces all matches.
C. sub deletes matches, gsub adds new text.
D. sub works only on arrays, gsub works on strings.
Solution
Step 1: Understand sub behavior
sub replaces only the first occurrence of the pattern in the string.
Step 2: Understand gsub behavior
gsub replaces all occurrences of the pattern in the string.
Final Answer:
sub replaces only the first match, gsub replaces all matches. -> Option B
Quick Check:
sub = first match, gsub = all matches [OK]
Hint: Remember: sub = single replace, gsub = global replace [OK]
Common Mistakes:
Confusing which method replaces all matches
Thinking sub works on arrays
Believing gsub replaces only one match
2. Which of the following is the correct syntax to replace the first occurrence of 'cat' with 'dog' in the string "cat and cat" using sub?
easy
A. "cat and cat".substitute('cat', 'dog')
B. "cat and cat".gsub('cat', 'dog')
C. "cat and cat".replace('cat', 'dog')
D. "cat and cat".sub('cat', 'dog')
Solution
Step 1: Identify correct method for first occurrence replacement
sub replaces only the first match, so it is the correct method here.
Step 2: Check method syntax
The syntax is string.sub(pattern, replacement), so "cat and cat".sub('cat', 'dog') is correct.
Final Answer:
"cat and cat".sub('cat', 'dog') -> Option D
Quick Check:
sub syntax = string.sub(pattern, replacement) [OK]
Hint: Use sub for first match: string.sub('old', 'new') [OK]
Common Mistakes:
Using gsub instead of sub for first occurrence
Using non-existent methods like replace or substitute
Wrong method call syntax
3. What will be the output of the following Ruby code?
text = "hello hello hello"
result = text.sub('hello', 'hi')
puts result
medium
A. hi hello hello
B. hello hi hello
C. hi hi hi
D. hello hello hi
Solution
Step 1: Understand what sub does here
sub replaces only the first occurrence of 'hello' with 'hi'.
Step 2: Apply replacement to the string
The first 'hello' becomes 'hi', the rest remain unchanged, so the string becomes "hi hello hello".
Final Answer:
hi hello hello -> Option A
Quick Check:
sub replaces first match only [OK]
Hint: sub changes only first match, others stay same [OK]
Common Mistakes:
Thinking sub replaces all matches
Confusing output with gsub behavior
Misreading the string content
4. The following code is intended to replace all occurrences of 'apple' with 'orange' in the string, but it only replaces the first one. What is the error?
fruit = "apple banana apple grape"
new_fruit = fruit.sub('apple', 'orange')
puts new_fruit
medium
A. Use gsub instead of sub to replace all occurrences.
B. The string variable should be an array for replacement.
C. The replacement string must be in double quotes.
D. The method sub does not exist in Ruby.
Solution
Step 1: Identify the method used
The code uses sub, which replaces only the first match.
Step 2: Correct method for replacing all matches
To replace all occurrences, gsub should be used instead of sub.
Final Answer:
Use gsub instead of sub to replace all occurrences. -> Option A
Quick Check:
gsub replaces all matches, sub replaces first only [OK]
Hint: Use gsub for all replacements, sub for first only [OK]
Common Mistakes:
Using sub when gsub is needed
Thinking sub works on arrays
Believing method names are case sensitive differently
5. Given the string "2023-04-01 and 2023-05-01", you want to replace only the month part (the two digits after the first dash) with '12' for all dates. Which Ruby code correctly does this using gsub and a regex?
hard
A. "2023-04-01 and 2023-05-01".sub(/-(\d{2})-/, '-12-')
B. "2023-04-01 and 2023-05-01".gsub(/\d{2}-/, '12-')
C. "2023-04-01 and 2023-05-01".gsub(/-(\d{2})-/, '-12-')
D. "2023-04-01 and 2023-05-01".gsub(/-\d{2}/, '-12')
Solution
Step 1: Identify the pattern to replace
The month is the two digits between dashes, so the regex -(\d{2})- matches the dash, two digits, then dash.
Step 2: Use gsub to replace all matches
Using gsub with the pattern and replacement '-12-' replaces the month part in all dates.
Final Answer:
"2023-04-01 and 2023-05-01".gsub(/-(\d{2})-/, '-12-') -> Option C
Quick Check:
gsub with regex replaces all month parts correctly [OK]
Hint: Use gsub with regex matching month pattern for all replacements [OK]
Common Mistakes:
Using sub instead of gsub for all matches
Incorrect regex missing dashes
Replacing wrong parts of the string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.