← Home · Ruby skill tree

An interactive Ruby lesson

Strings

A String holds text. Build on Values and Variables to put values into text and work with the result.

Read code from top to bottom. p(...) displays an inspected value, with quotes around strings. puts(...) prints a string's text without those quotes, then starts a new line. It can also print a number.

Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.

The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.

1. Write text in quotes

Matching double quotes or single quotes create a string. Predict how p and puts display the same text.

Show expected output
"Ruby"
"Ruby"
Ruby

Try it: Change Ruby to Hello! on each line. Keep the matching quotes.

2. Put a variable into text

Inside a double-quoted string, #{name} inserts the value of name as text. This is called interpolation. Single quotes keep #{name} as literal text. Predict both lines.

Show expected output
Hello, Ana!
Hello, #{name}!

Try it: Change name to "Leo". Predict which output line changes.

3. Calculate inside interpolation

An expression produces a value. Ruby evaluates the expression inside #{...} and inserts its result as text. Predict the number in the message.

Show expected output
Total: 15 coins

Try it: Change ticket_count to 4. Predict the new message before running.

4. Join strings with +

For two strings, + joins their text into a new string. It adds no spaces of its own. Predict how the explicit " " changes the greeting.

Show expected output
HelloAna
Hello Ana

Try it: Replace " " with ", ". Predict the punctuation and spacing.

5. Count characters with length

A method is an operation called on a value. The dot in "Ruby".length calls the length method on that string. It returns a character count as an integer. Count the spaces too. "" is an empty string.

Show expected output
4
10
0

Try it: Add ! after rocks. Predict which count changes.

6. Change letter case

upcase returns a new string with uppercase letters. downcase returns one with lowercase letters. Predict those results and the original text on the last line.

Show expected output
RUBY
ruby
RuBy

Try it: Assign "Hello" to word. Predict all three lines before running.

Practice on your own

Replace the code in the last editor with a program for these tasks. Predict the output, then run it.

  1. Assign "Mina" to name.
  2. Use interpolation to assign "Hello, Mina!" to greeting, inserting the value of name.
  3. Use puts to display greeting, then its uppercase form.
  4. Display the length of name.
Show one solution and its output
name = "Mina"
greeting = "Hello, #{name}!"
puts(greeting)
puts(greeting.upcase)
puts(name.length)

Output:

Hello, Mina!
HELLO, MINA!
4

Ruby documentation

See the Ruby 4.0 string literal documentation for quotes and interpolation, and the String documentation for +, length, upcase, and downcase.

Return to the Ruby skill tree to choose another topic.