Ruby Logo

Ruby Object Model & Everything is an Object

Master Ruby's fundamental principle: everything is an object. Learn about BasicObject hierarchy, method lookup chain, eigenclasses, and singleton methods.

Home Ruby Ruby Object Model & Everything is an Object

Ruby Object Model Mastery

Understand Ruby's fundamental principle: "Everything is an object." Master the object model, BasicObject hierarchy, and how Ruby's object-oriented design influences every aspect of the language.

Everything is an Object

Ruby's Core Philosophy: In Ruby, everything is an object - numbers, strings, classes, modules, even true, false, and nil. This unified object model makes Ruby incredibly consistent and powerful.

Proving Everything is an Object

# Numbers are objects
42.class
# => Integer
42.methods.count
# => 170+ methods!
42.even?
# => true

# Strings are objects
"hello".class
# => String
"hello".upcase
# => "HELLO"
"hello".reverse
# => "olleh"

# Even true, false, and nil are objects!
true.class
# => TrueClass
false.class
# => FalseClass
nil.class
# => NilClass

# Classes are objects too!
String.class
# => Class
Class.class
# => Class

# Methods are objects
method_obj = "hello".method(:upcase)
method_obj.class
# => Method
method_obj.call
# => "HELLO"

Ruby Object Hierarchy

Object Hierarchy: Ruby's object system is built on a clear hierarchy starting with BasicObject at the root, providing a clean and consistent foundation for all objects.

The Object Hierarchy Tree

# Ruby Object Hierarchy
BasicObject
└── Object
├── String
├── Numeric
│ ├── Integer
│ └── Float
├── Array
├── Hash
├── Class
├── Module
├── TrueClass
├── FalseClass
├── NilClass
└── ... (all other classes)

# Verify the hierarchy
String.superclass
# => Object
Object.superclass
# => BasicObject
BasicObject.superclass
# => nil (top of hierarchy)

# Check if everything inherits from Object
42.is_a?(Object)
# => true
"hello".is_a?(Object)
# => true
[1, 2, 3].is_a?(Object)
# => true
String.is_a?(Object)
# => true (classes are objects too!)

BasicObject vs Object

# BasicObject - minimal object with almost no methods
BasicObject.instance_methods
# => [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

# Object - adds common methods we expect
Object.instance_methods.count
# => 60+ methods
Object.instance_methods.include?(:to_s)
# => true
Object.instance_methods.include?(:inspect)
# => true
Object.instance_methods.include?(:class)
# => true

# Why BasicObject exists - for proxy objects
class
MinimalProxy
<
BasicObject
def
initialize
(target)
@target = target
end

def
method_missing
(method, *args, &block)
@target.send(method, *args, &block)
end
end

proxy = MinimalProxy.new("hello")
proxy.upcase
# => "HELLO" (delegated to target)

Object Identity & Equality

Object Identity: Every object in Ruby has a unique identity (object_id) and various ways to compare objects for equality and identity.

Object Identity Methods

# Object identity - unique ID for each object
str1 = "hello"
str2 = "hello"
str3 = str1

str1.object_id
# => 70368527277240 (unique number)
str2.object_id
# => 70368527277220 (different object)
str3.object_id
# => 70368527277240 (same as str1)

# Identity comparison with equal?
str1.equal?(str2)
# => false (different objects)
str1.equal?(str3)
# => true (same object)

# Value comparison with ==
str1 == str2
# => true (same content)
str1 == str3
# => true (same content)

# Special case: small integers and symbols are cached
42.equal?(42)
# => true (same object!)
:symbol.equal?(:symbol)
# => true (symbols are immutable and cached)

Object Inspection and Debugging

# Object inspection methods
obj = "Hello Ruby"

obj.class
# => String
obj.inspect
# => "\"Hello Ruby\"" (shows quotes)
obj.to_s
# => "Hello Ruby" (string representation)

# Object introspection
obj.methods.count
# => 180+ (all available methods)
obj.instance_variables
# => [] (strings have no instance variables)
obj.respond_to?(:upcase)
# => true

# Object ancestry
String.ancestors
# => [String, Comparable, Object, Kernel, BasicObject]

# Check object type
obj.kind_of?(String)
# => true
obj.kind_of?(Object)
# => true
obj.instance_of?(String)
# => true (exact class match)

Classes as Objects

Classes are Objects: In Ruby, classes themselves are objects! They're instances of the Class class and can be manipulated at runtime like any other object.

Classes as First-Class Objects

# Classes are instances of Class
String.class
# => Class
Array.class
# => Class
Class.class
# => Class (even Class is an instance of Class!)

# Classes have methods like any object
String.methods.include?(:new)
# => true
String.respond_to?(:ancestors)
# => true

# You can store classes in variables
my_class = String
instance = my_class.new("hello")
instance.class
# => String

# Classes can be created dynamically
DynamicClass = Class.new do
def
greet
"Hello from dynamic class!"
end
end

obj = DynamicClass.new
obj.greet
# => "Hello from dynamic class!"

Metaclasses and Eigenclasses

# Every object has a hidden eigenclass (singleton class)
str = "hello"
eigenclass = str.singleton_class
eigenclass.class
# => Class

# Add methods to specific objects
def
str.shout
self.upcase + "!!!"
end

str.shout
# => "HELLO!!!"

other_str = "world"
other_str.respond_to?(:shout)
# => false (method only on str object)

# Class methods are singleton methods on the class object
class
MyClass
def
self
.class_method
"I'm a class method"
end
end

MyClass.singleton_class.instance_methods(false)
# => [:class_method]

What You've Learned

Key Takeaways

  • Everything is an object: Numbers, strings, classes, modules, and even nil are objects with methods
  • Object hierarchy starts with BasicObject: The minimal root class with essential methods only
  • Object identity vs equality: Use equal? for identity, == for value comparison
  • Classes are objects too: They're instances of Class and can be manipulated at runtime
  • Eigenclasses enable singleton methods: Each object can have unique behavior via its hidden singleton class

Try It Yourself - Interactive Practice

Learning Tip: The best way to understand Ruby's object model is by exploring it hands-on! Use these examples to discover how objects, classes, and methods work together.

Interactive Code Runner

Ruby Code Editor
Output will appear here when you run the code...
Ruby Internals & VM
Method Lookup Path & Ancestors Chain

Quick Navigation

Read Topic
Watch Video Tutorial

Related Topics

Garbage Collection & Memory Management → Open Classes & Monkey Patching → Classes & Objects →

Back to Ruby Home

Video Tutorial

Watch and learn ruby object model & everything is an object

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