Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
4: Variables and Types
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:pendingor:completed. - Booleans: Simply
trueorfalse. The bread and butter of logic. - Nil:
nilrepresents 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 dreadedNoMethodError), 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 tonilto 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"!
There are no comments for now.