Ruby Academy · Lesson

Substitution

gsub with patterns.

Lesson 3 of 413 steps

Substitution 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.

Replacing Text with sub

String#sub replaces the first match of a pattern with a replacement string. The original string is not changed; a new one is returned.

s = 'hello world'
puts(s.sub(/world/, 'Ruby'))
puts s

Replacing All with gsub

gsub means global substitution. It replaces every match, not just the first.

puts('a-b-c-d'.gsub(/-/, '_'))
puts('one two two'.gsub(/two/, 'three'))

Patterns in Substitution

The first argument to gsub can be a full regex, letting you match flexible patterns instead of fixed text.

puts('id42 id7 id100'.gsub(/\d+/, '#'))
puts('a1b2c3'.gsub(/[0-9]/, '*'))

Backreferences in Replacement

In the replacement string you can refer to captured groups with \1, \2, and so on.

puts('2026-05-30'.gsub(/(\d{4})-(\d{2})-(\d{2})/, '\3/\2/\1'))

The Whole Match Backreference

\0 or \& refers to the entire matched text, useful for wrapping matches.

puts('cat dog'.gsub(/\w+/, '[\0]'))

Block Form of gsub

gsub accepts a block. Each match is passed in, and the block's return value becomes the replacement. This lets you compute replacements dynamically.

result = 'a1 b2 c3'.gsub(/\d/) { |d| (d.to_i * 10).to_s }
puts result

Uppercasing Matches

The block form makes transformations easy. Here we capitalize each word.

title = 'the great escape'.gsub(/\w+/) { |w| w.capitalize }
puts title

Hash Replacement

If you pass a Hash as the second argument, each match is looked up as a key and replaced by its value. Great for translation maps.

map = { 'cat' => 'kedi', 'dog' => 'kopek' }
puts('cat and dog'.gsub(/cat|dog/, map))

In-Place Versions

sub! and gsub! modify the string in place and return nil if no change was made.

s = 'hello'
s.gsub!(/l/, 'L')
puts s
puts(s.gsub!(/z/, 'x').inspect)

Removing Text

To delete matches, replace them with an empty string.

puts('h e l l o'.gsub(/\s/, ''))
puts('price: $42'.gsub(/[^0-9]/, ''))

Named Group Backreferences

You can name groups with (?<name>...) and reference them in the replacement using \k<name>.

out = 'John Smith'.gsub(/(?<first>\w+) (?<last>\w+)/, '\k<last>, \k<first>')
puts out

Quick Check

What is the difference between sub and gsub?

Recap: Substitution

You learned to transform strings with patterns:

  • sub first match, gsub all matches
  • Backreferences \1, \& in replacements
  • Block form computes replacements dynamically
  • Hash argument maps matches to values
  • sub! / gsub! mutate in place
puts('foo bar baz'.gsub(/b\w+/) { |w| w.upcase })
Free to start

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 “Substitution” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Substitution”, 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 “Substitution”?

gsub with patterns. 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 “Substitution” 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

  1. Regex Literals
  2. Matching and Capturing
  3. Substitution
  4. Named Groups and Anchors
← Back to Ruby Academy