How to Replace Text in a String in Ruby: Simple Guide
In Ruby, you can replace text in a string using the
String#sub method to replace the first occurrence or String#gsub to replace all occurrences. Both methods return a new string with replacements, while their bang versions sub! and gsub! modify the original string in place.Syntax
The main methods to replace text in Ruby strings are:
string.sub(pattern, replacement): replaces the first match ofpatternwithreplacement.string.gsub(pattern, replacement): replaces all matches ofpatternwithreplacement.sub!andgsub!are the same but change the original string directly.
ruby
string.sub(pattern, replacement) string.gsub(pattern, replacement) string.sub!(pattern, replacement) string.gsub!(pattern, replacement)
Example
This example shows how to replace the first and all occurrences of a word in a string using sub and gsub. It also shows how to modify the original string with gsub!.
ruby
text = "I like apples. Apples are tasty." # Replace first occurrence (case sensitive) new_text = text.sub("apples", "oranges") puts new_text # Replace all occurrences (case sensitive) new_text_all = text.gsub("apples", "oranges") puts new_text_all # Replace all occurrences ignoring case new_text_ignore_case = text.gsub(/apples/i, "oranges") puts new_text_ignore_case # Modify original string text.gsub!("apples", "oranges") puts text
Output
I like oranges. Apples are tasty.
I like oranges. Apples are tasty.
I like oranges. Oranges are tasty.
I like oranges. Oranges are tasty.
Common Pitfalls
Common mistakes when replacing strings in Ruby include:
- Using
subwhen you want to replace all occurrences instead of just the first. - Not using regular expressions when you want case-insensitive replacements.
- Expecting
suborgsubto modify the original string without using the bang versionssub!orgsub!.
ruby
text = "Hello hello hello" # Wrong: only replaces first occurrence puts text.sub("hello", "hi") # Right: replace all occurrences ignoring case puts text.gsub(/hello/i, "hi") # Wrong: expecting original string changed text.sub("Hello", "Hi") puts text # Right: modify original string text.sub!("Hello", "Hi") puts text
Output
Hello hi hello
hi hi hi
Hello hello hello
Hi hello hello
Quick Reference
| Method | Description | Modifies Original String? |
|---|---|---|
| sub(pattern, replacement) | Replace first match, returns new string | No |
| gsub(pattern, replacement) | Replace all matches, returns new string | No |
| sub!(pattern, replacement) | Replace first match, modifies original string | Yes |
| gsub!(pattern, replacement) | Replace all matches, modifies original string | Yes |
Key Takeaways
Use
sub to replace the first occurrence and gsub to replace all occurrences in a string.Add
! to modify the original string instead of returning a new one.Use regular expressions with
i flag for case-insensitive replacements.Remember
sub and gsub return new strings and do not change the original unless you use sub! or gsub!.Test your replacement to avoid unexpected results with case sensitivity or partial matches.