Your first Ruby
What Ruby is, printing output three ways, numbers that behave, and the string interpolation you will use in every program after this one.
A language written for people
Ruby was designed with an explicit goal: to make programmers happy. It reads like English where it can, it lets you write the same thought in more than one shape, and its author's first rule was that the computer should be the one doing the boring work. That philosophy is why Ruby powers so much of the web's tooling and why Rails became a genre of its own.
The sandbox here is the real thing — CRuby, the same interpreter that runs on a server, compiled to WebAssembly and running in this page. The first Run downloads that runtime, which takes a few seconds; every Run after it is immediate. One honest limitation: there is nowhere for a program to read input from, so lessons print rather than ask.
Three ways to say something
puts prints a line. print prints without the line break, so the next thing continues on the same line. And p prints the value in its raw form — quotes around strings, brackets around arrays — which is why it is the tool you reach for while working something out.
A comment starts with # and runs to the end of the line. Ruby has no end-of-statement punctuation: a newline ends the thought, which is the first sign of a language that trusts you to mean what you wrote.
Numbers, and a string with a hole in it
Arithmetic is the arithmetic you expect — +, -, /, and a star for multiplication — with one rule that surprises everyone for a minute: dividing two integers gives an integer, so 10 / 4 is 2. Make either side a float and you get 2.5, and 2 ** 10 is a thousand and twenty-four. Integers are not limited to machine size, so raising two to the hundredth is a perfectly ordinary answer.
A string in double quotes may contain #{...}, and the little piece of Ruby inside is computed and pasted in. That is interpolation, and it is how Ruby programs build text — "Hello, #{name}!" — instead of gluing pieces together with plus signs. Single-quoted strings are taken literally: no interpolation, no surprises.
The interpreter reads downward
Ruby evaluates a file from the top down, one expression at a time. Each line is asked for its value, even when nothing is done with it. puts is a method call like any other; it writes its argument and answers nil, which is why you call it for the writing rather than for the answer.
The absence of punctuation is not the absence of structure. A newline ends an expression, end closes a block, and a method call may leave its parentheses off when the meaning is clear: puts 'hello' and puts('hello') are the same call. Learning which words are methods and which are names is most of learning to read Ruby, and the optional brackets stop being a puzzle once you see them that way.
Three mistakes cover most first days. = assigns and == compares, so if count = 3 sets the count and is always true. Interpolation only happens in double quotes: 'Hello, #{name}' prints the characters themselves. And integer division drops the remainder, so 10 / 4 is 2; write 10.0 / 4 when you want the fraction.
What the output pane shows
Ruby writes to standard output, and the pane under the editor is a terminal as far as the program is concerned. puts adds a newline after its argument; print does not. That single difference explains the most common first puzzle, where two values appear jammed together as onetwo because both were printed without a break.
The pane also collects warnings and errors. A warning is Ruby telling you something is suspicious — a constant reassigned, a variable shadowed — while the program carries on. An error stops the program and prints a backtrace, which is a list of the calls that led to the failure, newest first. Read the first line of a backtrace before the rest; it names the file and line where the failure happened.
The three prints, and text with a hole in it
puts 'Hello, Ruby!'
# print leaves the cursor where it is.
print 'one '
print 'two'
puts ''
p 'a string'
p [1, 2, 3]
name = 'Ada'
puts "Hello, #{name}!"
puts "Two plus two is #{2 + 2}"
# Division of integers stays whole.
puts 10 / 4
puts 10.0 / 4
puts 2 ** 10Run it, then make it yours
Press Run and read the output line by line. Then change the name, add a line that prints a sum of your own, and try both kinds of quotes around a string that contains a name.
When output surprises you, reach for p. It shows the value itself rather than the friendly form puts would print — so a number that arrived as text shows its quotes, and a missing value shows as nil.
Try these next
- Print a name, a price and a count with one
puts, interpolating each value. - Write the same greeting with single quotes and double quotes, and compare the output.
- Compute a bill with integer division and with a float, and explain the difference in a comment.
A conversion card
Run it and follow each line. Then change the starting temperature, and add a line that converts Fahrenheit back to Celsius with the same formula in reverse.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What is the difference between puts and print?
