Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of variables like jars in a kitchen. You don't just throw flour and salt in a pile on the counter; you put them in containers and slap a label on them. The label "Sugar" tells you what's inside, but the jar itself is just a holder. If you empty out the sugar and fill that same jar with salt, the label might still say "Sugar" for a moment, but the actual substance inside has changed. In Ruby, variables are those labels, and the data types are the ingredients.

Labeling your data

In Ruby, we use snake_case for our variables. I’ve seen a lot of beginners try to use camelCase because they're coming from JavaScript or Java, but in the Ruby world, we stick to underscores. It's just the way the community does it, and your code will look much more "idiomatic" if you follow suit.

# A simple label for a string
user_name = "Alice"

# A label for a whole number
login_attempts = 3

# A label for a decimal
account_balance = 42.50

Notice how I didn't have to tell Ruby that user_name is a string or that login_attempts is an integer? That's because Ruby is dynamically typed. It looks at the value you're assigning and figures out the type on its own. It saves a lot of typing, though it does mean you have to be careful not to accidentally put "salt" in your "sugar" jar.

The different ingredients in the pantry

While Ruby handles the types for you, you still need to know what those types are so you don't try to do something impossible—like trying to subtract a word from a number.

  • Integers and Floats: Whole numbers (Integers) and decimals (Floats). Ruby is smart enough to handle massive numbers without crashing, which is a huge relief.
  • Strings: Text wrapped in quotes. "Hello" or 'Hello'. I usually use double quotes because they allow for string interpolation, which we'll hit later.
  • Symbols: These look like :my_symbol. Think of a symbol as a "frozen" string. While a string is meant to be manipulated (changed, sliced, concatenated), a symbol is meant to be a unique identifier. I use these constantly for keys in hashes or for status flags like :pending or :completed.
  • Booleans: Simply true or false. The bread and butter of logic.
  • Nil: nil represents the absolute absence of a value. It's not the same as an empty string or a zero; it's literally "nothing." It's the most common source of errors in Ruby (the dreaded NoMethodError), so keep a close eye on it.

Switching contents on the fly

Because of that dynamic typing I mentioned, you can change the type of data a variable holds at any time. I'll be honest: just because you can do this doesn't mean you always should. It can make your code confusing if a variable named price starts as a number and suddenly becomes the string "Sold Out".

current_status = 100 # Started as an Integer
current_status = "Active" # Now it's a String
current_status = :online # Now it's a Symbol

Beyond the local jar

Most of the time, you'll just use the standard variables I showed you. But as you start building larger apps, you'll see labels with special prefixes. You'll see @name (instance variables) and $global_config (global variables). For now, just know that the @ symbol means that variable belongs to a specific object, and the $ means it's accessible from anywhere in your entire program. Use globals sparingly—they're like leaving a jar of open mayonnaise on the counter; eventually, something is going to mess it up and ruin everything.




📋 Practical Task

Build a Retro RPG Character Sheet

Your task is to create a script that defines a character's starting stats using the various Ruby types we discussed. This will help you practice distinguishing between data that should be a number, a string, a symbol, or a boolean.

Create a script that defines the following variables for a character:

  • character_name: A string.
  • level: An integer.
  • health_points: A float (e.g., 95.5).
  • character_class: A symbol (e.g., :warrior, :mage, or :rogue).
  • is_alive: A boolean.
  • equipment_slot_2: Set this to nil to represent an empty slot.

Finally, use puts to print each variable to the console so you can verify the values. Bonus points if you change the level variable to a new number halfway through your script to simulate a "level up"!

Rating
0 0

There are no comments for now.

to be the first to leave a comment.