Object-oriented programming organizes work around objects. To calculate, manipulate data, or perform input and output, a program creates objects and asks them to act or provide information. In Ruby, those requests are messages that usually correspond to method calls. section 2.1.1 section 2.0
Start with one object
`obj = Object.new` creates a generic object and assigns it to a variable. Define `obj.talk`, then call `obj.talk`: the object executes its new method and prints a response. An object need not represent a real-world thing to be useful. section 2.1.2
def obj.talk
puts "I am an object."
puts "(Do you object?)"
end
The `def obj.talk` listing shows behavior defined on this particular object, rather than beginning with a class definition. section 2.1.2
Objects are typically instances of classes, whose method definitions help determine their behavior. Ruby also lets an individual object learn a method its class did not provide. This is why objects are a useful starting point even though classes matter: a Ruby class is itself an object. section 2.1.1 section 2.1.2
Object-oriented design asks what objects should do and how they should communicate. Method names also communicate with programmers: a cargo hold’s `unload` method expresses its purpose more clearly than the underlying stack operation it calls. Objects can therefore present domain-specific behavior while using methods supplied by a mixed-in module. section 4.1.3
Listing 4.5 Using the Stacklike module a second time for a different class
require_relative "stacklike"
class Suitcase
end
class CargoHold
include Stacklike 1
def load_and_report(obj) 2
print "Loading object "
puts obj.object_id
add_to_stack(obj) 3
end
def unload
take_from_stack 4
end
end
ch = CargoHold.new 5
sc1 = Suitcase.new
sc2 = Suitcase.new
sc3 = Suitcase.new
ch.load_and_report(sc1)
ch.load_and_report(sc2)
ch.load_and_report(sc3)
first_unloaded = ch.unload
print "The first suitcase off the plane is...."
puts first_unloaded.object_id
The cargo-hold listing puts that design choice in context: `CargoHold` includes `Stacklike` but exposes `load_and_report` and `unload` to its callers. section 4.1.3








