×

ruby · level 2 · unit 3 · lesson 1

  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

cuts a string into an array. join glues it back.

Lots of real data arrives as one long string, like "eggs,milk,bread". .split(",") cuts it wherever it finds the , here a comma, and hands back an array of the pieces.

.split with nothing in the brackets cuts at spaces and skips any extra ones, which is perfect for the words in a sentence.

.join goes the other way. It glues an array's items into one string, with whatever you give it placed between them.

line = "eggs,milk,bread"
items = line.split(",")
p items
puts items.join(" and ")
words = "  the quick   brown fox ".split
p words
puts words.map { |word| word.capitalize }.join(" ")

.capitalize makes the first letter a capital and the rest small.