Design Patterns in Ruby

  • ISBN13:


  • ISBN10:


  • Edition: 1st
  • Format: Hardcover
  • Copyright: 2007-12-10
  • Publisher: Addison-Wesley Professional

Note: Supplemental materials are not guaranteed with Rental or Used book purchases.

Purchase Benefits

  • Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $56.99 Save up to $19.95
  • Rent Book $37.04
    Add to Cart Free Shipping


Supplemental Materials

What is included with this book?

  • The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.
  • The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.


Praise forDesign Patterns in Ruby "Design Patterns in Rubydocuments smart ways to resolve many problems that Ruby developers commonly encounter. Russ Olsen has done a great job of selecting classic patterns and augmenting these with newer patterns that have special relevance for Ruby. He clearly explains each idea, making a wealth of experience available to Ruby developers for their own daily work." -Steve Metsker, Managing Consultant with Dominion Digital, Inc. "This book provides a great demonstration of the key 'Gang of Four'design patterns without resorting to overly technical explanations. Written in a precise, yet almost informal style, this book covers enough ground that even those without prior exposure to design patterns will soon feel confident applying them using Ruby. Olsen has done a great job to make a book about a classically 'dry'subject into such an engaging and even occasionally humorous read." -Peter Cooper "This book renewed my interest in understanding patterns after a decade of good intentions. Russ picked the most useful patterns for Ruby and introduced them in a straightforward and logical manner, going beyond the GoF's patterns. This book has improved my use of Ruby, and encouraged me to blow off the dust covering the GoF book." -Mike Stok "Design Patterns in Rubyis a great way for programmers from statically typed objectoriented languages to learn how design patterns appear in a more dynamic, flexible language like Ruby." -Rob Sanheim, Ruby Ninja, Relevance Most design pattern books are based on C++ and Java. But Ruby is different-and the language's unique qualities make design patterns easier to implement and use. In this book, Russ Olsen demonstrates how to combine Ruby's power and elegance with patterns, and write more sophisticated, effective software with far fewer lines of code. After reviewing the history, concepts, and goals of design patterns, Olsen offers a quick tour of the Ruby language-enough to allow any experienced software developer to immediately utilize patterns with Ruby. The book especially calls attention to Ruby features that simplify the use of patterns, including dynamic typing, code closures, and "mixins" for easier code reuse. Fourteen of the classic "Gang of Four" patterns are considered from the Ruby point of view, explaining what problems each pattern solves, discussing whether traditional implementations make sense in the Ruby environment, and introducing Ruby-specific improvements. You'll discover opportunities to implement patterns in just one or two lines of code, instead of the endlessly repeated boilerplate that conventional languages often require. Design Patterns in Rubyalso identifies innovative new patterns that have emerged from the Ruby community. These include ways to create custom objects with metaprogramming, as well as the ambitious Rails-based "Convention Over Configuration" pattern, designed to help integrate entire applications and frameworks. Engaging, practical, and accessible,Design Patterns in Rubywill help you build better software while making your Ruby programming experience more rewarding.

Author Biography

Russ Olsen has been building software for more than twenty-five years. He has led projects through several generations of programming technologies, from FORTRAN to C to C++ to Java, and now Ruby. He has been using and teaching Ruby since 2002. Olsen writes the popular technology blog Technology As If People Mattered (http://www.russolsen.com).

Table of Contents

Forewordp. xvii
Prefacep. xix
Acknowledgmentsp. xxv
About the Authorp. xxvii
Patterns and Rubyp. 1
Building Better Programs with Patternsp. 3
The Gang of Fourp. 4
Patterns for Patternsp. 4
Separate Out the Things That Change from Those That Stay the Samep. 5
Program to an Interface, Not an Implementationp. 5
Prefer Composition over Inheritancep. 7
Delegate, Delegate, Delegatep. 12
You Ain't Gonna Need Itp. 13
Fourteen Out of Twenty-Threp. 15
Patterns in Ruby?p. 17
Getting Started with Rubyp. 19
Interactive Rubyp. 20
Saying Hello Worldp. 20
Variablesp. 23
Fixnums and Bignumsp. 24
Floatsp. 26
There Are No Primitives Herep. 26
But Sometimes There Is No Objectp. 27
Truth, Lies, and nilp. 28
Decisions, Decisionsp. 30
Loopsp. 32
More about Stringsp. 34
Symbolsp. 37
Arraysp. 38
Hashesp. 40
Regular Expressionsp. 40
A Class of Your Ownp. 41
Getting at the Instance Variablesp. 43
An Object Asks: Who Am I?p. 46
Inheritance, Subclasses, and Superclassesp. 46
Argument Optionsp. 47
Modulesp. 49
Exceptionsp. 52
Threadsp. 53
Managing Separate Source Filesp. 54
Wrapping Upp. 55
Patterns in Rubyp. 57
Varying the Algorithm with the Template Methodp. 59
Keeping Up with What Life Throws at Youp. 60
Separate the Things That Stay the Samep. 61
Discovering the Template Method Patternp. 65
Hook Methodsp. 66
But Where Are All the Declarations?p. 68
Types, Safety, and Flexibilityp. 69
Unit Tests Are Not Optionalp. 71
Using and Abusing the Template Method Patternp. 73
Templates in the Wildp. 74
Wrapping Upp. 75
Replacing the Algorithm with the Strategyp. 77
Delegate, Delegate, and Delegate Againp. 78
Sharing Data between the Context and the Strategyp. 80
Duck Typing Yet Againp. 82
Procs and Blocksp. 84
Quick-and-Dirty Strategiesp. 88
Using and Abusing the Strategy Patternp. 90
The Strategy Pattern in the Wildp. 90
Wrapping Upp. 92
Keeping Up with the Times with the Observerp. 95
Staying Informedp. 95
A Better Way to Stay Informedp. 97
Factoring Out the Observable Supportp. 100
Code Blocks as Observersp. 104
Variations on the Observer Patternp. 105
Using and Abusing the Observer Patternp. 106
Observers in the Wildp. 108
Wrapping Upp. 109
Assembling the Whole from the Parts with the Compositep. 111
The Whole and the Partsp. 112
Creating Compositesp. 114
Sprucing Up the Composite with Operatorsp. 118
An Array as a Composite?p. 119
An Inconvenient Differencep. 120
Pointers This Way and Thatp. 120
Using and Abusing the Composite Patternp. 122
Composites in the Wildp. 123
Wrapping Upp. 125
Reaching into a Collection with the Iteratorp. 127
External Iteratorsp. 127
Internal Iteratorsp. 130
Internal Iterators versus External Iteratorsp. 131
The Inimitable Enumerablep. 133
Using and Abusing the Iterator Patternp. 134
Iterators in the Wildp. 136
Wrapping Upp. 140
Getting Things Done with Commandsp. 143
An Explosion of Subclassesp. 144
An Easier Wayp. 145
Code Blocks as Commandsp. 147
Commands That Recordp. 148
Being Undone by a Commandp. 151
Queuing Up Commandsp. 154
Using and Abusing the Command Patternp. 154
The Command Pattern in the Wildp. 155
ActiveRecord Migrationsp. 155
Madeleinep. 156
Wrapping Upp. 160
Filling in the Gaps with the Adapterp. 163
Software Adaptersp. 164
The Near Missesp. 167
An Adaptive Alternative?p. 168
Modifying a Single Instancep. 170
Adapt or Modify?p. 172
Using and Abusing the Adapter Patternp. 173
Adapters in the Wildp. 173
Wrapping Upp. 174
Getting in Front of Your Object with a Proxyp. 175
Proxies to the Rescuep. 176
The Protecti
Table of Contents provided by Publisher. All Rights Reserved.


A former colleague of mine used to say that thick books about design patterns were evidence of an inadequate programming language. What he meant was that, since design patterns are the common idioms of code, a good programming language should make them very easy to implement. An ideal language would so thoroughly integrate the patterns that they would almost disappear from sight. To take an extreme example, in the late 80's I worked on a project that produced object oriented code in C. Yes, C, not C++. We pulled this off by having each "object" (actually a C structure) point off to a table of function pointers. We operated on our "objects" by chasing the pointer to the table and calling functions out of the table, thereby simulating a method call on an object. It was awkward and messy, but it worked. Had we thought of it, we might have called this technique the "object oriented" pattern. Of course with the advent of C++ and then Java, our object oriented pattern disappeared, absorbed so thoroughly into the language that it vanished from sight. Today, we don't usually think of object orientation as a pattern-it is too easy. But many things are still not easy enough. The justly famous Gang of Four book, (Design Patterns: Elements of Reusable Object-Oriented Softwareby Gamma, Helm, Johnson, and Vlissides) is required reading for every software engineer today. But actually implementing many of the patterns described in Design Patterns with the languages in widespread use today, Java and C++ and perhaps C#, looks and feels a lot like my 1980s vintage handcrafted object system. Too painful. Too verbose. Too prone to bugs. The Ruby programming language takes us a step closer to my old friend's ideal, a language that makes implementing patterns easy, so easy that sometimes they fade into the background. Building patterns in Ruby is easier for a number of reasons: Ruby is dynamically typed. By dispensing with static typing, Ruby dramatically reduces the code overhead of building most programs, including those that implement patterns. Ruby has code closures. Ruby allows us to pass around chunks of code and associated scope without having to laboriously construct entire classes and objects that do nothing else. Ruby classes are real objects. Since a class in Ruby is just another object, we can do any of the usual runtime things to a Ruby class that we can do to any object. At runtime we can create totally new classes. We can also modify existing classes by adding or deleting methods. We can even clone a class and change the copy, leaving the original alone. Ruby has an elegant system of code reuse. In addition to garden variety inheritance, Ruby provides also allows us to define mixins, which are a simple but flexible way to write code that can be shared among several classes. All of this makes code in Ruby compressible: In Ruby, like Java and C++, you can implement very sophisticated ideas, but with Ruby it is possible to hide the details of your implementations much more effectively. As you will see on the pages that follow, many of the design patterns that require many lines of endlessly repeated boiler plate code in traditional static languages require only one or two lines in Ruby. You can make a class into a singleton with a simple include Singleton. You can delegate as easily as you can inherit. Since Ruby enables you to say more interesting things in each line of code, you end up with less code. This is not just a question of keyboard laziness, it is an application of the DRY (Don't Repeat Yourself) principal. I don't think anyone today would mourn the passing of my old object oriented pattern in C-it worked for me, but it made me work for it, too. In the same way, the traditional implementations of many design patterns work, but they make you work too. Ruby is a real step forward to be able to do that work onl

Rewards Program

Write a Review