Contents
About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Praise for the first edition of Practical Object-Oriented Design in Ruby
Meticulously pragmatic and exquisitely articulate, Practical Object Oriented Design in Ruby makes otherwise elusive knowledge available to an audience which desperately needs it. The prescriptions are appropriate both as rules for novices and as guidelines for experienced professionals.
Katrina Owen, Creator, Exercism
I do believe this will be the most important Ruby book of 2012. Not only is the book 100% on-point, Sandi has an easy writing style with lots of great analogies that drive every point home.
Avdi Grimm, author of Exceptional Ruby and Objects on Rails
While Ruby is an object-oriented language, little time is spent in the documentation on what OO truly means or how it should direct the way we build programs. Here Metz brings it to the fore, covering most of the key principles of OO development and design in an engaging, easy-to-understand manner. This is a must for any respectable Ruby bookshelf.
Peter Cooper, editor, Ruby Weekly
So good, I couldnt put it down! This is a must-read for anyone wanting to do object-oriented programming in any language, not to mention it has completely changed the way I approach testing.
Charles Max Wood, Ruby Rogues Podcast co-host and CEO of Devchat.tv
Distilling scary OO design practices with clear-cut examples and explanations makes this a book for novices and experts alike. It is well worth the study by anyone interested in OO design being done right and light. I thoroughly enjoyed this book.
Manuel Pais, DevOps and Continuous Delivery Consultant, Independent
If you call yourself a Ruby programmer, you should read this book. Its jam-packed with great nuggets of practical advice and coding techniques that you can start applying immediately in your projects.
Ylan Segal, San Diego Ruby User Group
This is the best OO book Ive ever read. Its short, sweet, but potent. It slowly moves from simple techniques to more advanced, each example improving on the last. The ideas it presents are useful not just in Ruby but in static languages like C# too. Highly recommended!
Kevin Berridge, software engineering manager, Pointe Blank Solutions, and organizer, Burning River Developers Meetup
This is the best programming book Ive read in ages. Sandi talks about basic principles, but these are things were probably still doing wrong and she shows us why and how. The book has the perfect mix of code, diagrams, and words. I cant recommend it enough and if youre serious about being a better programmer, youll read it and agree.
Derick Hitchcock, software engineer, Cisco
Metzs take on the subject is rooted strongly in theory, but the explanation always stays grounded in real world concerns, which helped me to internalize it. The book is clear and concise, yet achieves a tone that is more friendly than terse.
Alex Strasheim, network administrator, Ensemble Travel Group
Whether youre just getting started in your software development career, or youve been coding for years (like I have), its likely that youll learn a lot from Ms. Metzs book. She does a fantastic job of explaining the whys of well-designed software along with the hows.
Gabe Hollombe, software craftsman, avantbard.com
P RACTICAL O BJECT -O RIENTED D ESIGN
An Agile Primer Using Ruby
Second Edition
Sandi Metz
Boston Columbus New York San Francisco Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City So Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2018939833
Copyright 2019 Pearson Education, Inc.
All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 978-0-13-445647-8
ISBN-10: 0-13-445647-5
For Amy, who read everything first
Introduction
We want to do our best work, and we want the work we do to have meaning. And, all else being equal, we prefer to enjoy ourselves along the way.
Those of us whose work is to write software are incredibly lucky. Building software is a guiltless pleasure because we get to use our creative energy to get things done. We have arranged our lives to have it both ways; we can enjoy the pure act of writing code in sure knowledge that the code we write has use. We produce things that matter. We are modern craftspeople, building structures that make up present-day reality, and no less than bricklayers or bridge builders, we take justifiable pride in our accomplishments.