Data Types & Objects
Learn what data types are, why they're important, and how Ruby treats everything as an object.
What are Data Types?
Data types define what the data is and what you can do with it, like adding numbers or combining words. Think of data types like different kinds of containers: Just like you wouldn't put soup in a wine glass, Ruby needs to know what kind of data you're working with so it can handle it properly.
Real-World Analogy
In Real Life:
- Numbers: Your age (25), price ($19.99)
- Text: Your name ("Sarah"), address
- Yes/No: Are you married? (true/false)
- Empty: An empty box (nil)
In Ruby:
- Numbers:
25,19.99 - Strings:
"Sarah","Hello" - Booleans:
true,false - Nil:
nil(nothing)
Why Do Data Types Matter?
- Different operations: You can add numbers but not strings
- Memory efficiency: Numbers take less space than text
- Error prevention: Ruby can catch mistakes early
- Clear communication: Makes your code intentions obvious
Everything is an Object
Ruby's unique feature: Unlike many programming languages, in Ruby, even simple things like numbers and text are objects that can do things!
What Does This Mean?
Why This is Amazing
- Consistency: Everything works the same way
- Power: Simple things can do complex operations
- Flexibility: You can extend any data type
- Elegance: Code reads like natural language
Numbers
Numbers in Ruby: Ruby has two main types of numbers - whole numbers (integers) and decimal numbers (floats).
Integer Whole Numbers
Like counting objects: 1, 2, 3, 100, -5. No decimal points.
Float Decimal Numbers
Like measuring: 3.14, 2.5, 99.99. Has decimal points.
What Can Numbers Do?
Strings (Text)
Strings are text: Names, messages, addresses - anything made of letters, numbers, and symbols.
'literal' Single Quotes
Exactly what you type: No special processing, just plain text.
"smart" Double Quotes
Smart text: Can include variables and special characters.
What Can Strings Do?
Booleans (True/False)
Booleans are yes/no answers: Like a light switch - it's either on (true) or off (false).
Truthy Things Ruby Considers "True"
true- the actual true value1- any number (except 0)"hello"- any text[]- even empty arrays
Falsy Things Ruby Considers "False"
false- the actual false valuenil- nothing/empty
Boolean Examples
Nil (Nothing)
Nil means "nothing": Like an empty box or a missing answer. It's Ruby's way of saying "there's no value here."
When Do You Get Nil?
Type Conversions
Converting between types: Sometimes you need to change a number to text, or text to a number. Ruby makes this easy!
Common Conversions
Practice Exercises
1 Number Magic
Try these commands in your Ruby console:
2 String Playground
Create a string with your name and try these methods:
3 Boolean Logic
Practice comparisons and see what you get:
4 Type Conversions
Practice converting between different data types:
5 Hash with Symbols
Use symbols as keys in a hash (we'll learn more about hashes later):
Try It Yourself
Practice Makes Perfect! Try these exercises in the code runner below or in an online Ruby REPL like Replit:
- Create a string and use
.upcaseand.reverseon it - Check if a number is even using
.even? - Try converting a string like "42" to an integer and add 10 to it
- Use a symbol as a key in a hash like
{:name => "Ruby"} - Test if an empty array
[]is truthy in an if statement
Try It Yourself
Now practice what you've learned! Use the code editor below to try the exercises above and experiment with Ruby data types.
- Start with the practice exercises above - try each one!
- Experiment with different data types:
puts 42.even? - Try type conversions:
puts "25".to_i + 10 - Test string methods:
puts "hello".upcase
What You've Learned
Key Takeaways
- Data types are like containers: Different types hold different kinds of information
- Everything is an object: Even simple things like numbers can do things
- Numbers: Integers (whole) and floats (decimal)
- Strings: Text with single or double quotes
- Booleans: True or false values
- Nil: Ruby's way of saying "nothing"
- Each type has methods: Ways to manipulate and work with the data
Try It Yourself - Interactive Practice
Learning Tip: The best way to understand data types is through hands-on practice! Try these examples and experiment with different values to see how Ruby handles different data types!