Ruby Logo

Ruby Data Types

Explore Ruby data types: numbers, strings, booleans, nil, and more.

Home Ruby Ruby Data Types

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?

# Even numbers can do things!
age = 25
puts age.even? # => false (25 is odd)
puts age.next # => 26 (next number)
puts age.to_s # => "25" (convert to text)

# Even text can do things!
name = "ruby"
puts name.upcase # => "RUBY"
puts name.length # => 4 (how many letters)
puts name.reverse # => "ybur"

# Even true/false can do things!
is_student = true
puts is_student.to_s # => "true"

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.

# Examples
age = 25
count = 100
negative = -10
big_number = 1_000_000

Float Decimal Numbers

Like measuring: 3.14, 2.5, 99.99. Has decimal points.

# Examples
price = 19.99
pi = 3.14159
temperature = 98.6
percentage = 0.15

What Can Numbers Do?

# Basic math
puts 5 + 3 # => 8
puts 10 - 4 # => 6
puts 6 * 7 # => 42
puts 15 / 3 # => 5

# Number methods
puts 4.even? # => true
puts 7.odd? # => true
puts 5.next # => 6
puts 3.14.round(1) # => 3.1

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.

# Examples
name = 'Sarah'
message = 'Hello World'
path = 'C:\Users\Name'

"smart" Double Quotes

Smart text: Can include variables and special characters.

# Examples
name = "Sarah"
greeting = "Hello #{name}"
multiline = "Line 1\nLine 2"

What Can Strings Do?

# String methods
text = "hello world"
puts text.upcase # => "HELLO WORLD"
puts text.downcase # => "hello world"
puts text.capitalize # => "Hello world"
puts text.length # => 11
puts text.reverse # => "dlrow olleh"
puts text.include?("world") # => true

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 value
  • 1 - any number (except 0)
  • "hello" - any text
  • [] - even empty arrays

Falsy Things Ruby Considers "False"

  • false - the actual false value
  • nil - nothing/empty

Boolean Examples

# Boolean variables
is_student = true
is_working = false
has_car = true

# Boolean operations
puts 5 > 3 # => true
puts 2 == 3 # => false
puts "hello" == "hello" # => true
puts 10 < 5 # => false

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?

# When something doesn't exist
numbers = [1, 2, 3]
puts numbers[10] # => nil (no 10th item)

# When a method finds nothing
puts numbers.find { |n| n > 5 } # => nil (no number > 5)

# When a variable isn't set
puts undefined_var # => nil

# Checking for nil
value = nil
puts value.nil? # => true
puts value == nil # => true

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

# Number to string
puts 25.to_s # => "25"
puts 3.14.to_s # => "3.14"

# String to number
puts "123".to_i # => 123
puts "3.14".to_f # => 3.14

# Nil and boolean conversions
puts nil.to_s # => ""
puts true.to_s # => "true"

# Careful with invalid conversions!
puts "hello".to_i # => 0 (can't convert, returns 0)

Practice Exercises

1 Number Magic

Try these commands in your Ruby console:

# Check if numbers are even or odd
puts 42.even? # => true
puts 15.odd? # => true

# Get the next number
puts 15.next # => 16

# Repeat something multiple times
7.times { |i| print "#{i} " } # => 0 1 2 3 4 5 6

2 String Playground

Create a string with your name and try these methods:

# Create your string
your_name = "Your Name Here"

# Try these methods
puts your_name.upcase # => "YOUR NAME HERE"
puts your_name.reverse # => "ereH emaN ruoY"
puts your_name.length # => 15
puts your_name.capitalize # => "Your name here"

3 Boolean Logic

Practice comparisons and see what you get:

# Number comparisons
puts 10 > 5 # => true
puts 3 == 3 # => true
puts 7 != 5 # => true

# String comparisons
puts "ruby".include?("by") # => true
puts "hello" == "hello" # => true

# Truthiness tests
puts !![] # => true (empty array is truthy)
puts !!nil # => false

4 Type Conversions

Practice converting between different data types:

# String to number and do math
age_string = "25"
age_number = age_string.to_i
puts age_number + 10 # => 35

# Number to string and combine
score = 95
message = "Your score: " + score.to_s
puts message # => "Your score: 95"

# Boolean to string
is_student = true
puts "Student: " + is_student.to_s # => "Student: true"

5 Hash with Symbols

Use symbols as keys in a hash (we'll learn more about hashes later):

# Create a hash with symbol keys
person = { :name => "Ruby", :age => 30 }
puts person[:name] # => "Ruby"
puts person[:age] # => 30

# Modern syntax (same thing)
person = { name: "Ruby", age: 30 }
puts person[:name] # => "Ruby"

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 .upcase and .reverse on 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.

Ruby Online Editor
Quick Tips
  • 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!

Interactive Code Runner

Ruby Code Editor
Output will appear here when you run the code...

Quick Navigation

Related Topics

Video Tutorial

Watch and learn ruby data types

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!