Searching and Replacing
gsub, sub, match.
Searching and Replacing is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
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(",", " | ")Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Searching and Replacing” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Searching and Replacing”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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