0Pricing
Ruby Academy · Lesson

Substitution

gsub with patterns.

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

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 })

Frequently asked questions

Is the “Substitution” lesson free?

Yes — the full text of “Substitution” 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 “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; 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 “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