Ruby Objects

Understanding instances and object-oriented programming

🎯 What are Ruby Objects?

Objects are instances of classes. Everything in Ruby is an object, from numbers to strings. Objects have their own data and can perform actions through methods.


# Creating objects from a class
class Cat
  def meow
    puts "Meow!"
  end
end

cat1 = Cat.new
cat1.meow
                                    

Output:

Meow!

Key Object Concepts

🆕

Creation

Create objects with .new method

obj = MyClass.new
🔑

Identity

Each object has a unique ID

obj.object_id
💾

State

Objects store their own data

@name = "Value"
âš¡

Behavior

Objects respond to methods

obj.method_name

🔹 Creating Multiple Objects

Each object created from a class is independent with its own data. You can create many objects from the same class, and each maintains separate state:

class Student
  def initialize(name, grade)
    @name = name
    @grade = grade
  end
  
  def display_info
    puts "#{@name}: Grade #{@grade}"
  end
end

student1 = Student.new("Alice", "A")
student2 = Student.new("Bob", "B")

student1.display_info
student2.display_info
                            

Output:

Alice: Grade A

Bob: Grade B

🔹 Object Identity

Every object in Ruby has a unique identifier. You can check if two variables point to the same object or different objects using object_id:

class Phone
  attr_accessor :model
  
  def initialize(model)
    @model = model
  end
end

phone1 = Phone.new("iPhone")
phone2 = Phone.new("iPhone")
phone3 = phone1

puts phone1.object_id
puts phone2.object_id
puts phone3.object_id
puts phone1 == phone3  # Same object?
                            

Output:

260

280

260

true

🔹 Object Methods

Objects can call methods to perform actions or retrieve information. Ruby provides built-in methods for all objects like class, methods, and respond_to?:

class Robot
  def initialize(name)
    @name = name
  end
  
  def greet
    "Hello, I'm #{@name}"
  end
end

robot = Robot.new("R2D2")
puts robot.greet
puts robot.class
puts robot.respond_to?(:greet)
                            

Output:

Hello, I'm R2D2

Robot

true

🔹 Everything is an Object

In Ruby, even basic data types like numbers and strings are objects. This means they have methods and behaviors you can use:

# Numbers are objects
puts 5.class
puts 5.next

# Strings are objects
puts "hello".class
puts "hello".upcase

# Arrays are objects
puts [1, 2, 3].class
puts [1, 2, 3].length
                            

Output:

Integer

6

String

HELLO

Array

3

🔹 Object State and Behavior

Objects combine state (data stored in variables) and behavior (methods). The state can change over time while the object exists:

class BankAccount
  def initialize(balance)
    @balance = balance
  end
  
  def deposit(amount)
    @balance += amount
    puts "Deposited $#{amount}. New balance: $#{@balance}"
  end
  
  def withdraw(amount)
    @balance -= amount
    puts "Withdrew $#{amount}. New balance: $#{@balance}"
  end
end

account = BankAccount.new(100)
account.deposit(50)
account.withdraw(30)
                            

Output:

Deposited $50. New balance: $150

Withdrew $30. New balance: $120

🧠 Test Your Knowledge

What method creates a new object from a class?