×

ruby · level 1 · unit 3 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Methods are tricks that every string knows.

A is a command that belongs to a value. Write the value, a dot, then the method's name. You met one already: .chomp. Here are some more.

counts every letter, space and symbol. .upcase shouts in capitals and .downcase whispers in small letters. .strip trims spaces off both ends.

.sub swaps the first match for new text, so "banana".sub("a", "o") gives bonana. swaps every match, giving bonono. The box stays the same unless you store the result.

word = "  Hello  "
puts word.strip
puts word.strip.length
puts word.upcase
puts "banana".gsub("a", "o")

You can chain methods: word.strip.length trims first, then counts what is left.