ruby · level 1 · unit 3 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Text and numbers look alike, but Ruby treats them differently.
5 is a number. "5" is a string that happens to hold the digit 5. Whole numbers like 5 are called integers. Ruby can do maths with integers, but not with strings.
With strings, + joins: "5" + "3" makes "53". With integers, + adds: 5 + 3 makes 8. Mix them, like "5" + 3, and Ruby stops with a .
Not sure which kind a value is? Every value has a , which says what kind of thing it is. Add .class to find out: 5.class is and "5".class is String.
puts "5" + "3"
puts 5 + 3
puts 5.class
puts "5".class Quotes are the clue: with quotes it is text, without quotes it is a number.
guess first
Apples and pears. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
apples = "4"
pears = "2"
puts apples + pears
your turn
Your turn. Count the pets in your street.
Type how many cats and how many dogs live in your street, as numbers with no quotes. Run it to see them added up and to see their class.
fix it
Two goals plus four should be six. Fix it!
- Read the error. It is a TypeError on line 2. What kind of value is in goals?
"2"has quotes, so it is a string. Ruby cannot add a string and an integer.- Remove the quotes on line 1 so it says goals = 2, then press run.
nailed it
you can tell text and numbers apart. :D
- Numbers have no quotes. Strings do.
+adds integers but joins strings..classtells you what kind of value you have.
new words in your collection
- integer
- class
- TypeError
This lesson isn’t saved. A free account remembers every lesson you finish.