Classes

We know how to create methods for individual objects, but that hardly begins to tap into the power of object-oriented programming. Now we can start thinking about how to mass-produce lots of objects from a single set of specifications.

Let's start with the problem of how to make several philosophers who all agree with each other. We could do it the hard way, telling each one what to say:

jane = Object.new
def jane.philosophize
  puts "People should behave ethically."
end
howard = Object.new
def howard.philosophize
  puts "People should behave ethically."
end
irma = Object.new
def irma.philosophize
  puts "People should behave ethically."
end
# ...

But that looks like a lot of unnecessary work at the keyboard, and of course it is. Since these ...

Get Sams Teach Yourself Ruby in 21 Days 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.