liveBook · Manning

Object-oriented programming

As explained in The Well-Grounded Rubyist, Fourth Edition

Object-oriented programming is a programming paradigm that organizes computation around objects that encapsulate data and perform actions in response to requests.

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

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines the approach through a generic Ruby object and its method, then connects it to design; defers in-depth treatment of classes.
Examples
Uses a talking generic object and a cargo hold whose `unload` method delegates to a stack operation.
What it assumes
Starts with a generic object and variable reference, while leaving detailed class behavior for later.
What you can do after
Build it from scratch by creating a Ruby object, defining and calling its method, and choosing names that express an object's purpose.
How it connects
Connects objects and messages to method calls, classes, modules, and object-oriented design.

Also explained in

Compare how 9 books explain object-oriented programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.