This site is a preview of the curriculum for our Summer 2018 bootcamp in Burlington, Vermont. As we continue our preparation, courses and lessons will appear and disappear; we will rename, rearrange, clarify, and obfuscate as needed... Please consider this a work in progress and keep checking in.
Learn to Code with Ruby:
A VARIABLE is a NAME for an object. You give an object a name using the ASSIGNMENT operator (it looks like an equal sign).
color = "blue" fruit = "berry"
Anywhere you can use an object, you can use a variable instead.
color + fruit fruit.upcase
The Warehouse Metaphor
Think of memory as a giant warehouse.
The Warehouse Metaphor Explained
If memory is a giant warehouse...
...and objects are boxes in that warehouse
...then a value is the contents of a box
...and a variable is a label you stick on the outside of the box
Variables are documentation
Which is clearer, this:
60 * 60 * 24
seconds_per_minute = 60 minutes_per_hour = 60 hours_per_day = 24 seconds_per_day = seconds_per_minute * minutes_per_hour * hours_per_day
Lab: Play In IRB
Let's spend a few minutes just playing around in IRB. Some things to try:
- write a poem
- YELL THE POEM
- calculate 2 + 2 and more complicated things
- assign your best friend to a variable
- reverse your best friend's name
- get a new best friend and reverse her too
The Pointer Metaphor
snack = "Apple"
Think of a variable as pointing to an object.
You can assign and reassign variables at will.
color = "blue" fruit = "berry" color + fruit color = "black" color + fruit
Changing a variable (using ASSIGNMENT) just changes the name of an object. It does not change the data inside the object.
This is analogous to a label being removed from one box and placed on a different box.
Many pointers can point to the same thing
fruit = "Apple" snack = fruit
After this both
- are pointing to the same object
- have the same value
This is analogous to two labels being placed on the same box.
Return values are new
most messages return new values
fruit = "banana" snack = fruit.upcase
"BANANA" are two different objects in memory
Most messages do not change the data inside the object.
But some messages do change the data!
This can be dangerous so sometimes those messages end with a BANG (exclamation point).
- variables are names for objects
- you can change which object a variable points to
- you can have many names for the same object
- sometimes values can change inside an object but the names (variables) stay the same