3.2. Instance variables and object state
When we created individual objects and wrote methods for each action or value we needed, we hard-coded the value into the object through the methods. With this technique, if a ticket costs $117.50, then it has a method called price that returns precisely that amount:
ticket = Object.new def ticket.price 117.50 end
But now we’re moving away from one-at-a-time object creation with Object.new and setting our sights on the practice of designing classes and creating many objects from them.
This means we’re changing the rules of the game when it comes to information like the price of a ticket. If you create a Ticket class, you can’t give it a price method that returns $117.50, for the simple reason that not ...
Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.