Intro to Variables

Their like Nicknames & Labels

Variables are like Nicknames

NickName Photo joshua-coleman-ZcsfQbsCq5c-unsplash.jpg

My name is Joe but I can also be called "Sweetheart" at home or "Mr. Doe" in more formal settings. All of them are variables for me, Joe.

mr_doe = "Joe"
puts mr_doe

This will print in the console Joe. The variable mr_doe is holding the name Joe.

Variables are nicknames for data. It's a way for us to store data. We use variables so that when we need that data, we can call our "nickname" for the data and retrieve it.

Think about it. Let's say I want to remember a phone number. I can save that phone number into a variable to retrieve when I need it.

Let's say I want to save my favorite pizza store's phone number:

favorite_pizza_store = "000-111-2222"

Now I'm getting a little hungry, what was that number again? Let me summon that variable puts favorite_pizza_store. My console will display 000-111-2222.

Awesome, now I can order some pizza.

Variables are like Labels (Another way to think of it)

Labels Pic by pexels-taryn-elliott-4440172.jpg

A delicious dinner was served at your favorite restaurant. However, the portions were so large that the waiter had to package up the food for you to go. However, this container is the type where it has a round foil bottom and white paper top. What is inside? How am I going to remember in 2 days from now when I'm scavenging through my fridge for food? -- I'll take a sharpie and write what my dinner was - "Lasagna with Mushrooms"

My variable Lasagna with Mushrooms tells me that my container has that food in it. When I want to know what is inside, I can tell based on my label.

It's important to name our variables that are relevant to the data it will be storing.

Ruby Conventions and Rules

  1. Rubyist use snake_case - this_is_snake_case
  2. Variables cannot start with a number such as 1variable
  3. Variables can hold and store numbers, strings, arrays, and hashes
  4. Variables can change their type and do NOT need to be defined. (They inherit whatever type of data they are storing - if its storing an integer such as 2, then the variable is an integer. if its storing a string, then the variable type is a string.)