Searching and Replacing
gsub, sub, match.
Searching and Replacing is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Finding a Substring
The index method returns the position of the first match, or nil if not found. include? returns true or false.
text = "hello world"
puts text.index("world")
puts text.include?("world")Replacing the First Match with sub
sub replaces only the first occurrence of a pattern with a new value.
text = "cat cat cat"
puts text.sub("cat", "dog")Replacing All Matches with gsub
gsub means global substitution. It replaces every occurrence.
text = "cat cat cat"
puts text.gsub("cat", "dog")Using Regular Expressions
Patterns between slashes like /.../ are regular expressions. /\d/ matches a digit. gsub and sub accept regex patterns.
text = "a1b2c3"
puts text.gsub(/\d/, "*")Matching with match?
match? returns true if a regex matches anywhere in the string. It is fast and clean for checks.
email = "ada@ruby.org"
puts email.match?(/@/)
puts "no at sign".match?(/@/)Capturing with match
match returns a MatchData object. Index [0] is the whole match; parentheses create capture groups accessed by [1], [2] and so on.
data = "2024-05-30"
m = data.match(/(\d+)-(\d+)-(\d+)/)
puts m[1]
puts m[2]scan for All Matches
scan returns an array of every match in the string. Perfect for extracting all numbers or words.
text = "phone 555 and 777"
puts text.scan(/\d+/).inspectAnchors
Anchors pin a match to a position. ^ means start of line, $ means end. \A and \z pin to the whole string edges.
puts "hello".match?(/^hel/)
puts "hello".match?(/llo$/)
puts "hello".match?(/^world/)Character Classes
Square brackets define a set of allowed characters. [aeiou] matches any vowel. [a-z] matches any lowercase letter.
text = "Ruby"
puts text.gsub(/[aeiou]/i, "_")Blocks in gsub
Pass a block to gsub to transform each match dynamically. The matched text is the block argument.
text = "one two"
result = text.gsub(/\w+/) { |w| w.upcase }
puts resultCase-Insensitive Search
The i flag after a regex makes it ignore letter case.
text = "Hello HELLO hello"
puts text.gsub(/hello/i, "hi")Quick Check
Check your search-and-replace skills.
Recap
You can now search and replace text:
index,include?,match?to findsub(first) andgsub(all) to replace- regex patterns, anchors, character classes
scanto extract all matches and blocks for dynamic replacement
Next: splitting and joining strings.
puts "a,b,c".gsub(",", " | ")Frequently asked questions
Is the “Searching and Replacing” lesson free?
Yes — the full text of “Searching and Replacing” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Searching and Replacing”?
gsub, sub, match. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Searching and Replacing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String Basics and Methods
- Interpolation and Formatting
- Searching and Replacing
- Splitting and Joining