message "A variable is a way to name a piece of data so we can do things with
it. It can hold all kinds of things - words, numbers, the result of a function,
and even a function itself."
goal "Understand what a variable is for"
goal "Store data in variables"
goal "Replace data in an existing variable"
message "Start up irb on the Virtual Machine."
irb "my_variable = 5"
message "This creates a new variable called `my_variable` and stores the value 5 in it."
message "Let's confirm that. To see what value a variable holds, type its name."
irb "another_variable = \"hi\""
message "This creates another variable and stores the value \"hi\" in it."
irb "my_variable = 10"
message "`my_variable` has a new value."
apples = 5
bananas = 10 + 5
fruits = 2 + apples + bananas
bananas = fruits - apples
message "Variables are assigned a value using a single equals sign (=)."
message "The right side of the equals sign is evaluated, and the result is
assigned to the variable named on the left side of the equals."
message "You've created a lot of variables. Let's see the list."
result "[:bananas, :fruits, :apples, :another_variable, :my_variable]"
message "Here Ruby gives you a list of variables you've defined in irb so
far. This might be useful for when you want to see what variable names are
already in use."
The opposite of a variable is `constant` - a value that doesn't change.
Numbers, strings, nil, true, and false are constants.
Check out what happens if you assign a value to a constant."
true == false
1 == 1
"a" == 1
message "(Notice in the last example we put the a in quotation marks to make it a string.)"
message "There are some rules about what you can name a variable."
Try making variables with the following kinds of names in irb:
* all letters (like 'folders')
* all numbers (like '2000')
* an underscore (like 'first_name')
* a dash (like 'last-name')
* a number anywhere (like 'y2k')
* a number at the start (like '101dalmations')
* a number at the end (like 'starwars2')
Which worked? Which didn't?
message "Variables can hold the result of a function."
irb 'shouting = "hey, you!".upcase'
message "Variables allow you to store data so you can refer to it by name
later. The data you store in variables will persist as long as your program
keeps running and vanish when it stops."