String methods in Ruby
The everyday string toolkit: strip, split, sub and friends.
line = " the well-grounded rubyist "
puts line.strip
puts line.strip.split.first
title = "ruby under a microscope"
puts title.capitalize
puts title.split.map(&:capitalize).join(" ")
puts title.sub("microscope", "lens")
puts title.start_with?("ruby")
puts title.tr("aeiou", "*")
puts title.center(30, "-")
How it works
striptrims whitespace;splitbreaks a line into words.map(&:capitalize).jointitle-cases a phrase word by word.subreplaces the first match;trswaps characters wholesale.centerpads both sides — handy for quick console banners.
Keywords and builtins used here
puts
The run, in numbers
- Lines
- 12
- Characters to type
- 308
- Tokens
- 88
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 59 seconds.
Step 2 of 3 in Variables & strings, step 2 of 29 in Language basics.