An interactive Ruby lesson
Values
Ruby works with values such as 7, "hello", and true. You can start here without any programming background.
Read code from top to bottom. Each example uses p(...) to display a value on its own line. The parentheses enclose what you want to display.
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. The code and answers remain readable without JavaScript.
1. Calculate with numbers
Predict each line. + adds, - subtracts, and * multiplies. Ruby evaluates the calculation before p prints its result.
Show expected output
9 5 14
Try it: change 7 to 10 on each line. Predict all three results before you run again.
2. Divide integers and floats
7 is an Integer, a whole number. 7.0 is a Float, a floating-point number written here with a decimal point. / divides. Predict which results include a fraction.
Show expected output
3 3.5 3.5
Dividing two integers rounds the result down to an integer. If either number is a float, these divisions produce a float.
Try it: replace each 7 with 9, keeping the decimal points. Then try -7 / 2. Rounding down gives -4.
3. Compare text and numbers
A String holds text. Quotes make "2" a string. 2 is a number. Predict what + does in each calculation.
Show expected output
"2" 2 "23" 5
p keeps quotes visible when it displays a string. For strings, + joins the text. For numbers, it adds.
Try it: change the strings to "Ruby" and "!". Keep the quotes.
4. Ask for the class of a value
A class tells you what kind of value you have. Add .class to ask a value for its class. The dot calls an operation, called a method, on that value.
A Symbol is often used as a name or label. :ready writes a symbol. Predict how its class differs from "ready".
Show expected output
Integer Float String Symbol :ready
Try it: replace both uses of :ready with :waiting. Does the class change?
5. Recognize true, false, and nil
true and false are boolean values. Comparisons produce booleans: > means “greater than,” and == asks whether two values are equal.
nil is a separate value often used for something missing or unknown. Predict the comparisons, then check the class of each special value.
Show expected output
true false nil TrueClass FalseClass NilClass
"2" == 2 gives false: a string and an integer are different kinds of value. Ruby uses separate classes for true and false.
Try it: change 3 > 2 to 3 > 5, then compare nil == false. Both comparisons give false.
6. Inspect values in a mixed example
p displays an inspected form of a value, which keeps strings, symbols, and nil visible. puts prints text without the surrounding quotes. Predict each line before running.
Show expected output
hello "hello" :hello 2 Float false
The extra parentheses in (8.0 / 2).class ask for the class of the division result.
Try it: change 8.0 to 8. Predict the new class before pressing Run.
Practice on your own
Replace the code in the last editor with one line for each task. Predict the output, then run your program.
- Display the result of dividing
9by2as integers. - Change one number to a float and display the new result.
- Join the strings
"9"and"2", then display the result. - Display the class of
:done. - Display whether
nilequalsfalse.
Show one solution and its output
p(9 / 2)
p(9.0 / 2)
p("9" + "2")
p(:done.class)
p(nil == false)Output:
4 4.5 "92" Symbol false
Ruby documentation
- Literals: numbers, strings, symbols, booleans, and nil
- Integer division
- Inspecting values with p and asking for a value's class
Return to the Ruby skill tree to see the planned foundations that build on values.