No site found for codelikethis.herokuapp.com; using CodeLikeThis content.

 Slides

String Messages

A string understands lots of messages. Here are a few:

"banana".upcase
"Cherry".downcase
"titanic".capitalize
"elderberry".reverse
"fig".length
"Fig Newton".swapcase
"".empty?
"syzygy".length

Try all of these out in irb!

String Operators

A string knows DOT, but also understands several other operators:

"blue" + "berry"
"yum" * 10
"elderberry"[8]

+ * and [] are pronounced PLUS, TIMES, and SUB

Try these out in irb!

Combining Messages and Operators

You can combine messages and operators at will.

"fig".upcase.reverse
"grape".reverse * 10 + "!!!"

Definitely try these out in irb! It's pretty fun.

LAB: Playing With Strings

  • What is the reverse of "stressed"?
  • How many characters long is your name?
  • What does your name look like, repeated 1000 times?
  • What is the tenth character of "Matz is nice"? (Trick question!)

Interpolation

first = "Joe"
last = "Smith"

+ does concatenation

full = first + " " + last

#{} does interpolation

full = "#{first} #{last}"