What if your objects could set themselves up perfectly the moment you create them?
Why Initialize method as constructor in Ruby? - Purpose & Use Cases
Imagine you want to create many objects representing different books. You have to set the title and author for each book manually every time after creating it.
Manually setting properties after creating each object is slow and easy to forget. It can cause errors if you forget to set important details, making your program unreliable.
The initialize method runs automatically when you create a new object. It sets up all the needed details right away, so you never forget and save time.
book = Book.new book.title = "Ruby Basics" book.author = "Jane"
book = Book.new("Ruby Basics", "Jane")
You can create fully ready objects in one simple step, making your code cleaner and safer.
When making a game, you can create a player with name and health set immediately, so the player is ready to play without extra steps.
Manual setup after object creation is slow and error-prone.
initialize runs automatically to set up new objects.
This makes object creation quick, safe, and clean.