<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Practicing Ruby</title><link>http://blog.rubybestpractices.com/</link><description>Solve Interesting Problems -- Write Better Code</description><language>en-us</language><item><title>Practicing Ruby's second volume now freely available</title><description>&lt;p&gt;Keeping with my promise to release content from my &lt;a href="http://practicingruby.com"&gt;Practicing Ruby journal&lt;/a&gt;, I&amp;#8217;ve put together a massive link dump of articles from its second volume. Please enjoy them and share them with your friends.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/tmxmprhfrpwq"&gt;Issue 2.1: Ways to load code&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/bhftubljbomqpmifbibmzmptlxhoin"&gt;Issue 2.2: How to attack sticky problems&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/mvzhovpjbghr"&gt;Issue 2.3: A closure is a double edged sword&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/ggcwduoyfqmz"&gt;Issue 2.4: Implementing Enumerable &amp;amp; Enumerator in Ruby&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/afshdqdholth"&gt;Issue 2.5: Thoughts on regression testing&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/vbmlgkdtahzd"&gt;Issue 2.6: Learning new things step-by-step&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/ozkzbsdmagcm"&gt;Issue 2.7: &amp;#8220;Unobtrusive Ruby&amp;#8221; in Practice&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/jleygxejeopq"&gt;Issue 2.8: Ruby and the singleton pattern don&amp;#8217;t get along&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/qyxvmrgmhuln"&gt;Issue 2.9: Building Unix-style command line applications&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/nlhxgszkgenq"&gt;Issue 2.10: From requirements discovery to release&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/iptocucwujtj"&gt;Issue 2.11: Domain specific &lt;span class="caps"&gt;API&lt;/span&gt; construction&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/oelhlibhtlkx"&gt;Issue 2.12: Working with binary file formats&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/gthgvfebjvyn"&gt;Issue 2.13: Designing business reporting applications&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/vpxpovppchww"&gt;Issue 2.14: Thoughts on &amp;#8220;Arguments and Results&amp;#8221;, Part 1&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/wdykkrmdfjvf"&gt;Issue 2.15: Thoughts on &amp;#8220;Arguments and Results&amp;#8221;, Part 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Practicing Ruby is a subscriber-supported service, and is literally how I pay my bills. If you like these articles and want to help make sure I can keep writing more of them, please &lt;a href="http://practicingruby.com/"&gt;become a subscriber&lt;/a&gt;. There is lots of content available to members that has not been publicly released yet, and I&amp;#8217;m adding more articles regularly.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;PS: Anyone who can&amp;#8217;t pay the full $8/month fee for &lt;span class="caps"&gt;ANY&lt;/span&gt; reason is welcome to contact gregory.t.brown@gmail.com for a free account.&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Mon, 26 Mar 2012 14:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/063-practicing-ruby-v2.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/063-practicing-ruby-v2.html</guid></item><item><title>Kicking ass together: How to improve coding skills as a group</title><description>&lt;p&gt;Over the last year and a half, I have worked with a small group of students and staff to create an excellent online learning community at Mendicant University. Unfortunately, because Mendicant is something that we&amp;#8217;re intentionally scaling at a very slow pace, we won&amp;#8217;t directly reach as many people as we&amp;#8217;d like to any time soon.&lt;/p&gt;
&lt;p&gt;In this post, I&amp;#8217;ve collected some of the things that I think contribute to making Mendicant University a great place to learn. I&amp;#8217;d love to see these ideas integrated into more local and online groups, so that we can create a lot more great places for programmers to learn and grow.&lt;/p&gt;
&lt;h3&gt;Emphasize the personal goals and interests of your group&lt;/h3&gt;
&lt;p&gt;Getting a small group together to discuss the currently trending topics in the global technology ecosystem while ignoring what is going on within your study group is a tremendous waste of effort and potential. It is much more effective to focus on the things that relate to the projects your community members are actively working on, as well as the challenges they face in their daily work.&lt;/p&gt;
&lt;p&gt;Rather that having talks or study sessions on topics of general interest, it is more effective to figure out some concrete problems that members of your group need to solve, and then use your combined resources to help identify the right learning materials or people to talk to in order to get past the obstacles in your way.&lt;/p&gt;
&lt;p&gt;While there are probably a lot of ways to do this, one interesting approach might be to have folks announce what they&amp;#8217;re working on and what topics they&amp;#8217;re interested in at the beginning of each meeting and then group together those with common interests. For an online group, you can use something like a wiki or periodic mailing list roundups to achieve similar results.&lt;/p&gt;
&lt;h3&gt;Practice formal code reviews&lt;/h3&gt;
&lt;p&gt;Rather than just talking in the abstract about ideas or strategies, it is best to actually sit down with editors open and code in front of you ready to review. A lot can be learned just through the process of explaining your code to someone else, and it is impossible to overstate the value of trying to teach others, even about tiny things like idioms or conventions.&lt;/p&gt;
&lt;p&gt;If a particular bit of code would be too gnarly to review effectively, it can still be productive to hammer out a simplified example that illustrates the core concept you&amp;#8217;re trying to study. The more concrete you can make your discussion, the more likely it is that you&amp;#8217;ll get more directly valuable information while interacting with others.&lt;/p&gt;
&lt;h3&gt;Prefer evidence based arguments&lt;/h3&gt;
&lt;p&gt;Arguments based on authority (&amp;#8220;So and so said&amp;#8230;&amp;#8221;) and arguments based on popularity (&amp;#8220;Everyone does this&amp;#8230;&amp;#8221;) are very common in programming communities, but ultimately distract and divide more than they enlighten. Fortunately, there is a more productive way to have a discussion about code.&lt;/p&gt;
&lt;p&gt;The most important thing for any discussion about different approachs to solving a given problem is to establish a clear context. Without context, it&amp;#8217;s unclear whether a hammer or a bulldozer is the right tool for the job. With a context established, all discussion can refer back to it as a basis for determining whether a given approach is suitable or not.&lt;/p&gt;
&lt;p&gt;From there, it&amp;#8217;s mostly a matter of weighing tradeoffs between different approaches. For example, you might say something like &amp;#8220;Sqlite is convenient to use because it doesn&amp;#8217;t require you to run a database server, but because you&amp;#8217;re going to be working with &lt;span class="caps"&gt;GIS&lt;/span&gt; data, you will probably want to work with PostgreSQL because PostGIS provides a ton of useful functionality there&amp;#8221;. This statement is not bulletproof, but it will lead to a much better conversation than &amp;#8220;Sqlite sucks, use PostgreSQL&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Sometimes, you will want to express things that are purely personal preferences, and that&amp;#8217;s okay. But when you do so, it helps to point out that you&amp;#8217;re not trying to have a reasoned argument but instead just sharing your gut feeling. This will help avoid religious arguments in most cases.&lt;/p&gt;
&lt;h3&gt;Seek purposeful ways of practicing and studying&lt;/h3&gt;
&lt;p&gt;Every day you will find some new recommendation for what the next revolutionary way to learn how to code is. You&amp;#8217;ll also discover that some new topic of interest is the one that every just needs to learn and talk about &lt;span class="caps"&gt;RIGHT&lt;/span&gt; &lt;span class="caps"&gt;NOW&lt;/span&gt;. Of course, this false sense of importance and urgency is nothing more than smoke and mirrors, and if you get caught up in it you&amp;#8217;ll spend more time chasing your tail than you do building useful stuff.&lt;/p&gt;
&lt;p&gt;Whenever possible, use a goal based, hands-on approach to learning new things. Try out new ideas and technology on low-risk projects that might end up being useful to you later if possible. If you do spend some time on deliberate practice as opposed to practicing-while-working, make sure that it&amp;#8217;s to address a specific need or problem you&amp;#8217;ve encountered. For example: a code kata might be just the right tool for learning the syntax of a new language or trying out learning new tricks in your text editor, but it&amp;#8217;s a bad idea to practice code katas just waiting for serindipitous learning. Happy accidents do happen, but you shouldn&amp;#8217;t rely on luck as a way to move you forward.&lt;/p&gt;
&lt;p&gt;While this particular point is more focused on individual practice than it is on practicing in a group, the same set of goals should be present in any group activities you engage in. Whenever possible, break into focus groups as needed to avoid forcing several members to practice/study something only remotely interesting or relevant to them. Our available time and attention is a precious resource, and should be spent wisely.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s worth noting that this advice is &lt;span class="caps"&gt;NOT&lt;/span&gt; meant to be taken as a push to focus only on narrow and practical goals. Deep studying of theory or classical topics can be very useful and is particularly suitable for group activities. It&amp;#8217;s just essential to tie this back in some way to a deep and personal goal rather than just a vague intellectual interest.&lt;/p&gt;
&lt;h3&gt;Create a good balance between socializing and getting stuff done&lt;/h3&gt;
&lt;p&gt;Without room for socializing, it will be hard for any group to develop a common culture and a shared set of interests. However, I&amp;#8217;ve seen far too many users group &amp;#8220;hackfests&amp;#8221; turn into water cooler discussions, and if the social norms are established to encourage this sort of behavior, there will be no chance for the values of deep practice to set in.&lt;/p&gt;
&lt;p&gt;For in-person gatherings, it might make sense to have either a working session followed by some social event, or separate nights for social and working sessions. Online, similar separations can be made by having some activities designed for work and others for casual socialization. You don&amp;#8217;t need to be a fascist about enforcing this dividing line, but setting some clear expectations go a long way.&lt;/p&gt;
&lt;h3&gt;Establish a culture of participation and sharing&lt;/h3&gt;
&lt;p&gt;Your group should be defined by what its members do, not just what they talk about. Do what you can to highlight the contributions of your members, and favor activities that involve active collaboration rather than ones that require one person to do the bulk of the work while everyone else passively consumes information.&lt;/p&gt;
&lt;p&gt;For in-person gatherings, I like things like lightning talks and hackfests, as long as they are mostly focused on what group members are working on, as opposed to just repeating something that someone else said or did. I also think that group discussions can be very effective as long as some structure and decent moderation is provided.&lt;/p&gt;
&lt;p&gt;Online, similar results can be achieved by focusing on code reviews and discussions of specific articles or questions about how to solve a given problem.&lt;/p&gt;
&lt;p&gt;Both online and in person, working on and discussing open source software goes a long way. Doing everything you can to encourage your members to share their work publicly will make a huge difference and create a positive form of social pressure to actively share. Of course, not everyone will have time to run their own projects or contribute large patches to other projects. But as soon as you hear someone complain about a bug or problem that they haven&amp;#8217;t reported yet, that&amp;#8217;s the time to sit down and show them how to write a minimal reproducing example and file a ticket. Sometimes a few minutes of guidance is all it takes to transform someone who does little more than complain on twitter to an active contributor to open source projects.&lt;/p&gt;
&lt;h3&gt;Practice social awareness and take care not to exclude marginalized groups&lt;/h3&gt;
&lt;p&gt;Many technical groups (both online and local) fail at breaking away from some pretty embarrassing social norms. While we may not feel this way as individuals, as a group we are consistently guilty of tolerating exclusionary behavior that would not be tolerated in most other social settings. Remember that while the bulk of programmers who attend technical events are heterosexual upper-middle class white males in their 20s and 30s, there is a whole world of folks who are just as passionate and capable of achieving great things technically but do not fit that stereotype.&lt;/p&gt;
&lt;p&gt;This does not mean being excessively politically correct or losing your sense of humor. It does mean that if you would not make a certain joke or statement in front of a more diverse group of people, you might want to avoid saying it to your programmer buddies. It also means that you should check your cultural assumptions about others at the door, and focus instead on what people can do rather than what makes them different from you.&lt;/p&gt;
&lt;p&gt;Most of the advice I&amp;#8217;ve given in this article will naturally create an environment that is more inviting to a broader-based community than the one we&amp;#8217;re currently serving, but I wanted to call out this issue in particular as being one that is too important to ignore. Organizers of communities need to specifically keep these issues in mind, as they are the ones that have the best chance at setting expectations for what is expected of group members.&lt;/p&gt;
&lt;p&gt;People work and learn best in an environment in which they feel safe, welcome, and appreciated. If each member in your group values that sort of environment, you&amp;#8217;ll end up achieving more as a group than you would in a group where some folks feel marginalized or unappreciated.&lt;/p&gt;
&lt;h3&gt;Questions?&lt;/h3&gt;
&lt;p&gt;I care a lot about these ideas, and would love to help spread them beyond the digital walls of Mendicant University and out into the world. If you want to start up your own group based on these ideas or want some help incorporating them into a group you&amp;#8217;re currently organizing, I&amp;#8217;d be happy to help. You can leave a comment here, or you can even catch up with me over Skype. Send an email to gregory.t.brown@gmail.com with your availability in &lt;span class="caps"&gt;UTC&lt;/span&gt; time and I will be happy to meet with you.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;If you enjoyed this post, please support my efforts by subscribing to the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby Journal&lt;/a&gt;. For only $8/month, you&amp;#8217;ll have access to tons of great content and an active group of dedicated Ruby learners. The proceeds from this project help fund my efforts on &lt;a href="http://mendicantuniversity.org"&gt;Mendicant University&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 06 Jan 2012 18:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/062-practicing-programming-group.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/062-practicing-programming-group.html</guid></item><item><title>Issue 1.26: Structural Design Patterns</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on February 28, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;In this two part series, I&amp;#8217;ve been looking at the classical design patterns laid out by the Gang of Four and exploring their relevance to modern day Ruby programming. Rather than teaching the design patterns themselves, my goal is to give practical examples using real code so that you can consider what the strengths and weaknesses of these techniques are.&lt;/p&gt;
&lt;p&gt;In this issue, I&amp;#8217;ll be focusing on the structural design patterns, all of which have to do with the relationships between clusters of code. The seven patterns listed below are the ones we&amp;#8217;ll focus on in this article.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Adapter&lt;/li&gt;
	&lt;li&gt;Bridge&lt;/li&gt;
	&lt;li&gt;Composite&lt;/li&gt;
	&lt;li&gt;Proxy&lt;/li&gt;
	&lt;li&gt;Decorator&lt;/li&gt;
	&lt;li&gt;Facade&lt;/li&gt;
	&lt;li&gt;Flyweight&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An important thing to keep in mind is that while the original GoF book was written with C++ and Smalltalk examples, none of the patterns were written with Ruby&amp;#8217;s semantics in mind. This makes it necessary to use a little poetic license to explore how these patterns apply in Ruby. That having been said, the examples we&amp;#8217;ll look at attempt to preserve the spirit of the patterns they implement even if they&amp;#8217;re not semantically identical.&lt;/p&gt;
&lt;h3&gt;Adapter&lt;/h3&gt;
&lt;p&gt;An &lt;a href="http://en.wikipedia.org/wiki/Adapter_pattern"&gt;Adapter&lt;/a&gt; is used when you want to provide a unified interface to a number of different objects that implement similar functionality. This pattern is easy to spot in the wild for a Ruby developer, as most of us have to use ActiveRecord in our day to day work. Under the hood, ActiveRecord talks to a large amount of different databases, but wraps them up in a common interface its implementation code can avoid worrying about their individual differences. This is exactly the sort of thing the Adapter pattern is useful for.&lt;/p&gt;
&lt;p&gt;Increasingly, Rubyists are finding this pattern to be useful in other settings as well. In particular, things like Intridea&amp;#8217;s &lt;a href="https://github.com/intridea/multi_json"&gt;multi_json&lt;/a&gt; gem allow libraries and applications to be built against an abstract interface rather than requiring some particular &lt;span class="caps"&gt;JSON&lt;/span&gt; library that might conflict with other dependencies. Inspired by the ideas behind multi_json, a Mendicant University student (Mitko Kostov) built a similar adapter library for Markdown processors called &lt;a href="https://github.com/mytrile/marky"&gt;Marky&lt;/a&gt;. The base implementation is very simple, and gives us a good opportunity to look at one way to implement an Adapter in Ruby from the ground up.&lt;/p&gt;
&lt;p&gt;The basic idea is that we want to be able to use a common interface while easily configuring which backend is used under the hood. The following example shows how Marky might be used.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
# using RDiscount as a backend
Marky.adapter = :rdiscount

Marky.to_html("hello world") #=&amp;gt; "&amp;lt;p&amp;gt;hello world&amp;lt;/p&amp;gt;

# using BlueCloth as a backend
Marky.adapter = :bluecloth

Marky.to_html("hello world") #=&amp;gt; "&amp;lt;p&amp;gt;hello world&amp;lt;/p&amp;gt;"
&lt;/pre&gt;
&lt;p&gt;To make this work, it is necessary to map the name of an adapter to a class which wraps the underlying engine and implements a &lt;tt&gt;to_html()&lt;/tt&gt; method. The code that actually wires up the adapter is shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Marky
  extend self

  def adapter
    return @adapter if @adapter
    self.adapter = :rdiscount
    @adapter
  end
   
  def adapter=(adapter_name)
    case adapter_name
    when Symbol, String
      require "adapters/#{adapter_name}"
      @adapter = Marky::Adapters.const_get("#{adapter_name.to_s.capitalize}")
    else
      raise "Missing adapter #{adapter_name}"
    end
  end

  def to_html(string)
    adapter.to_html(string)
  end
end
&lt;/pre&gt;
&lt;p&gt;While this uses a tiny bit of dynamic Ruby magic to look up the right module name, when we see the actual adapters, it all comes together.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
# adapters/bluecloth.rb

require "bluecloth"

module Marky
  module Adapters
    module Bluecloth
      extend self
      def to_html(string)
        ::BlueCloth.new(string).to_html
      end
    end
  end
end
&lt;/pre&gt;

&lt;pre name = "code" class = "ruby"&gt;
# adapters/rdiscount.rb

require "rdiscount"

module Marky
  module Adapters
    module Rdiscount
      extend self
      def to_html(string)
        ::RDiscount.new(string).to_html
      end
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;Since all the adapters implement a &lt;tt&gt;to_html()&lt;/tt&gt; method that share a common contract, &lt;tt&gt;Marky.to_html()&lt;/tt&gt; will work regardless of what adapter gets loaded. The win here is that if that libraries, applications and frameworks rely on adapters rather than concrete implementations, the choice of which engine to use can be done differently in different environments.&lt;/p&gt;
&lt;p&gt;While not every problem domain needs added level of indirection that Adapters introduce, they can come in handy when there are several competing implementations solving a common problem but you don&amp;#8217;t want to forced to choose one over the other.&lt;/p&gt;
&lt;h3&gt;Bridge&lt;/h3&gt;
&lt;p&gt;The idea behind a &lt;a href="http://en.wikipedia.org/wiki/Bridge_pattern"&gt;Bridge&lt;/a&gt; is to place a physical separation between the interface of a given type of object and its implementation. In languages in which the type system gets in the way, this kind of pattern is important for reducing the proliferation of various type permutations by making hierarchies of interfaces and implementations orthogonal to one another. If that sounds like academic jibberish to you, there is a &lt;a href="http://sourcemaking.com/design_patterns/bridge"&gt;SourceMaking article&lt;/a&gt; that might help sort the concepts out for you a bit.&lt;/p&gt;
&lt;p&gt;I had a hard time thinking through where this pattern has its place in Ruby, mainly because there is a built in separation of interface and implementation in Ruby via duck typing semantics. The best example I could come up with was to port the painfully complex C++ example that SourceMaking uses in their article to something that is a bit more natural looking in Ruby. Read it over, and think about what it might be gaining us as you go.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
##Concrete Implementations

class BasicTimeData
  def initialize(hour, minutes)
    @hour     = hour
    @minutes  = minutes
  end

  def formatted_output
    "Time is #{@hour}:#{@minutes}"
  end
end

class TimeWithMeridianData
  def initialize(hour, minutes, meridian)
    @hour     = hour
    @minutes  = minutes
    @meridian = meridian
  end

  def formatted_output
    "Time is #{@hour}:#{@minutes} #{@meridian}"
  end
end

##Bridge

module TimeFormatter
  def to_s
    @time_data.formatted_output
  end
end

## Abstract Objects linked to Concrete Implementations through Bridge

class BasicTime
  include TimeFormatter
  
  def initialize(*a, &amp;amp;b)
    @time_data = BasicTimeData.new(*a, &amp;amp;b)    
  end
end

class TimeWithMeridian
  include TimeFormatter

  def initialize(*a, &amp;amp;b)
    @time_data = TimeWithMeridianData.new(*a, &amp;amp;b)
  end
end

## Example Usage

time1  = BasicTime.new("10","30")
time2  = TimeWithMeridian.new("10","30","PM")

[time1, time2].each { |t| puts t }
&lt;/pre&gt;
&lt;p&gt;While it might just due to the nature of the example, I feel this code is still quite contrived, and that hides its benefits. The takeway here is mostly that it&amp;#8217;s possible for both &lt;tt&gt;BasicTimeData&lt;/tt&gt; and &lt;tt&gt;TimeWithMeridianData&lt;/tt&gt; to change without breaking &lt;tt&gt;BasicTime&lt;/tt&gt; and &lt;tt&gt;TimeWithMeridian&lt;/tt&gt;, as long as &lt;tt&gt;TimeFormatter&lt;/tt&gt; is updated. Similarly, a change in the needs of &lt;tt&gt;BasicTime&lt;/tt&gt; and &lt;tt&gt;TimeWithMeridian&lt;/tt&gt; will not affect the concrete implementations of the data structures, as long as the bridge provides the necessary wiring.&lt;/p&gt;
&lt;p&gt;The thing that I struggle with in this pattern is understanding what unique behaviors the &lt;tt&gt;BasicTime&lt;/tt&gt; and &lt;tt&gt;TimeWithMeridian&lt;/tt&gt; objects could implement that justify their existence. Is this pattern an artifact of static languages that isn&amp;#8217;t really needed in Ruby? Or am I just missing some important detail that could be cleared up with a good example or two? Feel free to leave a comment letting me know what you think.&lt;/p&gt;
&lt;h3&gt;Composite&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composite pattern&lt;/a&gt; is useful when you want to treat a group of objects as if it were a single, unified object. To explore this pattern, we can look at some experimental code I wrote for Prawn which was designed to make it possible to treat all objects drawn in the document as compositions of primitive &lt;span class="caps"&gt;PDF&lt;/span&gt; instructions. We can start with an example of rendering a curve, working from the outside in.&lt;/p&gt;
&lt;p&gt;When &lt;tt&gt;curve()&lt;/tt&gt; is called on a Prawn drawing, the necessary &lt;span class="caps"&gt;PDF&lt;/span&gt; content is generated as a side effect and the user does not need to do anything with the return value of that method. However, if someone wanted to play with the internals, they could call &lt;tt&gt;curve!()&lt;/tt&gt; instead and get themselves an object that implements a &lt;tt&gt;to_pdf()&lt;/tt&gt; method, as shown in the example below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
chunk = canvas.curve!(:point1 =&amp;gt; [100,100],
                      :point2 =&amp;gt; [50,50], 
                      :bound1 =&amp;gt; [60,90],
                      :bound2 =&amp;gt; [60,90])

puts chunk.to_pdf

...........................................................OUTPUTS..

  100.000 100.000 m
  60.000 90.000 60.000 90.000 50.000 50.000 c
&lt;/pre&gt;
&lt;p&gt;We can catch a glimpse of how this &lt;tt&gt;to_pdf()&lt;/tt&gt; method is actually implemented via several composed primitive objects by taking a look at the source for the &lt;tt&gt;curve!()&lt;/tt&gt; method.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def curve!(params)
  chunk(:curve, params) do |c|
    [ move_to!(:point =&amp;gt; c[:point1]),
      curve_to!(:point =&amp;gt; c[:point2],
                :bound1 =&amp;gt; c[:bound1],
                :bound2 =&amp;gt; c[:bound2]) ]

  end
end
&lt;/pre&gt;
&lt;p&gt;In the above, &lt;tt&gt;chunk()&lt;/tt&gt; is a helper method which builds a &lt;tt&gt;Prawn::Core::Chunk&lt;/tt&gt; object, which serves as the composite object in this system. We&amp;#8217;ll look at how chunks are implemented in a bit, but note that the block given to the &lt;tt&gt;chunk()&lt;/tt&gt; method defines what the individual chunk is composed of. In this case, a curve consists of two subchunks, one responsible for generating a move instruction, and one for rendering a curve from the current drawing location to another point. The definitions for both of those methods are shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def move_to!(params)
  chunk(:move_to, params) do |c|
    raw_chunk("%.3f %.3f m" % c[:point])
  end
end

def line_to!(params)
  chunk(:line_to, params) do |c|
    raw_chunk("%.3f %.3f l" % c[:point])
  end
end
&lt;/pre&gt;
&lt;p&gt;Similar to &lt;tt&gt;chunk!()&lt;/tt&gt;, these methods can called directly, producing an object with a &lt;tt&gt;to_pdf()&lt;/tt&gt; method, as shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
chunk = canvas.move_to!(:point =&amp;gt; [100,100])
puts chunk.to_pdf

chunk2 = canvas.curve_to!(:point =&amp;gt; [50,50],
                          :bound1 =&amp;gt; [60,90],
                          :bound2 =&amp;gt; [60,90])

puts chunk2.to_pdf

# combined output is identical to calling curve!() directly.
&lt;/pre&gt;
&lt;p&gt;Through these two methods, we encounter the leaf nodes in our composition, &lt;tt&gt;Prawn::Core::RawChunk&lt;/tt&gt; objects. These objects are where the actual text that is in our &lt;tt&gt;to_pdf()&lt;/tt&gt; is stored. With that in mind, we can now look at the actual objects that this graphics system is built on.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Prawn
  module Core
    class RawChunk
      def initialize(content)
        @content = content
        @command = :raw_pdf_text
        @params = {}
      end

      attr_accessor :content, :command, :params
      alias_method :to_pdf, :content
    end

    class Chunk
      def initialize(command, params={}, &amp;amp;action)
        @command = command
        @params = params
        @action = action
      end

      attr_reader :command, :params, :action

      def content
        action.call(self)
      end

      def to_pdf
        case results = content
        when Array
          results.map { |sub_chunk| sub_chunk.to_pdf }.join("\n")
        when Prawn::Core::Chunk, Prawn::Core::RawChunk
          results.to_pdf
        else
          raise "Bad Chunk: #{results.class} not supported"
        end
      end
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;From this we see that a chunk&amp;#8217;s content can be an array of children, a chunk, or a raw chunk object. The &lt;tt&gt;to_pdf()&lt;/tt&gt; method is responsible for traversing downwards through the composition until it reaches the raw chunk objects which simply return the raw content. But because the APIs match, we can look at the chunks at any level of the system and burrow down to their raw data.&lt;/p&gt;
&lt;p&gt;While this might be a bit of an intense example, it shows how the Composite pattern can be used to make a complex set of objects look and feel as if they were a single unified entity. It may be a bit much to take in on a first glance, but try to think of how you could apply these ideas to your own code and you might gain some useful insights.&lt;/p&gt;
&lt;h3&gt;Proxy&lt;/h3&gt;
&lt;p&gt;A &lt;a href="http://en.wikipedia.org/wiki/Proxy_pattern"&gt;Proxy&lt;/a&gt; is any object that acts as a drop-in replacement object that does a bit of work and then delegates to some other underlying object. This is another pattern that&amp;#8217;s easy to recognize for Rails developers, because it is used extensively in ActiveRecord associations support. The following example shows a typical interaction between a base model and one of its associations.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
quiz = Quiz.first
quiz.questions.class #=&amp;gt; Array

quiz.questions.count #=&amp;gt; 10

quiz.questions.create(:question_text =&amp;gt; "Who is the creator of Ruby?",
                      :answer        =&amp;gt; "Matz")

quiz.questions.count #=&amp;gt; 11

quiz.questions[-1].answer #=&amp;gt; "Matz" 
&lt;/pre&gt;
&lt;p&gt;While the questions association claims to be an array, it also provides some of the common helpers found in &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt;. If we stick to the core idea and ignore some of the Rails specific details, creating such an association proxy is fairly easy to do using Ruby&amp;#8217;s delegate standard library. The code below more or less does the trick.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "delegate"

class Quiz
  def questions
    @questions                  ||= HasManyAssociation.new([])
    @questions.associated_class ||= Question

    @questions
  end
end

class Question
  def initialize(params)
    @params = params
  end

  attr_reader :params

  def answer
    params[:answer]
  end
end

class HasManyAssociation &amp;lt; DelegateClass(Array)
  attr_accessor :associated_class

  def initialize(array)
    super(array)
  end

  def create(params)
    self &amp;lt;&amp;lt; associated_class.new(params)
  end
end

quiz = Quiz.new

# grab the proxy object
questions = quiz.questions


# use custom create() method on proxy object

questions.create(:question_text =&amp;gt; "Who is the creator of Ruby?",
                 :answer        =&amp;gt; "Matz")
questions.create(:question_text =&amp;gt; "Who is the creator of Perl?",
                 :answer        =&amp;gt; "Larry Wall")


# use array like behavior on proxy object

p questions[0].answer #=&amp;gt; "Matz"
p questions[1].answer #=&amp;gt; "Larry Wall"
p questions.map { |q| q.answer }.join(", ") #=&amp;gt; "Matz, Larry Wall"
&lt;/pre&gt;
&lt;p&gt;Interestingly enough, while Ruby provides a standard library for building Proxy objects, most people tend to implement them in a different way, through the use of an explicit &lt;tt&gt;method_missing()&lt;/tt&gt; hook on a blank slate object such as Ruby 1.9&amp;#8217;s BasicObject. For example, we could have written our HasManyAssociation code in the manner shown below and things would still work as expected.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class HasManyAssociation &amp;lt; BasicObject
  attr_accessor :associated_class

  def initialize(array)
    @array = array
  end

  def create(params)
    self &amp;lt;&amp;lt; associated_class.new(params)
  end

  def method_missing(*a, &amp;amp;b)
    @array.send(*a, &amp;amp;b)
  end
end
&lt;/pre&gt;
&lt;p&gt;Without looking at the source, I&amp;#8217;m almost sure that Rails does something similar to this, because doing some_association.class returns Array rather than the name of the proxy object. This is the only noticeable difference on the surface between this approach and the DelegateClass approach.&lt;/p&gt;
&lt;p&gt;Personally, I&amp;#8217;ve written proxies in both ways, and I tend to prefer the &lt;tt&gt;DelegateClass()&lt;/tt&gt; approach slightly, simply because it&amp;#8217;s more explicit and doesn&amp;#8217;t require me to explicitly define a &lt;tt&gt;method_missing()&lt;/tt&gt; hook. But on the other hand, we can see that rolling your own proxy implementation is trivial in Ruby, and some may prefer the self contained nature of doing the delegation work yourself. It&amp;#8217;d be interesting to hear what readers have to say on this topic, so please feel free to post to the mailing list if you prefer one approach over the other.&lt;/p&gt;
&lt;h3&gt;Decorator&lt;/h3&gt;
&lt;p&gt;While there is a more clear distinction between a &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorator&lt;/a&gt; and a Proxy in static languages, in Ruby the two concepts almost merge, except that a Decorator is used almost exclusively for the purpose of adding / extending behavior of a target object, and a Proxy is a more general concept.&lt;/p&gt;
&lt;p&gt;Since I&amp;#8217;ve already written up a cool example of using decorators on this blog, I think what I&amp;#8217;ll do is point you over there in the interest of keeping this article from being even more incredibly long than it already is. Check out the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/008-decorator-delegator-disco.html
"&gt;Decorator Delegator Disco&lt;/a&gt; if you want to see some interesting code samples that implement this pattern.&lt;/p&gt;
&lt;h3&gt;Facade&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Facade_pattern "&gt;Facade pattern&lt;/a&gt; simplifies how users interact with a codebase by implementing an interface that hides many implementation details for the most common behaviors needed by consumers. Looking at it from the outside, a perfect example of this is the open-uri standard library. When using open-uri, it is possible to do a simple &lt;span class="caps"&gt;HTTP&lt;/span&gt; get request with just a single line of code, as shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "open-uri"

puts open("http://www.google.com").read
&lt;/pre&gt;
&lt;p&gt;The purpose of open-uri is to make reading a resource via an &lt;span class="caps"&gt;HTTP&lt;/span&gt; &lt;span class="caps"&gt;GET&lt;/span&gt; request look and feel like opening an ordinary file. This is a very common use case, so open-uri makes it easy to work with. To see what this facade is hiding, we can write the equivalent code using the libraries it wraps, &lt;tt&gt;Net::&lt;span class="caps"&gt;HTTP&lt;/span&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;span class="caps"&gt;URI&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require 'net/http'
require 'uri'

url = URI.parse('http://www.google.com')

res = Net::HTTP.start(url.host, url.port) {|http|
  http.get('/index.html')
}

puts res.body
&lt;/pre&gt;
&lt;p&gt;While the code above isn&amp;#8217;t especially difficult to read, it certainly feels like more work than the previous example. This is primarily because of the flexibility and functionality tradeoff between the two approaches. The open-uri library can afford to be much more high level and limited in scope because its sole purpose is to help make a single particular task easier. On the other hand, &lt;tt&gt;Net::&lt;span class="caps"&gt;HTTP&lt;/span&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;span class="caps"&gt;URI&lt;/span&gt;&lt;/tt&gt; are both complex tools that can be used for a number of different things. The use of the Facade pattern allows for both kinds of objects to coexist peacefully within a single system.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s worth keeping in mind that pretty much every &lt;span class="caps"&gt;DSL&lt;/span&gt; you encounter is a Facade of some form or another. If you&amp;#8217;re interested in seeing how simple interfaces can mask highly complex networks of objects, consider doing a source dive into your favorite tool that utilizes a domain specific language, such as Rake, RSpec, or Sinatra. You&amp;#8217;ll find a number of different techniques at work depending on which project you explore, but all have the common characteristic of providing a simplified way to interact with a deep system.&lt;/p&gt;
&lt;h3&gt;Flyweight&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Flyweight_pattern"&gt;Flyweight pattern&lt;/a&gt; is a way to represent what would seem like a large amount of data in a lightweight way. This is one of those patterns that mostly goes away in Ruby due to built in language constructs, but it&amp;#8217;s worth taking a look at just for the sake of completeness.&lt;/p&gt;
&lt;p&gt;The wikipedia article linked above discusses font metrics as a common application of the flyweight pattern, in which you may want to associate each character in a string with a large amount of information describing how that character should be rendered. The basic idea is that it&amp;#8217;d be far too inefficient memory-wise to create a new instance of font metrics data for every character in a document. So instead, using the Flyweight pattern, it is possible to map the index of characters in a string to a single instance for each unique character, vastly reducing the amount of memory consumed. This is a problem I&amp;#8217;ve actually had to solve before within Prawn, but it&amp;#8217;s a bit of a tough one demonstrate briefly if we attempt to show real code.&lt;/p&gt;
&lt;p&gt;However, if we stub out the actual font metrics generation, it&amp;#8217;s easy to see that this problem can be solved by wrapping a simple Ruby hash that has an initializer block.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class FontMetrics

  def initialize(string)
    @string = string
  end

  def [](index)
    glyph_data[@string[index]]
  end

  def glyph_data
    @glyph_data ||= Hash.new { |h,k| h[k] = metrics_for(k) }
  end

  # stubbed out, but would typcially refer to something
  # largish and time consuming to generate
  # 
  def metrics_for(k)
    { :bbox =&amp;gt; [rand(100), rand(100), rand(100), rand(100)] }
  end

end

string = "Hello World"

metrics = FontMetrics.new(string)

p metrics[0]  #=&amp;gt; {:bbox=&amp;gt;[86, 44, 88, 31]}
p metrics[2]  #=&amp;gt; {:bbox=&amp;gt;[52, 7, 38, 98]}
p metrics[3]  #=&amp;gt; {:bbox=&amp;gt;[52, 7, 38, 98]}
p metrics[-2] #=&amp;gt; {:bbox=&amp;gt;[52, 7, 38, 98]}

p metrics[2].object_id == metrics[3].object_id #=&amp;gt; true

p metrics[0] == metrics[1] #=&amp;gt; false
p metrics[2] == metrics[3] #=&amp;gt; true
&lt;/pre&gt;
&lt;p&gt;From the above code, we see that the &lt;tt&gt;FontMetrics&lt;/tt&gt; object gives the appearance of having data for each and every character in the string, but checking the &lt;tt&gt;object_id&lt;/tt&gt; for each proves that only one object per unique character has been created. While I suppose we could call this a Flyweight, I think that in Ruby we&amp;#8217;d just say that we were caching the metrics data using a hash with an initializer. But perhaps using this vocabulary wouldn&amp;#8217;t hurt, if we want to keep our minds focused on the high level concepts.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;After writing two articles on the the topic, I&amp;#8217;m finding myself getting sick of design patterns. But when I look back on the code I&amp;#8217;ve shared with you, I realize that when I built these things, I never really thought consciously about what pattern they were, and it took me until the time of writing this article to put a name on them.&lt;/p&gt;
&lt;p&gt;A major concern I have about classical patterns is that in order to see the resemblance of the code I&amp;#8217;ve shared to the original GoF patterns, you really need to look at things sideways and squint a bit. It&amp;#8217;d be great for me to be able to just say &amp;#8216;Use a flyweight here&amp;#8217; and have it mean something, but if you say that to someone without a strong background in Ruby, you may end up with hundreds of lines of a Java-esque monstrosity.&lt;/p&gt;
&lt;p&gt;To be sure, I&amp;#8217;m not saying that this exploration has not been worthwhile. Forcing myself to think of how many of the classic GoF patterns might materialize themselves in Ruby has been a very interesting experience, because it&amp;#8217;s really making me think about how we build and design our code. But the problem of whether we can actually have a common vocabulary about concepts that really get distorted in translation makes me wonder about the merits of stacking up pattern definitions, at least without giving them new names.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m quite curious about what folks have been thinking as they read through the last two articles, in particular, I wonder if seeing me try to attack patterns from a purely pragmatic perspective has changed anything about the way readers look at these concepts. I&amp;#8217;m also kind of curious if &amp;#8216;that guy&amp;#8217; is out there, silently thinking to himself &amp;#8220;Well&amp;#8230; actually&amp;#8221; after seeing my somewhat liberal interpretation of these classic patterns. Please leave a comment letting me know what you think!&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt; (2011-12-27): If you want to keep receiving great content like this on a regular basis, please sign up for the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby Journal&lt;/a&gt;. It is like the original Practicing Ruby newsletter but much more polished.&lt;/b&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 27 Dec 2011 18:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/060-issue-26-structural-design-patterns.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/060-issue-26-structural-design-patterns.html</guid></item><item><title>Issue 1.25: Creational Design Patterns</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on February 22, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;In this issue and the next one, I look at several design patterns laid out by the Gang of Four and explore their relevance to modern day Ruby programming. My goal is not so much to teach the design patterns themselves, but instead give practical examples using real code so that you can consider what their strengths and weaknesses are.&lt;/p&gt;
&lt;p&gt;In this article, I focus on the creational design patterns, all of which have to do with instantiating new objects. They are listed below in no particular order:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Singleton&lt;/li&gt;
	&lt;li&gt;Multiton&lt;/li&gt;
	&lt;li&gt;Factory Method&lt;/li&gt;
	&lt;li&gt;Abstract Factory&lt;/li&gt;
	&lt;li&gt;Builder&lt;/li&gt;
	&lt;li&gt;Prototype&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An important thing to keep in mind is that while the original GoF book was written with C++ and Smalltalk examples, none of the patterns were written with Ruby&amp;#8217;s semantics in mind. This makes it necessary to use a little poetic license to explore how these patterns apply in Ruby. That having been said, the examples we&amp;#8217;ll look at attempt to preserve the spirit of the patterns they implement even if they&amp;#8217;re not semantically identical.&lt;/p&gt;
&lt;h3&gt;Singleton&lt;/h3&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011-12-20: Be sure to check out what &lt;a href='http://practicingruby.com/articles/shared/jleygxejeopq'&gt;Practicing Ruby 2.8&lt;/a&gt; has to say about implementing Singleton pattern in Ruby. My feelings on this have changed over time&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Singleton_pattern"&gt;Singleton pattern&lt;/a&gt; is used in situations where a single instance of a class is all you need. Singleton objects are meant provide an effective way of organizing global state and behavior, such as configuration data, logging support, or other similar needs. This pattern is common enough that Ruby provides a module in its standard library that is meant to make it easier to implement.&lt;/p&gt;
&lt;p&gt;In the code below, I&amp;#8217;ve implemented a simple logger using Ruby&amp;#8217;s singleton library, which on the surface should look quite like an ordinary Ruby class definition.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "singleton"

class SimpleLogger
  include Singleton

  def initialize
    @output = []
  end

  attr_reader :output

  def error(message)
    output &amp;lt;&amp;lt; formatted_message(message, "ERROR")
  end

  def info(message)
    output &amp;lt;&amp;lt; formatted_message(message, "INFO")
  end

  def write(filename)
    File.open(filename, "w") { |f| f &amp;lt;&amp;lt; output.join("\n") }
  end

  private

  def formatted_message(message, message_type)
    "#{Time.now} | #{message_type}: #{message}"
  end

end
&lt;/pre&gt;
&lt;p&gt;Nothing here looks out of the ordinary, but you can begin to see the impact of the Singleton mixin as soon as you try to create a SimpleLogger instance.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; logger = SimpleLogger.new
NoMethodError: private method `new' called for SimpleLogger:Class
  from (irb):2
&lt;/pre&gt;
&lt;p&gt;Since the purpose of the singleton pattern is to allow for the creation of only a single instance, it makes sense that you might not be able to expect construction to work as normal. The example below demonstrates what you need to do instead.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
logger = SimpleLogger.instance

logger.error("Some serious problem")
logger.info("Something you might want to know")
logger.write("log.txt")
&lt;/pre&gt;
&lt;p&gt;The class method &lt;tt&gt;instance()&lt;/tt&gt; gets added by the Singleton mixin, and takes responsibility for initializing the SimpleLogger object. The first time this method is called, an instance of SimpleLogger is created, and initialize gets called as usual. Each subsequent call refers to that exact instance, preventing additional instances of this class from being created.&lt;/p&gt;
&lt;p&gt;At this point, you might be wondering what the advantage of this approach is over just using ordinary class definitions and then assigning an object to a global variable, such as in the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
$LOGGER = SimpleLogger.new
$LOGGER.error("Some serious problem")
$LOGGER.info("Something you might want to know")
&lt;/pre&gt;
&lt;p&gt;This is a reasonable question to ask, because this approach is just as straightforward and has similar strengths and weaknesses. If Ruby did not have an open class system, you could argue that it&amp;#8217;s less likely that you&amp;#8217;d run into side effects with different definitions of SimpleLogger than you would with someone reassigning $&lt;span class="caps"&gt;LOGGER&lt;/span&gt;, but that clearly does not apply here. It does seem like it&amp;#8217;d be marginally more likely to see a variable reassigned than a class stomped on, particularly if proper namespacing was used, but it&amp;#8217;s important to notice that in Ruby, this particular benefit of Singleton objects is marginal at best.&lt;/p&gt;
&lt;p&gt;The real benefit that using the Singleton pattern provides in Ruby over its alternatives is that instantiation is lazy evaluated, and enforces the single instance limitation. The former provides a potential performance and memory bonus when the object never ends up getting used, and the latter helps prevent accidental object creation. Both of these things are nice to have, and it only takes a bit of extra effort make them happen.&lt;/p&gt;
&lt;p&gt;The standard library implementation of the Singleton pattern is reasonable, and fairly clever. However, I feel it probably leans a bit too closely to a direct translation, and instead typically use a different technique in my own code. Below you can see how I might write SimpleLogger if left to my own devices.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module SimpleLogger
  extend self

  def error(message)
    output &amp;lt;&amp;lt; formatted_message(message, "ERROR")
  end

  def info(message)
    output &amp;lt;&amp;lt; formatted_message(message, "INFO")
  end

  def write(filename)
    File.open(filename, "w") { |f| f &amp;lt;&amp;lt; output.join("\n") }
  end

  private

  def formatted_message(message, message_type)
    "#{Time.now} | #{message_type}: #{message}"
  end

  def output
    @output ||= []
  end
end
&lt;/pre&gt;
&lt;p&gt;This code preserves the lazy evaluation component while removing the concept of an instance entirely. The module itself becomes a singleton object, allowing you to call methods directly on it which affect the state stored on the module.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
SimpleLogger.error("Some serious problem")
SimpleLogger.info("Something you might want to know")
SimpleLogger.write("log.txt")
&lt;/pre&gt;
&lt;p&gt;As an experienced Rubyist, I like to use this approach because what I end up with is truly a single object, rather than a class who&amp;#8217;s job is to provide a single instance. I also like to be able to call my singleton methods directly on that object without having to retrieve the singular instance.&lt;/p&gt;
&lt;p&gt;The downside of this code, and the reason why I showed both approaches instead of just my preferred one, is that it requires a much greater amount of Ruby knowledge to actually understand how it works. Additionally, through the loss of the &lt;tt&gt;initialize()&lt;/tt&gt; hook, you need to either do something like create a setup method that you explicitly call before any other methods, or do lazy initialization of all data as I&amp;#8217;ve done in the &lt;tt&gt;output()&lt;/tt&gt; method. These idioms are fairly common, but again, take you a bit farther away from the look and feel of an &amp;#8216;ordinary class definition&amp;#8217;, which may be offputting to some.&lt;/p&gt;
&lt;p&gt;No matter which approach you choose, it&amp;#8217;s worth keeping in mind that the Singleton pattern should be used in moderation. Due to Ruby&amp;#8217;s open class system, singleton objects are essentially nothing more than overengineered global variables. As with any form of global state, this makes them more difficult to test, easier to corrupt, and harder to isolate when it comes time to debug issues with them. However, when used sparingly and for the right reasons, they can be a good tool for managing global state and interactions.&lt;/p&gt;
&lt;h3&gt;Multiton&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Multiton"&gt;Multiton pattern&lt;/a&gt; is an extension to the Singleton pattern which maps unique keys to specific instances of a class. The idea is that there should only be one instance of an object for each unique key in use, limiting the number of objects that need to be created. For a practical example, we can look at some code in the &lt;span class="caps"&gt;PDF&lt;/span&gt; generation library Prawn which implements this sort of behavior.&lt;/p&gt;
&lt;p&gt;As a &lt;span class="caps"&gt;PDF&lt;/span&gt; generation library that operates at the very low level, we need to process and utilize font metrics information extensively. Because font files can become quite large, the processing cost of initializing one of our Font objects can be very computationally expensive. However, since this information is unique for each font that we use, there is no need to re-instantiate fonts once they have been initialized.&lt;/p&gt;
&lt;p&gt;While the actual source code is more complex than what I&amp;#8217;ll show here, the basic idea of applying a multiton here is quite simple. We can start off by assuming that our &lt;tt&gt;Font&lt;/tt&gt; class has an ordinary constructor which takes a font file and then processes it into a meaningful dataset. Initializing a font directly then looks something like this.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
font = Font.new("/path/to/times.ttf")
&lt;/pre&gt;
&lt;p&gt;On top of this basic interface, we layer a mapping mechanism that when used, looks something like the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
Font.map("Times New Roman" =&amp;gt; "/path/to/times.ttf")
Font["Times New Roman"] #=&amp;gt; A font instance
&lt;/pre&gt;
&lt;p&gt;To bring all of the above together, we can take a look at a skeletal &lt;tt&gt;Font&lt;/tt&gt; class which implements the Multiton pattern.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Font
  class &amp;lt;&amp;lt; self
    def file_names
      @file_names ||= {}
    end

    def instances
      @instances ||= {}
    end

    def map(params)
      file_names.update(params)
    end

    def [](name)
      instances[name] ||= new(file_names[name])
    end
  end

  def initialize(filename)
    puts "processing #{filename}"
  end

  # details omitted
end
&lt;/pre&gt;
&lt;p&gt;While the mapping is a bit complex here because it&amp;#8217;s a two stage process, the core idea of the Multiton still shines through. What we have are lazy evaluated Font instances which do not get created until the first time &lt;tt&gt;Font[some_font_name]&lt;/tt&gt; is called. Each subsequent call will result in the original instance being returned rather than a new instance of Font being created.&lt;/p&gt;
&lt;p&gt;This basic pattern can be used any time you have a scenario in which each unique key can be mapped to exactly one object. This sort of structure can be a super effective caching technique, but should also be used with caution as it too introduces global state and added complexity that should not be taken lightly.&lt;/p&gt;
&lt;h3&gt;Factory Method&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Factory_method"&gt;Factory Method pattern&lt;/a&gt; is used for putting a layer of abstraction on top of object creation so that directly working with its constructor is no longer necessary. This process can lead to more expressive ways of building new objects, and can also allow for the creation of new objects without explicitly referencing their class.&lt;/p&gt;
&lt;p&gt;In a Mendicant University core skills course, one of our students (Carol Nichols) ran into a design issue that we were able to improve by introducing factory methods. She was building an &lt;tt&gt;AdjacencyMatrix&lt;/tt&gt; datastructure for storing graph data, and her original &lt;span class="caps"&gt;API&lt;/span&gt; looked something like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class AdjacencyMatrix
  def initialize(data, directed=false)
    #...
  end
end

undirected_matrix = AdjacencyMatrix.new(data) 
directed_matrix   = AdjacencyMatrix.new(data, true)
&lt;/pre&gt;
&lt;p&gt;Using boolean switches as arguments aren&amp;#8217;t especially expressive, and so the suggestion was quickly made to move towards a hash-based argument allowing for the following usage.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
undirected_matrix = AdjacencyMatrix.new(data) 
directed_matrix   = AdjacencyMatrix.new(data, :directed =&amp;gt; true)
&lt;/pre&gt;
&lt;p&gt;Generally speaking, this isn&amp;#8217;t a bad strategy, but the more we thought about it, the more we realized that there isn&amp;#8217;t really a need for a dynamic option here. Consumers would always know whether a graph they were trying to represent was directed or undirected at the time they constructed their object, and the default call without the &lt;tt&gt;:directed&lt;/tt&gt; argument still wasn&amp;#8217;t very expressive about this detail. After some discussion, we settled on the following design, which introduces the Factory method pattern.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class AdjacencyMatrix
  class &amp;lt;&amp;lt; self
    def undirected(data)
      new(data)
    end

    def directed(data)
      new(data,true)
    end

    private :new
  end

  def initialize(data, directed=false)
    #...
  end
end

undirected_matrix = AdjacencyMatrix.undirected(data) 
directed_matrix   = AdjacencyMatrix.directed(data)
&lt;/pre&gt;
&lt;p&gt;While this code does still have its original wart intact internally, the interface that consumers interact with has been greatly improved. By making the &lt;tt&gt;new()&lt;/tt&gt; method private at the class level, we force users to call the factory methods, and it becomes immediately clear what type of graph is being processed each time a new &lt;tt&gt;AdjacencyMatrix&lt;/tt&gt; is constructed.&lt;/p&gt;
&lt;p&gt;This type of scenario comes up often, and the use of factory methods can be used to simplify or at least hide the complexity of constructor calls by giving an expressive name to a certain way of constructing a given object. However, sometimes factories go even farther by completely decoupling the object creation process from the class of the object being created. While I won&amp;#8217;t go into much detail about this much less common use case, we see this sort of factory very often in test code, particularly when dealing with object relational mappers in web applications.&lt;/p&gt;
&lt;p&gt;The following example from &lt;tt&gt;factory_girl&lt;/tt&gt; demonstrates how code which references no particular class at all can be used to instantiate records via an &lt;span class="caps"&gt;ORM&lt;/span&gt; framework such as ActiveRecord.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
factory :user do
  first_name 'John'
  last_name  'Doe'
  admin false
end

FactoryGirl.build(:user) #=&amp;gt; an instance of the User model
&lt;/pre&gt;
&lt;p&gt;I won&amp;#8217;t go into the details about how this works, but it should serve as food for thought, and perhaps would be a good project to &lt;a href="https://github.com/thoughtbot/factory_girl"&gt;dig into the source&lt;/a&gt; so that you can get a sense of the power that factory methods can have in simplifying the object creation process while making it more flexible.&lt;/p&gt;
&lt;h3&gt;Abstract Factory&lt;/h3&gt;
&lt;p&gt;Due to the flexible nature of Ruby&amp;#8217;s type system, we don&amp;#8217;t need an actual language construct for abstract classes. With that in mind, when we note that an &lt;a href="http://en.wikipedia.org/wiki/Abstract_factory"&gt;Abstract Factory&lt;/a&gt; is simply an abstract interface for concrete Factory objects to conform to, this pattern pretty much falls away. This is perhaps easier to show than to explain, so I will go ahead and build out a Ruby version of the example shown in the wikipedia article I&amp;#8217;ve linked to above.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module OSXGuiToolkit
  extend self

  def button
    Button.new
  end
  
  class Button
    def paint
      puts "I'm an OSX Button"
    end
  end
end

module WinGuiToolkit
  extend self
  
  def button
    Button.new
  end

  class Button
    def paint
      puts "I'm a WINDOWS button"
    end
  end
end

class Application
  def initialize(gui_toolkit)
    button = gui_toolkit.button
    button.paint
  end
end

# this check is a very quick hack, not reliable.
if PLATFORM =~ /darwin/
  Application.new(OSXGuiToolkit)
else
  Application.new(WinGuiToolkit)
end
&lt;/pre&gt;
&lt;p&gt;In this example, you&amp;#8217;ll see that we&amp;#8217;ve eliminated the explicit Abstract Factory interface. Instead, what we&amp;#8217;ve done is created two concrete object factories, &lt;tt&gt;OSXGuiToolkit&lt;/tt&gt; and &lt;tt&gt;WinGuiToolkit&lt;/tt&gt;, that implement a common &lt;span class="caps"&gt;API&lt;/span&gt;. We then create a simple Application stub class which shows that the &lt;span class="caps"&gt;GUI&lt;/span&gt; toolkit factory should be injected into the Application. The reason for this is seen in the final bit of code which determines which toolkit to use based on the platform the code is running on.&lt;/p&gt;
&lt;p&gt;The notion of using dependency injection in combination with object factories is an interesting one to me. I honestly don&amp;#8217;t find myself writing code like this often at all (and so couldn&amp;#8217;t share a relevant example of my own), but I&amp;#8217;m not sure if that&amp;#8217;s just because I don&amp;#8217;t think of it in times where it might be the right strategy to use. In any case, this hopefully demonstrates that the notion of an Abstract Factory in Ruby is a conceptual one that has no need for actual abstract classes or interface objects.&lt;/p&gt;
&lt;h3&gt;Builder&lt;/h3&gt;
&lt;p&gt;The purpose of the &lt;a href="http://en.wikipedia.org/wiki/Builder_pattern"&gt;Builder pattern&lt;/a&gt; is to create an abstract blueprint describing the steps of creating an object, and then allow many different implementations to actually carry out that process as long as they provide the necessary steps. This is once again a pattern that in Ruby is purely conceptual due to the lack of a need for explicit interfaces or abstract classes. However, unlike the Abstract Factory pattern, I have actually used the ideas behind builder in some real code of my own, and have an example handy that captures the spirit of the pattern.&lt;/p&gt;
&lt;p&gt;A while back, I was experimenting with coming up with some generalized domain language for producing output in a number of different formats. While I had more complex goals in mind, the basic usage ended up looking something like what you see below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class ListFormatter &amp;lt; Fatty::Formatter
  format :text do
    def render
      params[:data].map { |e| "* #{e}" }.join("\n")
    end
  end

  format :html do
    def render
      list_elements = params[:data].map { |e| "&amp;lt;li&amp;gt;#{e}&amp;lt;/li&amp;gt;" }.join
      "&amp;lt;ul&amp;gt;#{list_elements}&amp;lt;/ul&amp;gt;"
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;With some formats defined, the ListFormatter class can be used in the following manner.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
  data = %w[foo bar baz]
  [:html, :text].each do |format|
    puts ListFormatter.render(format, :data =&amp;gt; data)
  end
&lt;/pre&gt;
&lt;p&gt;&lt;span class="caps"&gt;OUTPUTS&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;
  * foo
  * bar
  * baz
  &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;foo&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;bar&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;baz&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;   
&lt;/pre&gt;
&lt;p&gt;While this all may look a bit magical on the surface, the implementation is just a few lines of relatively dull dynamic Ruby code.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Fatty
  class Formatter
    
    class &amp;lt;&amp;lt; self    
      def formats
        @formats ||= {}
      end
    
      def format(name, options={}, &amp;amp;block)
        formats[name] = Class.new(Fatty::Format, &amp;amp;block)
      end
        
      def render(format, params={})        
        format_obj = formats[format].new
        format_obj.params = params
        format_obj.render
      end   
    end
  end
  
  class Format
    attr_accessor :params 
  end
end
&lt;/pre&gt;
&lt;p&gt;On the surface, the bulk of this code dealing with formats looks like my Multiton example, albeit one that operates on anonymous Class objects. But instead of focusing on that detail, you should turn your attention to the &lt;tt&gt;render()&lt;/tt&gt;, which is where the builder process comes in.&lt;/p&gt;
&lt;p&gt;Each time render is called, a new instance of an anonymous Format subclass is created, and then customized. The params attribute is set and the render method is called to return the finally constructed object, our output data. As long as each format object that is implements all the required steps, they can be used interchangeably, which is the key feature that the Builder pattern emphasizes.&lt;/p&gt;
&lt;p&gt;This example perhaps would be more convincing if you took a look at it in its real setting, in which I do things like call a validations hook, mix in helper methods to the format instances, and otherwise perform more interesting operations. But since we&amp;#8217;re running on the long side already with this article, I&amp;#8217;ll invite you to investigate this on your own by checking out &lt;a href="https://github.com/sandal/fatty"&gt;the source code&lt;/a&gt; I wrote a couple years ago for this experiment.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s worth noting that this isn&amp;#8217;t a perfect example of Builder because I think the pattern doesn&amp;#8217;t apply directly to Ruby, and that the code I&amp;#8217;ve demonstrated is a bit of a hack. But it might be a good conversation starter at least for those who are interested in investigating this pattern further, since it tries to attack a problem that the Builder pattern would be well suited for in a static language.&lt;/p&gt;
&lt;h3&gt;Prototype&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;ve included a reference to the &lt;A href="http://en.wikipedia.org/wiki/Prototype_pattern"&gt;Prototype pattern&lt;/a&gt; here for completeness, but unfortunately am unsure whether or not it is remotely applicable to Ruby. The purpose of the Prototype pattern is to provide an alternative to manually constructing new objects by allowing for customization through the cloning of existing objects. Presumably this is a good idea when the setup of an object is more costly than it would be to create a copy of the initial post-processed data. However, I&amp;#8217;m coming up with a hard time seeing when this approach would be better than ordinary object composition combined with some side effects free operations done on the source data.&lt;/p&gt;
&lt;p&gt;I think what&amp;#8217;s interesting about the Prototype pattern is that it gives you a different way of looking at object oriented programming which allows you to envision an object system without needing the concept of classes. There are two languages I know of that are specifically designed to implement that kind of environment, &lt;a href="
http://en.wikipedia.org/wiki/Self_%28programming_language%29"&gt;Self&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Io_%28programming_language%29
"&gt;Io&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Because I feel well out of my depth when it comes to suggesting a good use of this pattern in Ruby, I welcome readers to submit their own ideas. Personally, I feel like this pattern just isn&amp;#8217;t a good fit for Ruby, but I could be wrong and would love to see evidence of my own ignorance! :)&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;Every example I&amp;#8217;ve shown in this article reflects a design approach that I feel is elegant compared to the obvious alternatives. But each and every one of them requires a lot of explanation and assume more than a fair bit of Ruby and object oriented programming knowledge. For this reason, it always baffles me when beginners or even some intermediate developers bother to learn about patterns at all. Since they don&amp;#8217;t really make sense to think about until you run into fairly complex problems, and when you do need to apply them you need to be very careful not to overengineer things, they seem like a tool to be used sparingly by strongly skilled developers rather than gratuitously by the masses.&lt;/p&gt;
&lt;p&gt;That said, I think that it&amp;#8217;s nice to be able to recognize a pattern when you see it, regardless what your level of experience is. It&amp;#8217;s also good to have at least a working knowledge of how to implement patterns so that when someone is discussing design with you, you can have a common vocabulary to work with. For these reasons, the study of patterns might be relevant to all active developers.&lt;/p&gt;
&lt;p&gt;While there are clearly lots of different ways to abstract object creation, there are even more ways to create interesting compositions of object clusters. In the next article we&amp;#8217;ll explore that topic by looking at the structural design patterns.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: If you want to keep receiving great content like this on a regular basis, please sign up for the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby Journal&lt;/a&gt;. It is like the original Practicing Ruby newsletter but much more polished.&lt;/b&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 20 Dec 2011 18:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/059-issue-25-creational-design-patterns.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/059-issue-25-creational-design-patterns.html</guid></item><item><title>How Mendicant University Works</title><description>&lt;p&gt;Back in June 2010 I announced via this blog &lt;a href="http://blog.rubybestpractices.com/posts/gregory/025-ruby-mendicant-university.html"&gt;my intentions to create a free online school&lt;/a&gt; called &lt;b&gt;Ruby Mendicant University&lt;/b&gt;. After a year and a half of hard work, the school is still alive and kicking. These days we call it &lt;b&gt;Mendicant University&lt;/b&gt; instead of &lt;b&gt;Ruby Mendicant University&lt;/b&gt;, but it&amp;#8217;s still very much a place designed to help Ruby programmers improve their craft. In this post I&amp;#8217;ll walk you through how our program works so that you can figure out whether it&amp;#8217;d be a good fit for you or someone you know.&lt;/p&gt;
&lt;p&gt;The entry point into the Mendicant University community is our core software development skills course, which we now offer three times a year. To qualify for this course, prospective students take an entrance exam which uses PuzzleNode problems to determine whether or not they are ready for the course. We try to make these puzzles as fun and interesting as we can so that the admissions process feels less like a chore and more like a learning experience.&lt;/p&gt;
&lt;p&gt;I review new applications as they come in, typically within a day or so. I spend 10-15 minutes looking over the submitted code and will occasionally make a decision right away whether to accept or reject the application. However, most applications are send for further review by our alumni to get their thoughts on whether the candidate looks like they&amp;#8217;re ready. This typically takes a day or two, but can be longer or shorter depending on the situation.&lt;/p&gt;
&lt;p&gt;Once admissions closes, we reach out to all applicants and give them an opportunity to schedule a code review session with one of our alumni, whether or not they were accepted into the core skills course. During this code review feedback is given on what could have been done better, as well as what the student can study in order to improve their skills. This helps accepted students identify their weak points so that they can be better prepared for their course, and helps those who were not accepted figure out what they&amp;#8217;d need to do in order to be better prepared for a future session with us. Many students who are not accepted the first time around end up staying involved with us through our mentoring program and &lt;span class="caps"&gt;IRC&lt;/span&gt; channel. Some have even gone on to be accepted into a later session after getting some guidance from us on how to study and practice Ruby programming. We love the fact that our entrance exam tends to draw people into our community rather than push them away, and we plan to keep it that way.&lt;/p&gt;
&lt;p&gt;Those who do get accepted to the core skills course soon learn that they have their work cut out for them. Before participating in the core skills session, each student is expected to propose an individual project which they will implement, clean up, and publicly release during their three week session. The student must also come up with a plan for contributing to an existing open source project during their session, choosing from a list of projects that we&amp;#8217;ve selected because of their friendliness to new contributors. Both the individual project and community service proposals need to be accepted before the course begins, but we have alumni mentors standing by to help students through the process.&lt;/p&gt;
&lt;p&gt;Once the session begins, three open-ended exercises are released. Students are expected to complete the functional requirements for two of these exercises, and must choose one to polish up nicely so that it meets the Mendicant University quality standard. Both the individual project and the community service project also need to meet our quality standards by the end of the course. We set several intermediate checkpoints to make sure these goals are met and that progress happens fairly evenly throughout the three week session. While the course is lot of work for our students and can be a tough balancing act when it comes to time management, the support we offer to our participants is nothing short of excellent.&lt;/p&gt;
&lt;p&gt;Shane Emmons and I divide up the teaching responsibilities for each course, taking turns providing code reviews, evaluations, and office hours for our students. We also have a total of four alumni mentors in each session to help our students with informal reviews and general questions. We tend to vary three of the positions while keeping MU staff member Andrea Singh on the mentoring team in each course, to get a nice balance between consistency and new insights. All in all we have 15 students in each course, and 6 people who are available to help them on demand, which is a pretty good ratio. On top of this, we tend to put students together into review groups so that they can benefit from the insights of their peers as well.&lt;/p&gt;
&lt;p&gt;The atmosphere of the course is similar to what you might find on a well functioning open source project. Code reviews are happening often via the mailing list, &lt;span class="caps"&gt;IRC&lt;/span&gt; channel, and our homegrown university-web software. Discussions about topics of general interest to Ruby developers happen organically, and as long as they aren&amp;#8217;t too much of a distraction add to the overall learning experience in the course. The projects are open-ended and so they can be discussed openly without worrying about spoilers, but because they overlap on theme the students still can interact with and help each other in meaningful ways without feeling like they are being pulled away from their own work.&lt;/p&gt;
&lt;p&gt;Those who make it through the session successfully are invited to join our alumni network. This is where they find that the core skills course was just a starting point, and that a great community full of folks with common interests is now available to them. While the Mendicant University alumni network is still something that we are actively developing and evolving as time goes on, it is where our students find opportunities to interact socially with one another, participate in projects, take additional courses, and optionally involve themselves in the forward development of Mendicant University itself. Most of our students contribute back to the program in some way, which is a sign that we must be doing something right.&lt;/p&gt;
&lt;p&gt;Underneath all of this, there is a hard to describe yet palpable feeling that what we are doing is more than just some sort of humdrum training program. Our students and staff are folks who care about a lot of different things, and they care enough about those things to want to improve themselves so that they can go out and improve the world in some way. While that will mean something different to every one of our community members, the spirit of courage, kindness, and generosity that our students have in common makes it a wonderful place to teach, and a wonderful place to grow.&lt;/p&gt;
&lt;p&gt;If you would like to join us, &lt;a href="http://university.rubymendicant.com/changelog/accepting-new-students-for-january-2012"&gt;please take our entrance exam for the January 2012 session&lt;/a&gt;. I&amp;#8217;d be happy to answer any questions you have, either via the comments on this blog, or via the #rmu channel on Freenode.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: I only blog here on relatively rare occasions. If you want to keep up with my projects and announcements, &lt;a href="http://twitter.com/seacreature"&gt;follow me on twitter here&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 02 Dec 2011 22:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/059-how-mendicant-university-works.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/059-how-mendicant-university-works.html</guid></item><item><title>Practicing Ruby Journal: Three months in and still going strong</title><description>&lt;p&gt;Three months ago today I relaunched my &lt;a href="http://practicingruby.com" target="_blank"&gt;subscription-based weekly Ruby journal&lt;/a&gt;. Over the last 13 weeks I&amp;#8217;ve shared my thoughts with over 250 subscribers and wrote close to 150 pages of content. The work I&amp;#8217;ve done on Practicing Ruby has better prepared me to teach at &lt;a href="http://university.rubymendicant.com" target="_blank"&gt;Mendicant University&lt;/a&gt; and vice-versa. Because this has been so much fun for me, and appears to be trending towards becoming a sustainable business, I will keep doing it!&lt;/p&gt;
&lt;p&gt;To celebrate the fact that this second generation of the Practicing Ruby has made it through a full quarter of a year, I&amp;#8217;ve decided to publicly release the three articles that my subscribers enjoyed the most. Go ahead and check them out if you want to see for yourself what the journal is like:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/ggcwduoyfqmz" target="_blank"&gt;Issue 2.4: Implementing Enumerable &amp;amp; Enumerator in Ruby&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/vbmlgkdtahzd" target="_blank"&gt;Issue 2.6: Learning new things step-by-step&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://practicingruby.com/articles/shared/iptocucwujtj" target="_blank"&gt;Issue 2.11: Domain specific &lt;span class="caps"&gt;API&lt;/span&gt; construction&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While reading these articles might give you a sense of the quality of the content I&amp;#8217;ve been producing, that&amp;#8217;s only part of the overall picture. Behind the scenes, you&amp;#8217;ll find that:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Many of the Practicing Ruby article lead to great conversations among folks who are serious about becoming better developers.&lt;/li&gt;
	&lt;li&gt;I am highly available to my readers, doing extra legwork where necessary to make sure their questions are answered and thoughts considered.&lt;/li&gt;
	&lt;li&gt;I am doing the kind of research that I was never able to do when I was spending most of my time doing consulting work, leading me to write about things not because they are easy, but because they are challenging and interesting.&lt;/li&gt;
	&lt;li&gt;All of the back issues are available to new subscribers, because I treat Practicing Ruby more like a subscription to a digital library and less like a newsletter.&lt;/li&gt;
	&lt;li&gt;I am not alone in running this project: &lt;a href="mailto:nancykotary@gmail.com" target="_blank"&gt;Nancy Kotary&lt;/a&gt; provides me with awesome copyediting and Mendicant Univerity co-founder &lt;a href="http://twitter.com/jordan_byron" target="_blank"&gt;Jordan Byron&lt;/a&gt; maintains the Rails app that runs this service.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you look even deeper than this, you&amp;#8217;ll find that similar to my work on Mendicant University, the Practicing Ruby journal is an experiment in social responsibility for me. In particular, I&amp;#8217;ve committed to do a few things that I think ought to make paying subscribers feel good for supporting this project:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;I offer the Practicing Ruby service freely for anyone who cannot afford to pay for it for any reason. All it takes is to email gregory.t.brown@gmail.com and let me know you need a free account. The stories my free subscribers have told me are amazing, but I&amp;#8217;ve also given subscriptions to people who have offered no explanation as to why they could not pay the full price of $8/month.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;I am committed to release all content freely within a reasonable period of time after it has been released to subscribers. Some articles I release immediately, but you can expect every article to become publicly available within a year of its original publication date. Because I write on topics that are not usually ephemeral in nature, the articles will have not lost their value by the time they are released under a free content license.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;I&amp;#8217;ve set up liberal sharing policies for my subscribers, making it easy for them to share articles with their friends if they&amp;#8217;d like.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;The whole point of this service is to allow me to sustain all the free work I do on Mendicant University and open source software while still creating something of value. I&amp;#8217;m not looking to get rich off of this service, I just want to be able to maintain my current standard of living and be able to provide for my family. In fact, the articles shared in this blog post are using that system!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;My hope is that over the next several months, more and more folks will join Practicing Ruby so that I can make both it and Mendicant University that much better. If I could reach 400-500 subscribers I&amp;#8217;d be able to sustain this project indefinitely while still being highly available to my readers. While that seemed a bit like a pie-in-the-sky dream when I first started this service, I&amp;#8217;ve made it halfway there in just three months.&lt;/p&gt;
&lt;p&gt;If I&amp;#8217;ve managed to convince you that the work I&amp;#8217;m doing is worthwhile and that you&amp;#8217;ll get a whole lot of high quality Ruby learning materials for just $8/month, please go ahead and &lt;b&gt;&lt;a href="http://practicingruby.com"&gt;subscribe to the Practicing Ruby journal now&lt;/a&gt;&lt;/b&gt;. I can promise you that it&amp;#8217;s much more than a paid blog/newsletter, and that you&amp;#8217;ll not experience anything quite like it anywhere else. If you&amp;#8217;re not sure yet whether it&amp;#8217;d be a good fit for you, I&amp;#8217;d be happy to answer any questions you have about the service either in the comments on this blog or via email at gregory.t.brown@gmail.com.&lt;/p&gt;
&lt;p&gt;I cannot thank the current Practicing Ruby subscribers and supporters from the community enough for giving me the unique opportunity to have an independent lifestyle that is focused on doing some good for the world. It is awesome that I get to spend my days doing what I love while helping others along the way. The Ruby community is really special in that way, and I feel lucky to be a part of it.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;If you want to keep up with my projects and announcements, &lt;a href="http://twitter.com/seacreature"&gt;follow me on twitter here&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 23 Nov 2011 22:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/058-practicing-ruby-after-three-months.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/058-practicing-ruby-after-three-months.html</guid></item><item><title>The Death and Rebirth of Practicing Ruby</title><description>&lt;p&gt;&lt;i&gt;tl;dr &amp;#8212; The original Practicing Ruby newsletter was cancelled after four months because I didn&amp;#8217;t realize that it could be anything more than a side project. Recently, I relaunched the service with a better sense of its true potential, and it&amp;#8217;s now better than ever. &lt;a href="http://practicingruby.com"&gt;Subscribe now&lt;/a&gt;, if you haven&amp;#8217;t already. It&amp;#8217;s $8/month and new articles are published every Tuesday.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;One of the benefits of spending most of my time teaching and mentoring intermediate software developers is that I don&amp;#8217;t need to guess at what they want to learn anymore, I just need to listen to their questions and pay attention to the problems they&amp;#8217;re running into. I realized this almost a year ago and launched a paid newsletter called &amp;#8220;Practicing Ruby&amp;#8221; with the goal of having a sustainable funding model to support my volunteer work on &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The newsletter quickly gained in popularity, with 191 subscribers joining within the first week. In less than 3 months, I was closing in on nearly 400 subscribers and the numbers were gradually climbing from week to week. With such great success early on, it came as a huge surprise to folks when I pulled the plug on the project less than four months after I wrote the first issue. In a lot of ways, the success of the project early on was exactly what killed it.&lt;/p&gt;
&lt;p&gt;To put it briefly, I wasn&amp;#8217;t ready to take Practicing Ruby seriously, even though I didn&amp;#8217;t realize it at the time. Mendicant University was still taking up a tremendous amount of my time because I had set an extremely ambitious set of goals for it. I was also transitioning away from doing freelance development work to a strictly project management / consulting role, and that transition was more time consuming than I expected it to be. Being spread so thin would have been enough to kill the project on its own, but I was also experiencing lots of problems with the service I was using to run the newsletter, including major issues with getting paid in a timely manner. All these things combined made me feel like I couldn&amp;#8217;t make the project as awesome as I wanted it to be, so I decided to kill it off rather than let it become mediocre.&lt;/p&gt;
&lt;p&gt;Because I thought the newsletter was dead for good, and because I like my all content to eventually become free, I decided to gradually release the 26 articles from the Practicing Ruby newsletter here on this blog. Once I did that, I came to realize that it wasn&amp;#8217;t just those folks who were dedicated enough to pay for my material that appreciate the work I put into these articles, but the broader Ruby community as well. This filled me with pangs of regret, which gradually wore me down.&lt;/p&gt;
&lt;p&gt;In July 2011 out of pure curiousity, I sent an email to the former Practicing Ruby subscribers asking if they would be interested in subscribing again if I relaunched the service. Within an hour, I had received dozens of emails that ranged from &amp;#8220;Hell Yeah&amp;#8221; to &amp;#8220;Yes, but only if you charge more!&amp;#8221;. To make a long story short, I made the necessary changes in my life to make it possible for me to start the service up again, and in August I quietly launched it again while promising myself that it would be much, much better than the first iteration.&lt;/p&gt;
&lt;p&gt;A month and a half later, I&amp;#8217;m now happy to announce that Practicing Ruby is back with a vengeance, and is much better than it ever was. With the help of Jordan Byron, I have built out a custom application for running the service, which we are continuously improving. New articles are published weekly via the web, and subscribers are notified via email when they go live. The benefit of this approach is that I&amp;#8217;m able to make revisions and improvements to the articles over time, and readers have access to the full archives which they can browse at their convenience. The articles and conversations about them are located in the same place, similar to a blog. However, the content itself is much deeper than what you&amp;#8217;d typically find on a blog, and is designed to be more like book-quality material that you can consume in a single setting.&lt;/p&gt;
&lt;p&gt;The real thing that sets Practicing Ruby apart from traditional blogging is that it doesn&amp;#8217;t need to be just a side project for me, and I don&amp;#8217;t need to use it as an indirect form of self-promotion for something else. I can take real time and effort to research the content for it, and be much more responsive to my readers than would ever be possible if I was just doing this as a hobby. The value of good conversations with a dedicated group of Practicing Rubyists was something that I didn&amp;#8217;t anticipate the first time around with this service, but is something I&amp;#8217;ve planned for from the very beginning with the relaunched version. I want my readers to feel like I am accessible and willing to give them personalized attention as they work through the content I&amp;#8217;m writing. All of this will be possible, and even financially sustainable, with only a couple hundred subscribers.&lt;/p&gt;
&lt;p&gt;My goal is to make enough money between &lt;a href="http://practicingruby.com"&gt;Practicing Ruby&lt;/a&gt; and the &lt;a href="http://majesticseacreature.com/network.html"&gt;Mendicant Supporter Network&lt;/a&gt; to make it so that I can spend most of my time working on Mendicant University and contributing to the open source community. I am getting closer and closer to that goal each day, with the help of many kind folks who believe in the work I do. If you have enjoyed my writing via this blog and want to help keep me going, please consider sending some cash my way so that I can not think about paying the bills and instead think about how to write awesome code and prose that I can share with the world.&lt;/p&gt;
&lt;p&gt;Oh, one last thing. There are a lot of people out there who are in developing countries, are out of work or underworked, or otherwise are in a difficult financial situation. If you can&amp;#8217;t pay the full price of a Practicing Ruby subscription for any reason, just email me at gregory.t.brown@gmail.com and I&amp;#8217;ll give you a free subscription. It doesn&amp;#8217;t cost me anything, and you gain something, so I&amp;#8217;d be more than happy to hook you up.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 27 Sep 2011 22:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/057-the-practicing-ruby-journal.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/057-the-practicing-ruby-journal.html</guid></item><item><title>Issue #24: Connascence as a Software Design Metric</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on February 11, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/055-issue-23-solid-design.html"&gt;Last week&amp;#8217;s massive article on &lt;span class="caps"&gt;SOLID&lt;/span&gt;&lt;/a&gt; was inspired by a great talk I saw by Sandi Metz at GoRuCo 2009. Coincidentally, this week&amp;#8217;s article is inspired by another great talk I saw in 2009, called &lt;a href="http://confreaks.net/videos/77-mwrc2009-the-building-blocks-of-modularity"&gt;&amp;#8220;The Building Blocks of Modularity&amp;#8221;.&lt;/a&gt; This talk was given by Jim Weirich at &lt;span class="caps"&gt;MWRC&lt;/span&gt;, and if you haven&amp;#8217;t seen it yet, I urge you to stop what you&amp;#8217;re doing and head on over to Confreaks right now:&lt;/p&gt;
&lt;p&gt;In the talk, Jim jokingly claims he&amp;#8217;s presenting on the &amp;#8220;Grand Unified Theory of Software Development&amp;#8221;. Personally, I think that isn&amp;#8217;t too far off the mark, because connascence is a fundamentally simple concept when compared to things like the &lt;span class="caps"&gt;SOLID&lt;/span&gt; principles or any of other design concepts we&amp;#8217;ll be studying in this series.&lt;/p&gt;
&lt;h3&gt;Brief introduction to connascence for the uninitiated&lt;/h3&gt;
&lt;p&gt;Since I didn&amp;#8217;t know the concept of connascence even existed before seeing Jim&amp;#8217;s talk, and because it&amp;#8217;s not a super common discussion topic even among design geeks, we should at least steal some &lt;a href="http://en.wikipedia.org/wiki/Connascent_software_components"&gt;content from Wikipedia&lt;/a&gt; to frame our discussion around. A great place to start is the following definition:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;i&gt;&amp;#8220;Two software components are connascent if a change in one would require the other to be modified in order to maintain the overall correctness of the system. Connascence is a way to characterize and reason about certain types of complexity in software systems.&amp;#8221;&lt;/i&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you haven&amp;#8217;t watched Jim&amp;#8217;s talk yet, I&amp;#8217;ll remind you to go ahead and do that now. But assuming for some reason you can&amp;#8217;t or won&amp;#8217;t, you should know that the kinds of complexity that connascence can be used to reason about typically have something to do with coupling. The relationship between the concept of connascence to the concept of coupling becomes a little more clear when you look at the various kinds of connascence that can be found in software systems. Below I&amp;#8217;ve listed out the various kinds of connascence in order from weakest to strongest.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;b&gt;Name:&lt;/b&gt; when multiple components must agree on the name of an entity.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Type:&lt;/b&gt; when multiple components must agree on the type of an entity.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Meaning:&lt;/b&gt; when multiple components must agree on the meaning of specific values.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Position:&lt;/b&gt; when multiple components must agree on the order of values.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Algorithm:&lt;/b&gt; when multiple components must agree on a particular algorithm.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Execution (order):&lt;/b&gt; when the order of execution of multiple components is important.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Timing:&lt;/b&gt; when the timing of the execution of multiple components is important.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Identity:&lt;/b&gt; when multiple components must reference the entity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Knowing the various kinds of connascence gives us a solid metric for determining the characteristics and severity of the coupling in our systems. The idea is fairly simple: The more remote the connection between two clusters of code, the weaker the connascence between them should be.&lt;/p&gt;
&lt;p&gt;Good design principles encourages us to move from tight coupling to looser coupling where possible. But connascence allows us to be much more specific about what kinds of problems we&amp;#8217;re dealing with, which makes it easier to reason about the types of refactorings that can be used to weaken the connascence between components.&lt;/p&gt;
&lt;p&gt;In this article, I will demonstrate how to reduce several forms of connasence all the way down to the weakest form of connnascence. In particular, I&amp;#8217;ll show how you can convert instance of Type, Meaning, Position, and Algorithm-based Connascence down to Connascence of Name. While all forms of connascence are worth studying, these ones in particular are likely to appear in your day to day work.&lt;/p&gt;
&lt;p&gt;But before we can show how to reduce things to CoN, we should show an example of what it is.&lt;/p&gt;
&lt;h3&gt;Connascence of Name&lt;/h3&gt;
&lt;p&gt;Name based coupling exists when a name change in one place requires a code change in other places. Being the weakest form of connascence, it&amp;#8217;s also by far the most common. Every module, class, method and variable we create introduces connascence of name, assuming it is actually used for something.&lt;/p&gt;
&lt;p&gt;As a simple example, suppose that we have a &lt;tt&gt;Mailer&lt;/tt&gt; class, which is used in the manner shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
mailer = Mailer.new
mailer.deliver(:to      =&amp;gt; "gregory.t.brown@gmail.com", 
               :from    =&amp;gt; "fake@fake.com",
               :subject =&amp;gt; "You have won a lifetime supply of...",
               :body    =&amp;gt; "Dear Sir, I am pleased to inform...")
&lt;/pre&gt;
&lt;p&gt;In just this tiny bit of code, we see an incredible amount of name based coupling. Any of the following trivial changes to Mailer would cause all code that depends on it to break immediately.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Wrapping the Mailer class definition in a namespace, e.g. &lt;tt&gt;FancyUnicorn::Mailer&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Renaming the &lt;tt&gt;deliver()&lt;/tt&gt; method to &lt;tt&gt;send_message()&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Renaming any of the keys in the hash passed to &lt;tt&gt;deliver()&lt;/tt&gt;, i.e. changing the &lt;tt&gt;:to&lt;/tt&gt; key so that it reads &lt;tt&gt;:recipient&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But the fact is, there isn&amp;#8217;t really any way around this sort of coupling in most scenarios, and it&amp;#8217;s not necessarily a sign of a problem. That having been said, the reason why naming things is so important in computer science is because even loosely coupled, highly cohesive systems have copious amounts of name based coupling, which have widespread effects that only increase as systems get more complex.&lt;/p&gt;
&lt;p&gt;Sometimes, it is possible to eliminate Connascence of Name and the the coupling that comes along with it. For example, when defining class methods in Ruby, one way to write them is like this.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Mailer
  def Mailer.configure(*args)
    #...
  end
end
&lt;/pre&gt;
&lt;p&gt;There is a clear dependence in this code between the second line of code and the first, in which if the first line changes, so too must the second line. We can rewrite this to avoid that coupling, if we just take advantage of Ruby&amp;#8217;s self keyword here.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Mailer
  def self.configure(*args)
    #...
  end
end
&lt;/pre&gt;
&lt;p&gt;But while eliminating Connascense of Name is desireable when it is both possible and convenient to do so, it&amp;#8217;s not always realistic. We just need to accept that since names don&amp;#8217;t change all that often, a little bit of CoN is okay to have in our system. In fact, when given the choice between CoN and other forms of Connascence, we should almost always favor name based coupling. We will now take a look at the other forms of connascence to see why that is the case.&lt;/p&gt;
&lt;h3&gt;Connascence of Type&lt;/h3&gt;
&lt;p&gt;Folks like to think that Ruby is immune to all forms of issues with type problems, but that assumption is often far too optimistic. The following code is a direct example of Connascence of Type:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def average(values)
   raise ArgumentError unless values.kind_of?(Array)

   values.inject(:+) / values.size.to_f
end
&lt;/pre&gt;
&lt;p&gt;One might attempt to resolve the problem by moving away from strict class checking and instead use a &lt;tt&gt;respond_to?()&lt;/tt&gt; check&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def average(values)
   unless values.respond_to?(:inject) &amp;amp;&amp;amp; values.respond_to?(:size)
     raise ArgumentError 
   end

   values.inject(:+) / values.size.to_f
end
&lt;/pre&gt;
&lt;p&gt;This certainly loosens the type coupling, but does not eliminate it. If we accept the notion that the type of a Ruby object is defined by what that object can do, &lt;tt&gt;respond_to?()&lt;/tt&gt; is still a form of type check, done at the method level instead of at the level of the class hierarchy. It can sometimes even result in false negatives, because not all code which implements dynamic behavior through &lt;tt&gt;method_missing()&lt;/tt&gt; updates &lt;tt&gt;respond_to?()&lt;/tt&gt; to add those methods. This can lead to code similar to previous example to fail with certain kinds of proxy objects, even though they implement all necessary behaviors.&lt;/p&gt;
&lt;p&gt;To truly free ourselves from Connascence of Type, one option is to just remove the guard clause and let Ruby bubble up with an exception for objects that don&amp;#8217;t work as our code expects them to. But sometimes, we want to make sure our debugging isn&amp;#8217;t harder than it needs to be. Here&amp;#8217;s an alternative that preserves the error handling but does so in a way that is free of type dependencies.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def average(values)
   values.inject(:+) / values.size.to_f
rescue NoMethodError
   raise ArgumentError, "The average() function can only " +
                        "operate on collections which implement " +
                        "the inject() and size() methods"
end
&lt;/pre&gt;
&lt;p&gt;If this feels a bit overkill, it&amp;#8217;s because it probably is. But the general idea of removing the &lt;tt&gt;kind_of?()&lt;/tt&gt; and &lt;tt&gt;respond_to?()&lt;/tt&gt; checks is a good one, because it puts us squarely back in the realm of Connascence of Name. Our dependency is now simply that the values object has a pair of methods with the names &lt;tt&gt;inject()&lt;/tt&gt; and &lt;tt&gt;size()&lt;/tt&gt;.&lt;/p&gt;
&lt;h3&gt;Connascence of Meaning&lt;/h3&gt;
&lt;p&gt;In its most simple form, Connascence of Meaning is all about magic values. For example, perhaps you have a legacy system which implements access levels in which an admin is given the value 0, a manager 1, and an ordinary user 2. You could end up writing code like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
if user.access_level == 0
  shoot_nukes_at_moon
else
  raise AccessDeniedError
end
&lt;/pre&gt;
&lt;p&gt;The trouble is, once you&amp;#8217;ve littered your system with explicit values like this, you will have a hard time both remembering what they do, and hunting them down when they have changed. To fix this, we can make some small modifications to our hypothetical &lt;tt&gt;User&lt;/tt&gt; object.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class User
  ACCESS_LEVELS = { 0 =&amp;gt; :admin, 1 =&amp;gt; :manager, 2 =&amp;gt; :user }

  def admin?
    ACCESS_LEVELS[access_level] == :admin
  end
end
&lt;/pre&gt;
&lt;p&gt;We try to avoid repeating Connascence of Meaning even in the more local context of the &lt;tt&gt;User&lt;/tt&gt; class by storing the actual role mappings in a constant. We then provide a convenience method &lt;tt&gt;User#admin?&lt;/tt&gt; to be used externally, resulting in newly minted caller code that looks like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
if user.admin?
  shoot_nukes_at_moon
else
  raise AccessDeniedError
end
&lt;/pre&gt;
&lt;p&gt;Now I don&amp;#8217;t know about you, but I think there is going to be a better chance that I&amp;#8217;m not going to accidentally nuke the moon when those numeric &lt;tt&gt;access_levels&lt;/tt&gt; change if I write my code this way.&lt;/p&gt;
&lt;p&gt;We haven&amp;#8217;t totally eliminated the &lt;tt&gt;Connascence of Meaning&lt;/tt&gt;, but we&amp;#8217;ve moved it to a hyper-local context within a single constant on the User model. Since all of the calling code is now just exhibiting Connascence of Name, this is a great refactoring.&lt;/p&gt;
&lt;h3&gt;Connascence of Position&lt;/h3&gt;
&lt;p&gt;Connascence of Position is something that we see every day in Ruby because method parameters are ordered. If we go to our mailer example, we could have just as easily written the &lt;tt&gt;Mailer#deliver()&lt;/tt&gt; method to use explicitly ordered parameters, similar to what is shown in the example below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Mailer
  def deliver(to, from, subject, message)
    # ...
  end
end
&lt;/pre&gt;
&lt;p&gt;APIs like this annoy the heck out of me, because the calling code typically doesn&amp;#8217;t give any hints at why the arguments are specified in a particular order. Take a look at how opaque things get when we just try to reproduce our previous example using this slightly different &lt;span class="caps"&gt;API&lt;/span&gt;:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
mailer.deliver("gregory.t.brown@gmail.com", 
               "fake@fake.com",
               "You have won a lifetime supply of...",
               "Dear Sir, I am pleased to inform...")
&lt;/pre&gt;
&lt;p&gt;Looking at this code, it&amp;#8217;s very difficult to determine who is the sender and who is the recipient, and even more difficult to think about how you might introduce default values into the mix. Every change to the ordering or length of the list of arguments can lead to broken code in remote places in your codebase. For all of these reasons, Rubyists tend to prefer hash-based pseudo-keyword arguments for all but the most straightforward method signatures.&lt;/p&gt;
&lt;p&gt;However, introducing keyword arguments isn&amp;#8217;t the only way to reduce CoP in method signatures to CoN. Another alternative that is perhaps underused is to simply create objects that provide all the necessary attributes that you would typically use a hash for. In this case, we can envision a simple &lt;tt&gt;Message&lt;/tt&gt; object being introduced:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
message = Message.new
message.to      = "gregory.t.brown@gmail.com"
message.from    = "fake@fake.com"
message.subject = "You have won a lifetime supply of..."
message.body    = "Dear Sir, I am pleazed to inform..."

mailer.deliver(message)
&lt;/pre&gt;
&lt;p&gt;Assuming that the &lt;tt&gt;Mailer#deliver&lt;/tt&gt; method just depends on the attribute readers for those attributes, this is functionally equivalent to the hash based code but offers a number of advantages. &lt;tt&gt;Message&lt;/tt&gt; is now a reusable, independently testable entity that can do things like validations internally. This moves some of the error checking and simple transformation code that might be needed to use a parameters hash into a more local setting. With a little creativity, it&amp;#8217;s relatively easy to make the &lt;span class="caps"&gt;API&lt;/span&gt; look a little nicer by letting &lt;tt&gt;Mailer#deliver&lt;/tt&gt; create the message object for you.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
mailer.deliver do |message|
  message.to      = "gregory.t.brown@gmail.com"
  message.from    = "fake@fake.com"
  message.subject = "You have won a lifetime supply of..."
  message.body    = "Dear Sir, I am pleazed to inform..."
end
&lt;/pre&gt;
&lt;p&gt;This sort of &lt;span class="caps"&gt;API&lt;/span&gt; is fairly common in Ruby as well, but probably not as common as it should be. So next time you&amp;#8217;re faced with the CoP problem when dealing with method arguments, consider fixing it by putting a nice shiny new object in place.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s worth noting that Connascence of Position is certainly not limited to method arguments in Ruby. Anywhere in which a change in position of some data requires you to change code elsewhere, you&amp;#8217;ve got a CoP issue, and should think about how to reduce it if possible.&lt;/p&gt;
&lt;h3&gt;Connascence of Algorithm&lt;/h3&gt;
&lt;p&gt;Connascence of Algorithm often looks and smells a bit like the &lt;span class="caps"&gt;DRY&lt;/span&gt; principle. But there are many cases in which code that violates &lt;span class="caps"&gt;DRY&lt;/span&gt; doesn&amp;#8217;t have a CoA problem, and some rare cases where the opposite is true. The key thing about CoA is the dependency between two or more clusters of code.&lt;/p&gt;
&lt;p&gt;The following example is a CoA example from the wild, from a programming quiz site that we&amp;#8217;re working on as part of Mendicant University&amp;#8217;s admission process. First, you can see a fairly &lt;span class="caps"&gt;DRY&lt;/span&gt; model which is meant to compare uploaded solutions to the actual answer for a given puzzle.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Puzzle &amp;lt; ActiveRecord::Base
  def file=(tempfile)
    write_attribute :fingerprint, sha1(tempfile)
  end

  def valid_solution?(tempfile)
    fingerprint == sha1(tempfile)
  end

  private

  def sha1(tempfile)
    Digest::SHA1.hexdigest(tempfile.read)
  end
end
&lt;/pre&gt;
&lt;p&gt;Internally, this code is fairly free of CoA, particularly because the algorithm for fingerprinting solutions has been extracted into the &lt;tt&gt;Puzzle#sha1()&lt;/tt&gt; helper. But because this is a private method, I ended up with tests that explicitly do the hashing themselves to verify that the &lt;tt&gt;Puzzle#file=()&lt;/tt&gt; method is working as expected.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
test "must be able to create a fingerprint for a file" do
  tempfile = Tempfile.new("puzzle_sample")
  tempfile &amp;lt;&amp;lt; "Sample Text"
  tempfile.rewind

  expected_fingerprint = Digest::SHA1.hexdigest(tempfile.read)
  tempfile.rewind

  puzzle = Puzzle.new(:file =&amp;gt; tempfile)

  assert_equal expected_fingerprint, puzzle.fingerprint
end
&lt;/pre&gt;
&lt;p&gt;This has an upside in that it sanity checks the exact behavior, ensuring that the tempfile is actually hashed via SHA1. But since the focus of the test is more on ensuring that a hash was generated rather than the way it was generated, we might be able to improve this by extracting the fingerprinting code into its own module.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Fingerprint
  extend self

  def [](stream)
    Digest::SHA1.hexdigest(stream.read)
  end
end
&lt;/pre&gt;
&lt;p&gt;Then, I could rewrite the code and tests to look like they do below:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Puzzle &amp;lt; ActiveRecord::Base
  def file=(tempfile)
    write_attribute :fingerprint, Fingerprint[tempfile]
  end

  def valid_solution?(tempfile)
    fingerprint == Fingerprint[tempfile]
  end
end
&lt;/pre&gt;

&lt;pre name = "code" class = "ruby"&gt;
test "must be able to create a fingerprint for a file" do
  tempfile = Tempfile.new("puzzle_sample")
  tempfile &amp;lt;&amp;lt; "Sample Text"
  tempfile.rewind

  expected_fingerprint = Fingerprint[tempfile]
  tempfile.rewind

  puzzle = Puzzle.new(:file =&amp;gt; tempfile)

  assert_equal expected_fingerprint, puzzle.fingerprint
end
&lt;/pre&gt;
&lt;p&gt;The end result would be that the algorithm for how I was generating digital fingerprints for the solutions could change, and I would not need to update my tests, as long as the names of everything stayed the same.&lt;/p&gt;
&lt;p&gt;In this case, arguably just fully applying the &lt;span class="caps"&gt;DRY&lt;/span&gt; principle would lead us to the same place, but the concept of connascence lets us think about the consequences of &lt;span class="caps"&gt;DRY&lt;/span&gt; in a less abstract way. Like all the other types of connascence, there is a lot more we could talk about here, but in the interest of time, we&amp;#8217;ll skip the details for now.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;While we dug deep into some heavy theory in &lt;a href="http://blog.rubybestpractices.com/posts/gregory/055-issue-23-solid-design.html"&gt;last week&amp;#8217;s &lt;span class="caps"&gt;SOLID&lt;/span&gt; article&lt;/a&gt;, I tried to keep the connascence examples simple, practical, and common. But that is not to say that connascence isn&amp;#8217;t every bit as deep a concept as &lt;span class="caps"&gt;SOLID&lt;/span&gt;, and your investigations should not stop at the examples I&amp;#8217;ve shown here.&lt;/p&gt;
&lt;p&gt;In the two articles to follow this one, we&amp;#8217;ll be looking at particular patterns and techniques that can help you design better code. Now that you&amp;#8217;re armed with both the &lt;span class="caps"&gt;SOLID&lt;/span&gt; principles and the metrics of connascence, you have a solid basis for thinking about these problems in more specific contexts. I encourage you to re-read these first two articles as you continue on with this series, and get back to me with any questions you might have.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: There are only two articles left to release from Practicing Ruby Volume 1. If you want to keep receiving great content like this on a weekly basis, please sign up for &lt;a href="http://practicingruby.com"&gt;Practicing Ruby Volume 2&lt;/a&gt;, which already has five new articles published as of 2011-09-20.&lt;/b&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 20 Sep 2011 17:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/056-issue-24-connascence.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/056-issue-24-connascence.html</guid></item><item><title>Issue #23: SOLID Design Principles</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on February 5, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;SOLID&lt;/span&gt; is a collection of five object oriented design principles that go nicely together. Here&amp;#8217;s a super brief summary pulled from &lt;a href="http://en.wikipedia.org/wiki/SOLID"&gt;the wikipedia page&lt;/a&gt; on the topic:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Single responsibility principle: an object should have only a single responsibility.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Open/closed principle: an object should be open for extension, but closed for modification.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Liskov substitution principle: objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Interface segregation principle: many client specific interfaces are better than one general purpose interface.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Dependency inversion principle: depend upon abstractions, do not depend upon concretions&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The term &lt;span class="caps"&gt;SOLID&lt;/span&gt; was coined by Uncle Bob Martin to group together these important concepts. I had heard of each of the design principles &lt;span class="caps"&gt;SOLID&lt;/span&gt; covers over the years, but didn&amp;#8217;t really think much of them until I attended a great talk by Sandi Metz at GoRuCo 2009. Fortunately, Confreaks recorded &lt;a href="http://confreaks.net/videos/240-goruco2009-solid-object-oriented-design
"&gt;Sandi&amp;#8217;s talk&lt;/a&gt; so I won&amp;#8217;t need to try to summarize it here.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;d strongly recommend watching that video before moving on, because it will go through &lt;span class="caps"&gt;SOLID&lt;/span&gt; in a lot more detail than what I plan to do in this article. You might also watch &lt;a href="http://confreaks.net/videos/185-rubyconf2009-solid-ruby"&gt;another video on the same topic by Jim Weirich&lt;/a&gt;, which like pretty much any other talk Jim has done, is likely to blow your mind.&lt;/p&gt;
&lt;p&gt;Rather than giving a tutorial on these principles, I&amp;#8217;m going to trust you to either read up on them or watch the videos I&amp;#8217;ve linked to. This way, we can focus on what I think is a much more interesting problem: How to apply these ideas to real code.&lt;/p&gt;
&lt;h3&gt;Single responsibility principle&lt;/h3&gt;
&lt;p&gt;The idea that an object should have only a single responsibility shouldn&amp;#8217;t come as a surprise. This concept is one of the selling points for object oriented languages that sets them apart from the more procedural systems that preceded them. The hard part about putting this idea into practice is figuring out just how wide to cast the &amp;#8216;single responsibility&amp;#8217; net.&lt;/p&gt;
&lt;p&gt;In my experience, most objects are born with just one goal in mind, and so adhere to this principle at least superficially in the early stages of greenfield projects. It&amp;#8217;s later when systems get more complex that our objects lose their identity. To demonstrate this phenomena, we can look at the life cycle of the Document object from my &lt;span class="caps"&gt;PDF&lt;/span&gt; generation library Prawn.&lt;/p&gt;
&lt;p&gt;Back in early 2008, when the project was just beginning, my idea was that the job of the Document class would be to wrap the low level concept of a Document at the &lt;span class="caps"&gt;PDF&lt;/span&gt; layer, with a few extra convenience functions at the high level. For a sketch of what that looked like at the time, we can take a look at the object&amp;#8217;s public methods.&lt;/p&gt;
&lt;pre&gt;
Directly implemented on Prawn::Document
  start_new_page, page_count, page_size, page_layout, render, render_file

Mixed in via Prawn::Document::Graphics
  line_width=, line, line_to, curve_to, curve, circle_at, ellipse_at,
  polygon, rectangle, stroke, fill, fill_color, stroke_color

Mixed in via Prawn::Document::PageGeometry
  page_dimensions
&lt;/pre&gt;
&lt;p&gt;This is so early in Prawn&amp;#8217;s history that it didn&amp;#8217;t even have text support yet. While the &lt;span class="caps"&gt;API&lt;/span&gt; wasn&amp;#8217;t perfectly well factored at this point in time, the fact that almost all the above methods directly produced &lt;span class="caps"&gt;PDF&lt;/span&gt; instructions or manipulated low level structures made me feel that it was a reasonably cohesive set of features.&lt;/p&gt;
&lt;p&gt;Fast forward by a year, and we end up with feature explosion on &lt;tt&gt;Document&lt;/tt&gt;. Here&amp;#8217;s what shipped in Prawn 0.4.1:&lt;/p&gt;
&lt;pre&gt;
Directly implemented on Prawn::Document
  start_new_page, page_count, cursor, render, render_file, bounds,
  bounds=, move_up, move_down, pad_top, pad_bottom, pad, mask,
  compression_enabled?, y, margin_box, margins, page_size,
  page_layout, font_size

Included via Prawn::Document::Text
  text, text_options, height_of (via Prawn::Document::Text::Wrapping),
  naive_wrap (via Prawn::Document::Text::Wrapping)

Included via Prawn::Document::PageGeometry
  page_dimensions

Included via Prawn::Document::Internals
  ref, add_content, proc_set, page_resources, page_fonts, 
  page_xobjects, names

Included via Prawn::Document::Annotations
  annotate, text_annotation, link_annotation

Included via Prawn::Document::Destinations
  dests, add_dest, dest_xyz, dest_fit,  dest_fit_horizontally, 
  dest_fit_vertically, dest_fit_rect, dest_fit_bounds,
  dest_fit_bounds_horizontally, dest_fit_bounds_vertically

Included via Prawn::Graphics
  move_to, line_to, curve_to, rectangle, line_width=, line_width,
  line, horizontal_line, horizontal_rule, vertical_line, curve,
  circle_at, ellipse_at, polygon, stroke, stroke_bounds, fill,
  fill_and_stroke, fill_color (via Prawn::Document::Color), 
  stroke_color (via Prawn::Document::Color)

Included via Prawn::Images
  image
&lt;/pre&gt;
&lt;p&gt;The above list of methods is almost embarrassingly scattershot, but it was due to an oversight. The mistake I made was thinking that splitting different aspects of functionality into modules was a valid way of respecting the single responsibility principle. But this is deeply flawed thinking, because the end result of pulling in roughly 50 methods into a single object by mixing in 8 modules results in a single object, &lt;tt&gt;Prawn::Document&lt;/tt&gt; having 60+ public methods all sharing the same state and namespace. Any illusion of a physical separation of concerns is all smoke and mirrors here.&lt;/p&gt;
&lt;p&gt;Once an object gets this fat, thinking about the cohesiveness of the interface is the most minor detail to be worried about. I&amp;#8217;ve focused on the 60 public methods here, but if we count private methods, they would easily exceed 100. Sometimes folks think that private methods in mixins don&amp;#8217;t actually get mixed into the base object, but that&amp;#8217;s an incorrect assumption, making this problem much, much worse.&lt;/p&gt;
&lt;p&gt;Having close to two hundred methods living in one space causes you to run into really basic, fundamental problems such as namespace clashes on method names and variables. It also makes data corruption downright easy, because it&amp;#8217;s hard to keep track of how a couple hundred methods manipulate a common dataset. Once you reach this point, you&amp;#8217;re back in procedural coding land where all manners of bad things can happen.&lt;/p&gt;
&lt;p&gt;Now that I&amp;#8217;ve sufficiently kicked my own ass, I can tell you the solution to this problem is simple, if not easy to refactor towards once you&amp;#8217;ve already made the mess: you just introduce more objects. To do so, we need to identify the different concerns and group them together, putting abstraction barriers between their implementations and the behaviors they provide.&lt;/p&gt;
&lt;p&gt;An easy realization to make is that over time, Prawn&amp;#8217;s &lt;tt&gt;Document&lt;/tt&gt; became two different things at the conceptual level. When we see methods like &lt;tt&gt;page_xobjects&lt;/tt&gt;, &lt;tt&gt;ref&lt;/tt&gt;, and &lt;tt&gt;proc_set&lt;/tt&gt;, we know that there are some low level tools in use here. But what about methods like move_up, move_down, text, image, and others like them? These are clearly meant for something that resembles a domain specific language, and Prawn does look gorgeous at the high level, just see the simple example below to see what I mean.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
  Prawn::Document.generate('hello.pdf') do 
    text "Hello Prawn!"
  end 
&lt;/pre&gt;
&lt;p&gt;With 20/20 hindset, the solution to this problem is obvious: Produce a whole layer of low level tooling that closely follows the &lt;span class="caps"&gt;PDF&lt;/span&gt; spec, creating objects for managing things like a &lt;span class="caps"&gt;PDF&lt;/span&gt;-level page, the rendering of raw &lt;span class="caps"&gt;PDF&lt;/span&gt; strings, etc. Make as many objects as necessary to do that, and then maybe provide a facade that makes interacting with them a bit easier.&lt;/p&gt;
&lt;p&gt;Then, for the higher level features, do the same thing. Have an object who&amp;#8217;s job is only to provide pretty looking methods that reach out to Prawn&amp;#8217;s lower level objects to do the dirty work. Dedicate whole objects or even clusters of objects to text, images, graphics, and any other cluster of functionality that previously was mixed into Document directly. The objects might require a bit more wiring, but the facade can hide that by doing things like the pseudo-code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def text(contents, options={})
  text_element = Prawn::TextElement.new(contents, options)
  text_element.render_on(current_page)
end
&lt;/pre&gt;
&lt;p&gt;Naming the benefits of this over the previous design would take a long time, but suffice to say, it cuts out those pesky namespace and data corruption concerns while still providing a cohesive &lt;span class="caps"&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;While I don&amp;#8217;t think that the scale of our design problem in Prawn is comparable to what most Ruby hackers are likely to experience in their day to day work, it does show just how bad things can get when you start dealing with very complex systems. Prawn has improved a lot since its 0.4.1 release, but undoing the damage that was done by neglecting this for so long has been a slow and painful process for us.&lt;/p&gt;
&lt;p&gt;The real lesson here is that you can&amp;#8217;t respect &lt;span class="caps"&gt;SRP&lt;/span&gt; without real abstraction barriers. &lt;span class="caps"&gt;SRP&lt;/span&gt; is about more than just creating a cohesive &lt;span class="caps"&gt;API&lt;/span&gt;, you actually need to create a physical separation of concerns at the implementation level of your system.&lt;/p&gt;
&lt;p&gt;Since it&amp;#8217;s very likely that you&amp;#8217;re experiencing this sort of issue on a smaller scale in the projects you&amp;#8217;re working on, keeping the story about what happened to me in Prawn in mind may help you learn from my mistakes instead of your own.&lt;/p&gt;
&lt;h3&gt;Open/closed principle&lt;/h3&gt;
&lt;p&gt;The open/closed principle tells us that an object should be open for extension, but closed for modification. This can mean a lot of different things, but the basic idea is that when you introduce a new behavior to an existing system, rather than modifying old objects you should create new objects which inherit from or delegate to the target object you wish to extend. The theoretical payoff is that taking this approach improves the stability of your application by preventing existing objects from changing frequently, which also makes dependency chains a bit less fragile because there are less moving parts to worry about.&lt;/p&gt;
&lt;p&gt;Personally, I feel that treating this principle as an absolute law would lead to the creation of a lot of unnecessary wrapper objects that could make your application harder to understand and maintain, so much that it might outweigh the stability benefits you&amp;#8217;d gain. But that doesn&amp;#8217;t mean these ideas don&amp;#8217;t have their value, in fact, they provide an excellent alternative to extensive monkeypatching of third party code.&lt;/p&gt;
&lt;p&gt;To illustrate this, I&amp;#8217;d like to talk a bit about &lt;a href="https://github.com/carlosantoniodasilva/i18n_alchemy"&gt;i18n_alchemy&lt;/a&gt;, a project by Carlos Antonio da Silva that was built as a student project for his Mendicant University core course. The goal of this project was to make it easy to add localizations for numeric, time, and date values in ActiveRecord.&lt;/p&gt;
&lt;p&gt;Early on in the course, Carlos came to me with an implementation that more-or-less followed the standard operating procedure for developing Rails plugins. While Carlos shouldn&amp;#8217;t be faulted for following community trends here, the weapon of choice was a shotgun blast into an &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; object&amp;#8217;s namespace, via a mixin which could be used on a per-model level. By including this module, you would end up with behavior that looked a bit like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
some_model = SomeModel.where(something)
some_model.a_number     #=&amp;gt; &amp;lt;a localized value&amp;gt;
some_model.a_number_raw #=&amp;gt; &amp;lt;the original numeric value&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Now, there are pros and cons to this approach, but I felt pretty sure that we could do better, and through conversations with Carlos, we settled on a much better design that didn&amp;#8217;t make such far reaching changes to the model objects. Before I explain how it works, I&amp;#8217;d like to show an example of how i18n_alchemy works now:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
some_model = SomeModel.where(something)
some_model.a_number     #=&amp;gt; &amp;lt;the original numeric value&amp;gt;

localized_model = some_model.localized
localized_model.a_number #=&amp;gt; &amp;lt;a localized value&amp;gt;
&lt;/pre&gt;
&lt;p&gt;In this new implementation, you do have to explicitly ask for a localized object, but that small change gains us a lot. The module that gives us &lt;tt&gt;SomeModel#localized&lt;/tt&gt; only introduces that one method, rather than a hook that gets run for every &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; method that is called. That means that ordinary calls to models extended by i18n_alchemy still work as they always did.&lt;/p&gt;
&lt;p&gt;Our localized model act differently, but it&amp;#8217;s actually not an instance of SomeModel at all. Instead, it is a simple proxy object that defines special accessors for the methods that i18n_localized, delegating everything else to the target model instance.&lt;/p&gt;
&lt;p&gt;This makes it possible for the consumer to choose when it&amp;#8217;d be best to work with the localized object, and when it&amp;#8217;d be best to work with the model directly. By contrast to the first implementation which badly breaks the ordinary expected behavior of an ActiveRecord model, this approach creates a new entity which can have new behaviors while reusing the old functionality.&lt;/p&gt;
&lt;p&gt;We were both pretty proud of the results here, because it gives some of the convenient feel of mixing in some new functionality into an existing Ruby object without the many downsides. This of course is only a single example of how you can use &lt;span class="caps"&gt;OCP&lt;/span&gt; in your own code, but I think it&amp;#8217;s a particularly good one.&lt;/p&gt;
&lt;h3&gt;Liskov substitution principle&lt;/h3&gt;
&lt;p&gt;The idea behind Liskov substitution is that functions that are designed operate on a given type of object should work without modification when they operate on objects that belong to a subtype of the original type. In many object oriented languages, the type of an object is closely tied to its class, and so in those languages, this principle mostly describes a rule about a relationship between a subclass and a superclass. In Ruby, this concept is a bit more fluid, and probably requires a bit more explanation up front.&lt;/p&gt;
&lt;p&gt;When we talk about the type of an object in Ruby, we&amp;#8217;re mostly concerned with what messages that object responds to, rather than what class that object is an instance of. This may seem like a subtle difference, but it has a profound impact on how we think about thing. In Ruby, type checking can range from very strict to none at all, as shown by the examples below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
  ## Different ways of type checking, from most to least coarse ##

  # verify the class of an object matches a specific class
  object.class == Array

  # verify object's class descends from a specific class
  object.kind_of?(Array)   

  # verify a specific module is mixed into this object
  object.kind_of?(Enumerable)
  
  # verify object claims to understand the specified message
  object.respond_to?(:sort)   

  # don't verify, trust object to either behave or raise an error
  object.sort                 
&lt;/pre&gt;
&lt;p&gt;Regardless of the level of granularity of the definition, objects that are meant to be treated as subtypes of a base type should not break the contracts of the base type. This is a very hard standard to live up to when dealing with ordinary class inheritance or module mixins, since you basically need to know the behavior specifications for everything in the ancestry chain, and so the rule of thumb is basically not to inherit from anything or mix in a module unless you&amp;#8217;re fairly certain that the behavior you&amp;#8217;re implementing will not interfere with the internal operations of your ancestors.&lt;/p&gt;
&lt;p&gt;To demonstrate a bit of a weird &lt;span class="caps"&gt;LSP&lt;/span&gt; issue, let&amp;#8217;s think about what happens when you subclass an &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; object. Technically speaking, if we give ourselves a pass for breaking signature of methods provided by Object, we&amp;#8217;d still need to keep track of all the behaviors &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; provides, and take care not to violate them. Here&amp;#8217;s a brief list of method names, but keep in mind we&amp;#8217;d also need to match signatures and return values.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; ActiveRecord::Base.instance_methods(false).sort
=&amp;gt; ["==", "[]", "[]=", "attribute_for_inspect", "attribute_names",
"attribute_present?", "attribute_types_cached_by_default", "attributes",
"attributes=", "attributes_before_type_cast", "becomes", "cache_key",
"clone", "colorize_logging", "column_for_attribute", "configurations",
"connection", "connection_handler", "decrement", "decrement!",
"default_scoping", "default_timezone", "delete", "destroy",
"destroy_without_callbacks", "destroy_without_transactions",
"destroyed?", "eql?", "freeze", "frozen?", "has_attribute?", "hash",
"id", "id=", "id_before_type_cast", "include_root_in_json", "increment",
"increment!", "inspect", "lock_optimistically", "logger",
"nested_attributes_options", "new_record?", "partial_updates",
"partial_updates?", "pluralize_table_names", "primary_key_prefix_type",
"quoted_id", "readonly!", "readonly?", "record_timestamps", "reload",
"reload_without_autosave_associations", "reload_without_dirty", "save",
"save!", "save_without_dirty", "save_without_dirty!",
"save_without_transactions", "save_without_transactions!",
"save_without_validation", "save_without_validation!", "schema_format",
"skip_time_zone_conversion_for_attributes", "store_full_sti_class",
"store_full_sti_class?", "table_name_prefix", "table_name_suffix",
"time_zone_aware_attributes", "timestamped_migrations", "to_param",
"toggle", "toggle!", "update_attribute", "update_attributes",
"update_attributes!", "valid?", "valid_without_callbacks?",
"write_attribute", "write_attribute_without_dirty"]
&lt;/pre&gt;
&lt;p&gt;Hopefully your impression after reading this list is that &lt;span class="caps"&gt;LSP&lt;/span&gt; is basically impossible to be a purist about, but let&amp;#8217;s try to come up with a plausible violation that isn&amp;#8217;t some obscure edge case. For example, what happens if we&amp;#8217;re building a database model for describing a linux system configuration, which has a field called logger in it? You can certainly at least get away with the migration for it without Rails complaining, using something like the code shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class CreateLinuxConfigs &amp;lt; ActiveRecord::Migration
  def self.up
    create_table :linux_configs do |t|
      t.text :logger
      t.timestamps
    end
  end

  def self.down
    drop_table :linux_configs
  end
end
&lt;/pre&gt;
&lt;p&gt;The standard behavior of &lt;tt&gt;ActiveRecord&lt;/tt&gt;&amp;#8216;s models is to provide dynamic accessors to a record&amp;#8217;s database fields, which means we should expect the following behavior under normal operation.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
config        = LinuxConfig.new
config.logger = "syslog-ng"
config.logger #=&amp;gt; "syslog-ng"
&lt;/pre&gt;
&lt;p&gt;But because &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; also implements a method called logger, and the dynamic attribute lookup is just a method_missing hack, we end up with a totally different kind of behavior.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
config        = LinuxConfig.new
config.logger = "syslog-ng" 
config.logger #=&amp;gt; #&amp;lt;ActiveSupport::BufferedLogger:0x00000000b6de38 
              #     @level=0, @buffer={}, @auto_flushing=1, 
              #     @guard=#&amp;lt;Mutex:0x00000000b6dde8&amp;gt;,
              #     @log=#&amp;lt;File:/home/x/demo/log/development.log&amp;gt;&amp;gt;
&lt;/pre&gt;
&lt;p&gt;If you&amp;#8217;ve been following closely, you probably saw this coming from a mile away, even if you couldn&amp;#8217;t predict the exact behavior. It&amp;#8217;s worth mentioning that even Rails knows that this sort of setup will lead to bad things, but their checks which raise an error when they spot this &lt;span class="caps"&gt;LSP&lt;/span&gt; violation apparently aren&amp;#8217;t comprehensive. But to be fair, if we try to set this at the time our record was initialized, or if we try to use write_attribute, we get a pretty decent error message.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; config = LinuxConfig.new(:logger =&amp;gt; "syslog-ng")
ActiveRecord::DangerousAttributeError: logger is defined by ActiveRecord
&lt;/pre&gt;

&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; config = LinuxConfig.new
=&amp;gt; #&amp;lt;LinuxConfig id: nil, logger: nil, created_at: nil, updated_at: nil&amp;gt;
&amp;gt;&amp;gt; config.write_attribute(:logger, "syslog-ng")
ActiveRecord::DangerousAttributeError: logger is defined by ActiveRecord
&lt;/pre&gt;
&lt;p&gt;This sort of proactive error checking is actually more than we should expect from most parent classes, &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; just takes special consideration because it is so widely used. You can&amp;#8217;t expect every object you might subclass to even try to catch these sorts of violations, and it&amp;#8217;s not a great idea to introduce this sort of logic into your own base classes without carefully considering the context. Of course, that doesn&amp;#8217;t mean that there aren&amp;#8217;t measures you can take to avoid &lt;span class="caps"&gt;LSP&lt;/span&gt; violations in code that you design yourself.&lt;/p&gt;
&lt;p&gt;I don&amp;#8217;t want to go into too much detail here, but there are two techniques I like to use for mitigating &lt;span class="caps"&gt;LSP&lt;/span&gt; issues. The first one is object composition, and the second is defining per-object behavior. Just as an experiment, I&amp;#8217;ve thrown together a rethinking of how &lt;tt&gt;ActiveRecord&lt;/tt&gt; could handle dynamic accessors in a slightly more robust way.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "delegate"

module DynamicFinderProxy

  extend self

  def build_proxy(record)
    proxy = SimpleDelegator.new(record)
    record.attribute_names.each do |a|
      proxy.singleton_class.instance_eval do
        define_method(a) { read_attribute(a) }
        define_method("#{a}=") { |v| write_attribute(a,v) }
      end
    end

    proxy
  end

end

class FakeActiveRecord

  class &amp;lt;&amp;lt; self
    def new
      obj = allocate
      obj.send(:initialize)
      DynamicFinderProxy.build_proxy(obj)
    end

    def column_names(*names)
      @column_names = names unless names.empty?
      @column_names
    end
  end

  def attribute_names
    self.class.column_names
  end

  def read_attribute(a)
    logger.puts("Reading #{a}")
    instance_variable_get("@#{a}")
  end

  def write_attribute(a,v)
    logger.puts("Writing #{a}")
    instance_variable_set("@#{a}",v)
  end

  def logger
    STDOUT
  end
end

class LinuxConfig &amp;lt; FakeActiveRecord
  column_names "logger", "crontab"
end

record = LinuxConfig.new
record.logger = "syslog-ng"
p record.logger
&lt;/pre&gt;
&lt;p&gt;Now, I&amp;#8217;ll admit that there is some deep voodoo in this code, but it at least indicates to me that we should be thinking differently about our options in Ruby. We have more than just vanilla inheritance to play with, and even ordinary mixins have their limitations, so maybe we need a whole new set of design principles that take Ruby&amp;#8217;s deeply dynamic nature into account? Or perhaps I&amp;#8217;ve just passed the midway point in a very long article and have decided to go off on a little tangent to keep myself entertained. I&amp;#8217;ll let you be the judge.&lt;/p&gt;
&lt;h3&gt;Interface segregation principle&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;ve seen a couple different interpretations of the interface segregation principle, with the most narrow ones almost directly outlining the use case for Java-style interfaces, which is to prevent code from specifying that an object must be a specific type when all that is required is a certain set of methods to have a meaningful implementation.&lt;/p&gt;
&lt;p&gt;Ruby offers a lot of flexibility and its dynamic typing makes a lot of interface segregation principle violations just go away on their own. That having been said, we still see a lot of &lt;tt&gt;is_a?()&lt;/tt&gt; and &lt;tt&gt;respond_to?()&lt;/tt&gt; checks which are both a form of &lt;span class="caps"&gt;LSP&lt;/span&gt; violation.&lt;/p&gt;
&lt;p&gt;To protect against those violations, the best bet is to embrace duck typing as much as possible. Since this article is already super long and we&amp;#8217;ve already covered duck typing extensively in issues &lt;a href="http://blog.rubybestpractices.com/posts/gregory/046-issue-14-duck-typing.html"&gt;#14&lt;/a&gt; and &lt;a href="http://blog.rubybestpractices.com/posts/gregory/047-issue-15-duck-typing-2.html"&gt;#15&lt;/a&gt; of Practicing Ruby, It would be sufficient to simply re-read those articles if you need a refresher and then promptly move on to the next principle. But in case you want to dig deeper, here are a couple more articles related to this topic that you should definitely read if you haven&amp;#8217;t seen them before. All three are about how to get around explicitly naming classes in case statements, which is a form of &lt;span class="caps"&gt;LSP&lt;/span&gt; violation.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://sandimetz.com/2009/06/ruby-case-statements-and-kindof.html" title="Sandi Metz"&gt;Ruby case statements and kind_of?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/aaronp/001_double_dispatch_dance.html" title="Aaron Patterson"&gt;The Double Dispatch Dance&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/008-decorator-delegator-disco.html" title="Gregory Brown"&gt;The Decorator Delegator Disco&lt;/a&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That should add an extra hour or so of homework for you. This is getting a bit crazy though, so let&amp;#8217;s hit that last principle and call it a day.&lt;/p&gt;
&lt;h3&gt;Dependency inversion principle&lt;/h3&gt;
&lt;p&gt;You probably already know about the values of dependency inversion (aka dependency injection) if you&amp;#8217;ve been working in Ruby for a while now. You also probably know that unlike some other languages, there really isn&amp;#8217;t a need for DI frameworks because it implements all the necessary tools for good DI at the language level. But in case you didn&amp;#8217;t get the memo, I&amp;#8217;ll go through a quick example of how dependency inversion can come in handy.&lt;/p&gt;
&lt;p&gt;Suppose we have a simple object, like a &lt;tt&gt;Roster&lt;/tt&gt;, which keeps track of a list of people, and we have a &lt;tt&gt;RosterPrinter&lt;/tt&gt; which creates formatted output from that list. Then we might end up with some code similar to what is shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Roster
  def initialize        
    @participants = []
  end

  def &amp;lt;&amp;lt;(new_participant)
    @participants &amp;lt;&amp;lt; new_participant
  end

  def participant_names
    @participants.map { |e| e.full_name }
  end

  def to_s
    RosterPrinter.new(participant_names).to_s
  end
end

class RosterPrinter
  def initialize(participant_names)
    @participant_names = participant_names
  end

  def to_s
    "Participants:\n" +
    @participant_names.map { |e| "* #{e}" }.join("\n")
  end
end
&lt;/pre&gt;
&lt;p&gt;The nice thing about this code is that it separates the presentation of a roster from its data representation, bringing it nicely in line with the single responsibility principle. But the problem with it is that &lt;tt&gt;Roster&lt;/tt&gt; and &lt;tt&gt;RosterPrinter&lt;/tt&gt; are needlessly coupled, which sort of limits the value of separating the objects in the first place. A small change to &lt;tt&gt;Roster#to_s()&lt;/tt&gt; can solve this problem.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Roster
  # other methods same as before

  def to_s(printer=RosterPrinter)
    printer.new(participant_names).to_s
  end
end

# usage
roster.to_s 
&lt;/pre&gt;
&lt;p&gt;This new code is functionally equivalent to our previous example when called with no arguments, but opens a whole host of new opportunities. For example, we can trivially swap in any printer object we&amp;#8217;d like now.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
class HTMLRosterPrinter
  def initialize(participant_names)
    @participant_names = participant_names
  end

  def to_s
    "&amp;lt;h3&amp;gt;Participants&amp;lt;/h3&amp;gt;&amp;lt;ul&amp;gt;"+
    @participant_names.map { |e| "&amp;lt;li&amp;gt;#{e}&amp;lt;/li&amp;gt;" } +
    "&amp;lt;/ul&amp;gt;
  end
end

# usage
roster.to_s(HTMLRosterPrinter)
&lt;/pre&gt;
&lt;p&gt;By injecting the printer object into &lt;tt&gt;Roster&lt;/tt&gt;, we can avoid having to resort to something as uncouth as creating a &lt;tt&gt;Roster&lt;/tt&gt; subclass for the sole purpose of wiring up the &lt;tt&gt;HTMLRosterPrinter&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Of course, the most common place that talk about dependency inversion comes up is when folks are thinking about automated testing. While Ruby makes it possible to mock out calls to pretty much any object, it&amp;#8217;s a whole lot cleaner to pass in raw mock objects than it is to set expectations on real objects.&lt;/p&gt;
&lt;p&gt;Dependency inversion can really come in handy, but it&amp;#8217;s important to provide sensible defaults so that you don&amp;#8217;t end up forcing consumers of your &lt;span class="caps"&gt;API&lt;/span&gt; to do a lot of tedious wiring. The trick is to make it so you can swap out implementations easily, it&amp;#8217;s not as important for your code to have no opinion about which implementation it should use. Folks sometimes forget this and as a result their code gets quite annoying to work with. However, Ruby makes it easy to provide defaults, so there is no real reason why this issue can&amp;#8217;t be averted.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;This article is much longer than I expected it would be, but I feel like I&amp;#8217;ve just scratched the surface. An interesting thing about the &lt;span class="caps"&gt;SOLID&lt;/span&gt; principles is that they all sort of play into each other, so you tend to get the most out of them by looking at all five concepts at once rather than each one in isolation.&lt;/p&gt;
&lt;p&gt;One thing I want to emphasize is that when I make use of &lt;span class="caps"&gt;SOLID&lt;/span&gt; or any other set of design principles, I tend to use them as a metric rather than a set of constructive rules. I don&amp;#8217;t typically set out designing a system with all of these different guidelines in mind, as that would give me a very clausterphobic feeling. However, when the time comes to sanity check a new design or make incremental improvements to an old one during a refactoring session, &lt;span class="caps"&gt;SOLID&lt;/span&gt; provides a good checklist for pinpointing areas of my code that might deserve some rethinking.&lt;/p&gt;
&lt;p&gt;Sometimes you break these rules by accident, and that&amp;#8217;s okay. Sometimes you break them because you are making a conscious trade to avoid some other bad thing from happening, and that&amp;#8217;s okay too. As long as you&amp;#8217;re regularly checking your assumptions about things and actually caring about the overall design of your system, you shouldn&amp;#8217;t feel guilty for not following these guidelines perfectly. In fact, it is more dangerous to blindly follow design principles to the letter than it is to completely ignore them.&lt;/p&gt;
&lt;p&gt;We have much, much more design discussion to come, so hopefully you enjoyed this article. :)&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 15 Sep 2011 20:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/055-issue-23-solid-design.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/055-issue-23-solid-design.html</guid></item><item><title>Issue #22: How to practice (2 of 2)</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on January 28, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: I usually edit these articles before posting on the blog to avoid temporal references such as &amp;#8220;today&amp;#8221; or &amp;#8220;this week&amp;#8221;, because it can get confusing due to the fact that these articles were originally published many months ago. But for this particular issue, it seemed like a good idea to keep those references intact. Just keep in mind that all of this is written in the context of January 2011, not September 2011.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/053-issue-21-how-to-practice.html"&gt;previous issue&lt;/a&gt;, I provided a series of questions and instructions that outlined the way I practice. While some may have been expecting code katas or other indirect exercises, my style is more geared towards learning on the job. You start by figuring out what&amp;#8217;s important to you, figure out a baby-step that you can make, and then execute. Once you&amp;#8217;ve brought yourself one step closer to your goal, you reflect a bit on how things are going, in particular, what parts of the project still scare you.&lt;/p&gt;
&lt;p&gt;I use this technique often, in particular, when I want to get started on a new project or explore a new area that I&amp;#8217;m not that familiar with. As luck would have it, I actually have a new project I need to start, and so today I&amp;#8217;ll give you the chance to metaphorically look over my shoulder as I work through this exercise myself. If you haven&amp;#8217;t read &lt;a href="http://blog.rubybestpractices.com/posts/gregory/053-issue-21-how-to-practice.html"&gt;Issue #21&lt;/a&gt;, now is a good time to do that, so that the rest of this article makes sense.&lt;/p&gt;
&lt;h3&gt;Step 1: Find out what&amp;#8217;s important&lt;/h3&gt;
&lt;p&gt;What interesting problems do you need to solve?&lt;/p&gt;
&lt;p&gt;Lately, this question has been one that has caused me great anxiety.The success of Mendicant University and even Practicing Ruby to a certain extent has caused an explosion of ideas that all feel worthwhile and important to me. But they&amp;#8217;re fairly easy to separate into wants and needs, and thankfully, much of what I&amp;#8217;ve come up with falls in the former category.&lt;/p&gt;
&lt;p&gt;When I think about it, there is something that I feel I need to do sooner rather than later. While our courses are booked up until May, we will need to start admissions for our second trimester soon. Towards the end of last year, we decided we wanted to make something a bit more fun and lighthearted than an entrance exam, but we didn&amp;#8217;t really take much action since then. With the clock ticking down, making headway on this project would surely help give me some peace of mind.&lt;/p&gt;
&lt;p&gt;What I&amp;#8217;d like to build is a programming quiz site that is inspired by the &lt;a href="http://ipsc.ksp.sk"&gt;Internet Problem Solving Contest&lt;/a&gt; and &lt;a href="http://projecteuler.net"&gt;Project Euler&lt;/a&gt;, but with an MU-themed twist. I&amp;#8217;ll have Mendicant&amp;#8217;s co-founder &lt;a href="http://twitter.com/Jordan_Byron"&gt;Jordan Byron&lt;/a&gt; to help me with the frontend, but since he&amp;#8217;s busy with 100 other development tasks for MU, I&amp;#8217;m the one who needs to build out the backend for this new app. I&amp;#8217;ll use my need to write this article as motivation to help me break ground on this new project today.&lt;/p&gt;
&lt;h3&gt;Step 2: Make a commitment&lt;/h3&gt;
&lt;p&gt;I made the broad commitment to our students that we&amp;#8217;d have a nice replacement for MU&amp;#8217;s currently dull admissions process before the next trimester began. But broad commitments don&amp;#8217;t particularly inspire action, so I needed to make a specific commitment as well.&lt;/p&gt;
&lt;p&gt;With that in mind, I decided to tell Jordan I&amp;#8217;d have something for him to look at today, even if it was just a small start. Since he&amp;#8217;ll be arriving here at my home within an hour of me finishing this article, I am already feeling the pressure of having something to show for myself, which I think is a good thing.&lt;/p&gt;
&lt;h3&gt;Step 3: Identify a baby step&lt;/h3&gt;
&lt;p&gt;The next step in this process is to come up with a small step to get you just a little bit closer to your goal. I&amp;#8217;ll admit, I knew by the time I finished Issue #21 that I&amp;#8217;d be working on this project, so I&amp;#8217;ve been subconsciously chewing on my baby-step for a couple days now. This to me is totally fine, it gives my brain a chance to think things through and makes actually sitting down and coding something easier. Of course, the key thing is that my delivery time was still boxed in, if you leave these things open ended, it&amp;#8217;s very likely you&amp;#8217;ll talk yourself out of building anything at all.&lt;/p&gt;
&lt;p&gt;When coming up with a tiny step, I try to focus on something that is core to the underlying project, to maximize the amount I learn from the mini spike. In the case of our quiz application (which we&amp;#8217;re calling PuzzleNode), validating user submissions is one of the most important pieces of functionality.&lt;/p&gt;
&lt;p&gt;What I&amp;#8217;ll do today is do a rough proof of concept of the submission validation system, which essentially compares the expected output to the actual file uploaded by a user. I generally like to subdivide my tasks even when working only for an hour, so I&amp;#8217;m going to attack this in three phases.&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;A simple function that compares two files using a SHA1 hash and returns true or false depending on whether they match.&lt;/li&gt;
&lt;/ol&gt;
&lt;ol&gt;
	&lt;li&gt;A tiny sinatra application that does the same, but introduces file uploads into the picture.&lt;/li&gt;
&lt;/ol&gt;
&lt;ol&gt;
	&lt;li&gt;A minimal Rails app that actually records whether a submission was valid or invalid, and properly links puzzles with their expected output.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I&amp;#8217;m setting my time limit for an hour, so I&amp;#8217;m not sure how far I&amp;#8217;ll actually get. No matter what happens, I&amp;#8217;ll try to jot down some notes to give you a feel for my though process as I work through this exercise.&lt;/p&gt;
&lt;h3&gt;Step 4: Get one step closer&lt;/h3&gt;
&lt;p&gt;[06:40] I&amp;#8217;ve got my clock set now, and I&amp;#8217;m ready to get started. Please excuse me while I go heads down for a bit. I&amp;#8217;ll pop up with some brief notes here each time I reach a transition point, and then go into more detail in the reflections phase.&lt;/p&gt;
&lt;p&gt;[06:45] Basic &lt;a href="https://github.com/sandal/pr-issue-22"&gt;github project&lt;/a&gt; set up for this experiment.&lt;/p&gt;
&lt;p&gt;[06:48] Add three text files, a reference which is meant to act as the expected solution, a good file which is just a copy of the reference, and a bad file with some modifications to make it not match the reference.&lt;/p&gt;
&lt;p&gt;[06:51] Phase 1 complete!&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
$ ruby check_solution.rb samples/reference.txt samples/good.txt 
GOOD

$ ruby check_solution.rb samples/reference.txt samples/bad.txt
BAD
&lt;/pre&gt;
&lt;p&gt;Source code is dead simple, just a few lines.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "digest/sha1"

expected = Digest::SHA1.hexdigest(File.read(ARGV[0]))
actual   = Digest::SHA1.hexdigest(File.read(ARGV[1]))

puts(expected == actual ? "GOOD" : "BAD")
&lt;/pre&gt;
&lt;p&gt;[06:54] Next step is to remind myself how file uploads work in Sinatra, an indicator of how rusty my frontend webdev knowledge is&amp;#8230;&lt;/p&gt;
&lt;p&gt;[06:57] Google for &amp;#8220;File uploads sinatra&amp;#8221; and find Peter Cooper talking about &lt;a href="http://technotales.wordpress.com/2008/03/05/sinatra-the-simplest-thing-that-could-possibly-work/"&gt;this blog post&lt;/a&gt; via Ruby Inside.&lt;/p&gt;
&lt;p&gt;Outdated, but worth a shot since it&amp;#8217;s just a one liner.&lt;/p&gt;
&lt;p&gt;[07:00] File uploads working via curl. Now time to integrate the phase 1 code into my sinata app.&lt;/p&gt;
&lt;p&gt;[07:06] Have something I think should work but now messing with some unexpected bugs. Drat!&lt;/p&gt;
&lt;p&gt;[07:08] Oh, apparently I just don&amp;#8217;t know how to use curl, working now! (albiet with a little echo hack to add a newline)&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
$ curl -F "data=@samples/bad.txt" 127.0.0.1:4567/reference.txt; echo
BAD
$ curl -F "data=@samples/good.txt" 127.0.0.1:4567/reference.txt; echo
GOOD
&lt;/pre&gt;
&lt;p&gt;Source is still quite simple, so I can inline it here.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "rubygems"
require "sinatra"
require "digest/sha1"

ACCEPTED_FILES = ["reference.txt"]

post "/:expected" do
  raise unless ACCEPTED_FILES.include?(params[:expected])

  expected = 
    Digest::SHA1.hexdigest(File.read("samples/#{params[:expected]}"))

  actual   = Digest::SHA1.hexdigest(params[:data][:tempfile].read)

  expected == actual ? "GOOD" : "BAD"
end
&lt;/pre&gt;
&lt;p&gt;Off we go to phase 3!&lt;/p&gt;
&lt;p&gt;[07:11] I need to think up a few AR models. Off to the whiteboard, back in a moment.&lt;/p&gt;
&lt;p&gt;[07:15] I&amp;#8217;ve decided to cheat a bit. I realized that for a very basic demo, I don&amp;#8217;t actually need to store the uploaded files anywhere, but instead, I just need each puzzle to store its SHA1 fingerprint. Then, when a new submission is made, you just hash the file uploaded by the user and compare it to the associated puzzle.&lt;/p&gt;
&lt;p&gt;This data model omits a lot, and would need a lot of love to actually be used in our application, but it is sufficient for demonstrating just the validation step.&lt;/p&gt;
&lt;pre&gt;
Puzzle(name: text, fingerprint: text) 
Submission(puzzle_id: integer, correct: boolean)
&lt;/pre&gt;
&lt;p&gt;Time to go spit out a Rails skeleton, I suppose. The key thing this has saved me is a trip through paperclip&amp;#8217;s documentation, and a host of questions about whether that&amp;#8217;s still the right tool for the job and whether it works with Rails 3 smoothly. I roughly assume that the answer to each of those questions is yes, but better to not have to answer them right now.&lt;/p&gt;
&lt;p&gt;[07:22] Only 18 minutes to go and rails is still installing, sloooooow.&lt;/p&gt;
&lt;p&gt;[07:24] Still installing! Should have used &amp;#8212;no-rdoc &amp;#8212;no-ri!&lt;/p&gt;
&lt;p&gt;[07:25] Finally finished installing, while waiting I stumbled upon &lt;a href="
http://stackoverflow.com/questions/1381725/how-to-make-no-ri-no-rdoc-default-for-gem-install"&gt;this post on disabling documentation by default&lt;/a&gt;. Will need to try that out later.&lt;/p&gt;
&lt;p&gt;[07:26] Doh, never going to undo my stupid muscle memory&lt;/p&gt;
&lt;p&gt;$ rails puzzlenode&lt;br /&gt;
Usage:&lt;br /&gt;
  rails new APP_PATH [options]&lt;/p&gt;
&lt;p&gt;[07:29] Hmm, rails comes with a .gitignore file now? That&amp;#8217;s handy. Though I&amp;#8217;m pretty sure I just accidentally checked in my config/database.yml. Oh well, not a big deal, this is just a spike, right?&lt;/p&gt;
&lt;p&gt;[07:30] Wow, now is not the time to be punished by the fact that I aliased mvim to sl on my Gentoo box in an effort to stop typing mvim where it doesn&amp;#8217;t work. That seemed like a good idea at the time, of course.&lt;/p&gt;
&lt;p&gt;[07:32] Toot toot! Time to switch consoles, this is taking forever. Dear reader, you &lt;strong&gt;have&lt;/strong&gt; googled sl by now, right? :)&lt;/p&gt;
&lt;p&gt;[07:39] Ran out of time, so just messed with the data models a bit in the console to imagine their interactions. Will need to save a proper implementation for later.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; Puzzle.create(:name =&amp;gt; "Reference", :fingerprint =&amp;gt;
&amp;gt;&amp;gt; Digest::SHA1.hexdigest(File.read("#{RAILS_ROOT}/samples/reference.txt")))

=&amp;gt; #&amp;lt;Puzzle id: 1, name: "Reference", fingerprint:
"a59eb2c51e07e2b7369baef8a0c3cb3b5d7ed3d9", created_at: "2011-01-28
12:37:41", updated_at: "2011-01-28 12:37:41"&amp;gt;

&amp;gt;&amp;gt; Submission.create(:puzzle_id =&amp;gt; 1, :correct =&amp;gt; false)

=&amp;gt; #&amp;lt;Submission id: 1, puzzle_id: 1, correct: false, 
     created_at: "2011-01-28 12:38:41", updated_at: "2011-01-28 12:38:41"&amp;gt;
&amp;gt;&amp;gt; Submission.create(:puzzle_id =&amp;gt; 1, :correct =&amp;gt; false)

=&amp;gt; #&amp;lt;Submission id: 2, puzzle_id: 1, correct: false, 
   created_at: "2011-01-28 12:38:42", updated_at: "2011-01-28 12:38:42"&amp;gt;

&amp;gt;&amp;gt; Submission.create(:puzzle_id =&amp;gt; 1, :correct =&amp;gt; true)

=&amp;gt; #&amp;lt;Submission id: 3, puzzle_id: 1, correct: true, 
   created_at: "2011-01-28 12:38:45", updated_at: "2011-01-28 12:38:45"&amp;gt;

&amp;gt;&amp;gt; Puzzle.find(1).submissions.where(:correct =&amp;gt; true).count
=&amp;gt; 1
&amp;gt;&amp;gt; Puzzle.find(1).submissions.where(:correct =&amp;gt; false).count
=&amp;gt; 2
&lt;/pre&gt;
&lt;p&gt;Hah, at least my associations seem to be working correctly. I can has rails!&lt;/p&gt;
&lt;h3&gt;Step 5: Reflect on your progress&lt;/h3&gt;
&lt;p&gt;This exercise went more or less as I expected it to, with a couple surprises here and there. One thing that didn&amp;#8217;t dawn on me until I reached stage 3 is that I don&amp;#8217;t necessarily need to worry about file attachments in this application. While certain features such as having the ability to review user submissions or display the reference output would require it, a simple alpha product could be shipped without those features and still be quite usable.&lt;/p&gt;
&lt;p&gt;The exercise hopefully also reflects a bit of realism, as I didn&amp;#8217;t rehearse it ahead of time and ran into some stupid things that slowed me down, which is what might happen to anyone. That&amp;#8217;s really okay, because in the process, I learned some things worth looking into later on.&lt;/p&gt;
&lt;p&gt;Now that I&amp;#8217;m an hour into this project, my instructions call for me to reflect on what scares me about it. I actually have a lot of general fears, but in order to explain them I&amp;#8217;d need to give a lot of context about the project and those ideas are still fuzzy even in my own mind. That having been said, there is a specific concern that I can share which this small spike keeps reminding me of.&lt;/p&gt;
&lt;h3&gt;What about this project scares me?&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;m not entirely sure whether I like fingerprinting as a mechanism for determining whether a solution is valid. It scares me to think that if a problem called for you to generate a bit of &lt;span class="caps"&gt;XML&lt;/span&gt;, simply alterations to whitespace could result in an otherwise perfectly valid submission getting rejected.&lt;/p&gt;
&lt;p&gt;The way that &lt;span class="caps"&gt;IPSC&lt;/span&gt; and Project Euler solve this problem is by deeply restricting the submission format. In the case of &lt;span class="caps"&gt;IPSC&lt;/span&gt; this consists of bits of numbers or text separated by newlines, and for Project Euler the solutions are always to compute a simple number. I could definitely adopt this strategy, but it makes me worried that it&amp;#8217;ll limit the kinds of problems I can run at PuzzleNode.&lt;/p&gt;
&lt;p&gt;I want to avoid making the problems at PuzzleNode primarily algorithmic in nature, with a focus more on practical problem solving and creative thinking. Both Project Euler and &lt;span class="caps"&gt;IPSC&lt;/span&gt; do a good job of this within a subset of their problems, but a good share of them happen to be algorithmic as well. I wonder if that&amp;#8217;s due to the input constraints, and if it is, that would be bad for MU.&lt;/p&gt;
&lt;p&gt;One possibility is that rather than doing a bitwise matching via a fingerprint, I could force users to provide &lt;span class="caps"&gt;JSON&lt;/span&gt; data which I could then process and compare based on the object structure. This would allow for much greater flexibility in the way I validate submissions, and eliminate the failure-by-formatting issue I pointed out before, but it&amp;#8217;d both increase the overhead of submitting a solution and make the backend functionality a good deal more complex.&lt;/p&gt;
&lt;p&gt;I think that what I need to do is draft up a few puzzles and see how much the current fingerprinting validation restrictions get in the way. I may be worrying about nothing, but the only way to tell is to produce some content and see where that brings me.&lt;/p&gt;
&lt;h3&gt;Step 6: Rinse and Repeat&lt;/h3&gt;
&lt;p&gt;My next step is to actually flesh out the Rails backend, since I didn&amp;#8217;t get that far with it. I&amp;#8217;m glad to have found that I can defer file uploads until a bit later, this is something I don&amp;#8217;t think I would have realized if I started directly by jumping into the Rails boilerplate.&lt;/p&gt;
&lt;p&gt;Once I have a minimal system functioning, my next step will be to come up with three or four problems to test it against. I already have one idea in mind, generating a few more shouldn&amp;#8217;t be too bad.&lt;/p&gt;
&lt;p&gt;With a plan in mind for how I can take that next step, I feel confident that this project will keep moving forward.&lt;/p&gt;
&lt;h3&gt;Closing Thoughts&lt;/h3&gt;
&lt;p&gt;This is a real outline of how I practice. At first, when I wrote up the set of instructions, I thought formalizing it would make it feel artificial to me. But honestly, once I got rolling on the spike, things happened pretty much the way they always would, and the comments I left were just the thoughts that came up in my mind as I went along. In that sense, it didn&amp;#8217;t feel like practice.&lt;/p&gt;
&lt;p&gt;You&amp;#8217;ll notice that I start with what I know and work outwards from there. I rarely try to think too hard about what I need to know ahead of time, because I find it causes me to study the wrong things at the wrong time. A more formal approach might have lead me to study paperclip up front, because this process involves file uploads. But the 20-30 minutes that might have costed me we found through experimentation is something that I can put off for several weeks without it affecting my progress.&lt;/p&gt;
&lt;p&gt;I tend not to plug into the firehose of information coming from books, blogs, and reddit/HN for the same reason. Soaking up that material is begging to find a solution in search of a problem, rather than the other way around. It&amp;#8217;s always easy to ask for a recommendation at the time you actually need something, and Google is pretty good at digging up well read blog posts or articles about whatever tool you might need, and so I put off studying until it is necessary.&lt;/p&gt;
&lt;p&gt;I don&amp;#8217;t do a whole lot of code katas, or little practice exercises that I can&amp;#8217;t actually use for something. I will certainly do those things for entertainment, but I don&amp;#8217;t schedule &amp;#8216;practice time&amp;#8217; in my day to day life and honestly, I never have. There is no shortage of necessary learning that takes place when chasing practical goals, and the reward is much greater than just having an abstract feeling of learning a bit more, you end up with something you can use.&lt;/p&gt;
&lt;p&gt;The more I can make my life my practice, the less I need to be disciplined about making adequate time for formal academic exercises. I fully admit that there are a lot of things about my lifestyle and circumstances that make me especially blessed in this department, but I wasn&amp;#8217;t always in a fortunate position and would give similar advice even when I was struggling to make ends meet.&lt;/p&gt;
&lt;p&gt;So in closing, it may be true that the way to Carnegie Hall is via the &amp;#8220;Practice! Practice! Practice!&amp;#8221; path, but in my mind, that means less time playing with yourself in the comfort of your own home, and more time on small stages until they lead you to a slightly bigger stage which you can then occupy until it too, becomes too small.&lt;/p&gt;
&lt;p&gt;This is how I practice. I hope hearing about it has been useful to you.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.09.09&lt;/b&gt;: &lt;i&gt; The &lt;a href="http://puzzlenode.com"&gt;PuzzleNode website&lt;/a&gt; was successfully launched on time, and has been used to conduct three entrance exams for Mendicant University already. The puzzles there are language agnostic, and may be fun to try out even if you aren&amp;#8217;t planning to apply to MU. But I&amp;#8217;d be just as happy to hear that you&amp;#8217;re too busy working on real projects that you care a lot about instead.&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 09 Sep 2011 16:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/054-issue-22-how-to-practice.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/054-issue-22-how-to-practice.html</guid></item><item><title>Issue #21: How to practice (1 of 2)</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on January 22, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;When I was 17 years old, I was asked by an Air Force military training instructor how to get to Carnegie Hall. Before I could even think of why he&amp;#8217;d ask me this question, he shouted &amp;#8220;Practice! Practice! Practice!&amp;#8221; This was followed by an hour long monologue about the finer points of properly making a bed and stowing underwear in a locker, which might explain why my aspirations for a military career started and ended in &lt;span class="caps"&gt;JROTC&lt;/span&gt;. But that cliché of a first line stuck with me, even to this day.&lt;/p&gt;
&lt;p&gt;You are reading a newsletter called Practicing Ruby. Therefore, you must not be averse to practice or are at least not a stranger to it. However, skillful practice is an art form, and not all types of practice should be considered equal. I assume that our readers have goals that are closer to taking the stage at Carnegie Hall than they are to perfectly folding a pair of underwear. With that in mind, I&amp;#8217;ll be sharing my secrets about how I practice, in the hopes that the techniques I&amp;#8217;ve developed over the years will work for you, too.&lt;/p&gt;
&lt;p&gt;Please decide at this point whether you have about 2-3 hours to spare within the next few days to try out some exercises that go along with this article. If you don&amp;#8217;t feel like you can do that, stop reading now, skip and come back to this two-part article when you have time for it. The ideas I am covering will only really sink in if you put them into action.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;ve decided to be brave and read on, great! Now is the time to open up your text editor or grab a sheet of paper and a pen.&lt;/p&gt;
&lt;h3&gt;The $64,000 Question&lt;/h3&gt;
&lt;p&gt;The question you need to answer before it ever makes sense to practice, work hard, or even think about anything seriously, is simply this:&lt;/p&gt;
&lt;p&gt;What interesting problems do you need to solve?&lt;/p&gt;
&lt;p&gt;When properly considered, this question can serve as well as a compass at pointing you in the right direction. The key is to pay attention to every last detail that it demands, so that you can pick the right kind of goal. Here are some guidelines for picking the right area to focus in:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;First, it must be something interesting, i.e. something that stands out from the ordinary. Now, as long as you find the topic interesting, it needn&amp;#8217;t interest the whole world. But at the very least, it must be something that won&amp;#8217;t get lost in the background noise of our day to day lives.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Second, it must be a problem. Without some form of conflict, without a struggle between what is and what can be, it is very difficult for creativity or productivity to occur naturally. Problems have a way of capturing the imagination in ways that &amp;#8216;exercises&amp;#8217; or &amp;#8216;routines&amp;#8217; never can.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Third, it must be a need of yours. Notice I use the word need and not want. While at the physiological level, needs are very basic (food, water, clothing, shelter, sex, etc), psychologically our needs are much more complex. To me a need is the kind of thing that eats at you until you find a way to satisify it. A need is something that when ignored, makes things worse than what they should be.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Fourth, it must be related to you. There are many, many problems out there that are interesting and need to be solved. Which challenges are you uniquely qualified to solve? Which challenges are you uniquely pained by if they&amp;#8217;re left unsolved.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Finally, it must be solvable, or at least show promise of being solvable. There are lots of problems out there that would be great if we could solve them, but nothing seems to change about them. Fortunately, there are just as many that can be solved with sufficient motivation. Don&amp;#8217;t waste your time on the impossible, feel free to settle for something challenging but surmountable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So now that I&amp;#8217;ve explained it in detail, I&amp;#8217;ll repeat the question, and then I&amp;#8217;d like for you to take some time and write down a serious answer to it. What interesting problems do you need to solve?&lt;/p&gt;
&lt;h3&gt;Making a commitment&lt;/h3&gt;
&lt;p&gt;Derek Sivers claimed you should keep your intentions to yourself when planning your goals. But Freakonomics thinks he&amp;#8217;s wrong, and so do I.&lt;/p&gt;
&lt;p&gt;Every single project that I&amp;#8217;ve been successful on, I&amp;#8217;ve described in public before I even broke ground. The &lt;a href="http://www.oreillynet.com/ruby/blog/2008/03/id_love_to_quit_my_job_sort_of.html 
"&gt;original Ruby Mendicant project&lt;/a&gt; that brought us &lt;a href="http://github.com/sandal/prawn"&gt;Prawn&lt;/a&gt;, and my current work at &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt; are just two examples that I can offer some anecdotal evidence for.&lt;/p&gt;
&lt;p&gt;Now have I succeeded at every project I have discussed publicly? Hell no! But to be honest, I&amp;#8217;ve been kept busy enough by my successes that I don&amp;#8217;t need to worry too much about my failures in life. I&amp;#8217;ve also found that by sharing my ideas as early as possible, I can get a sense if people are as excited about it as I am. While a lukewarm response isn&amp;#8217;t necessarily an indicator of a bad project, things that create buzz often indicate that you&amp;#8217;ve struck a real nerve. When you stumble across problems like that, you really ought to invest in solving them.&lt;/p&gt;
&lt;p&gt;So now that you&amp;#8217;ve written down your goal, the next step is to share it with someone. I&amp;#8217;d recommend posting it in the comments section at the end of this article, but I&amp;#8217;d pretty much accept any action you can do to share your idea with someone else in the world. Tell your kid, tell your wife or husband. It doesn&amp;#8217;t really matter who you tell, as long as you put it out there.&lt;/p&gt;
&lt;p&gt;Now is the time where people most often second guess themselves, thinking their idea is not ready yet, or that it won&amp;#8217;t be appreciated by others. If that&amp;#8217;s really how you feel, fine, go back to step one and generate a problem that you &lt;strong&gt;can&lt;/strong&gt; share that still meets all those guidelines.&lt;/p&gt;
&lt;p&gt;The next step is to shift from having an idea to having something you can actually act on.&lt;/p&gt;
&lt;h3&gt;Making progress&lt;/h3&gt;
&lt;p&gt;At the beginning of the article, I asked you to set aside two to three hours for working through these exercises. Maybe you&amp;#8217;ve spent a little bit of time brainstorming already, but you probably have plenty of time left on the clock, right? If so, now is the time to roll up your sleeves and get your hands dirty.&lt;/p&gt;
&lt;p&gt;You&amp;#8217;ve got a goal, and you&amp;#8217;ve told someone about it. Now your next task is to answer this question: What is a concrete, measurable action you can take that will take you an hour or less, but still manage to get you closer to your goal?&lt;/p&gt;
&lt;p&gt;Write down your answer to that question and then pass it along to whoever you shared your goal with. Then, sit down and actually try to do what you said you would do. Keep working on it until either you&amp;#8217;ve solved the small subproblem you&amp;#8217;ve just described, or until the full hour runs out.&lt;/p&gt;
&lt;p&gt;Once you&amp;#8217;ve put the work in, make sure to let your selected observers know. That&amp;#8217;ll make you want to build something that you actually believe meets your goals, rather than mentally lowering your standards to match what you&amp;#8217;ve actually produced. If you fell short of your objectives, don&amp;#8217;t be disappointed, just explain what obstacles got in your way. Folks are typically more understanding than we give them credit for, and your observer is not likely to be an exception.&lt;/p&gt;
&lt;h3&gt;Reflecting on your progress&lt;/h3&gt;
&lt;p&gt;You now have made a few steps in the direction of solving an important problem that you find personally interesting. Great work!&lt;/p&gt;
&lt;p&gt;Now, there is only one question left to ask: What scares you about this project?&lt;/p&gt;
&lt;p&gt;If the answer to that is &amp;#8216;nothing&amp;#8217;, pause a moment and double check whether you&amp;#8217;re really being honest with yourself. I personally find fear or extreme uncertainty to be a common phenomena when working on hard problems, and if I feel 100% confident with no real doubts or worries, I begin to think that maybe the problem I&amp;#8217;m working on isn&amp;#8217;t worth my time. That said, a lack of fear also sometimes comes from being in the state of flow, which is a really pleasant experience. Try to distinguish between the two, and only settle for &amp;#8216;nothing&amp;#8217; if you really believe it to be true.&lt;/p&gt;
&lt;p&gt;Assuming you do dig up some fears or doubts, write them down, in as much detail as you&amp;#8217;d like. Once you&amp;#8217;ve done that, try to separate thing things that you can figure out answers to from the things that you can&amp;#8217;t. Take a quick glance at the list of fears that you won&amp;#8217;t be able to reason your way out of, and decide if they&amp;#8217;re worth giving up your project over. If they are worth surrendering over, quit and start all the way at the beginning, forming a new goal for solving a new problem that you really care about.&lt;/p&gt;
&lt;p&gt;But assuming you have the courage to press on, throw out the list of unresolvable fears and focus on the ones that you can do something about. Study the topics they cover, talk to friends for ideas on how to get through them, and then once you feel better, throw that list out too.&lt;/p&gt;
&lt;h3&gt;Rinse and Repeat&lt;/h3&gt;
&lt;p&gt;Today I asked you for a couple hours of your time, to get you to practice your craft by actually working towards solving something that is important to you. Odds are, a couple hours wasn&amp;#8217;t enough time to fully solve one of your big problems. But it was a start.&lt;/p&gt;
&lt;p&gt;The good news is that the process recurses from here. You don&amp;#8217;t need to work in one hour intervals, but ask yourself what you can accomplish in an afternoon, in a work day, or in a week. Then, come up with a plan, tell people about it, execute, reflect, and repeat.&lt;/p&gt;
&lt;p&gt;This is what I do, and if it works for me, it may well work for you. Please try it out and share your thoughts with the group once you&amp;#8217;ve worked through the suggested exercises in this article.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;I started out by saying that practice is important, and you may have then expected to hear me talk about how important it is to read books and blogs, or to work on code katas, or something else that involves a disconnected form of learning that is separate from real world applications. But frankly, I find that sort of approach to be inefficient, when real goals do so much of a better job of guiding you towards what is really important to focus on.&lt;/p&gt;
&lt;p&gt;Hopefully by working through the exercises I&amp;#8217;ve proposed here, you&amp;#8217;ll gain a better understanding of why this approach to learning can be so power. In the next issue, I will share some examples from my own projects in which I&amp;#8217;ve used this process to make good progress in a short period of time. Through that, you&amp;#8217;ll hopefully be able to see how almost all of my time spent studying is done through goal based learning rather than some sort of separate, isolated practice sessions.&lt;/p&gt;
&lt;p&gt;More than any other article I&amp;#8217;ve published so far, I hope you will participate in this one. Even if it seems a bit cheesy, I think that actually working through these exercises will prove to be a worthwhile experience for you.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 06 Sep 2011 16:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/053-issue-21-how-to-practice.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/053-issue-21-how-to-practice.html</guid></item><item><title>Issue #20: Thoughts on Mocking (2 of 2)</title><description>&lt;p&gt;&lt;i&gt;Originally published as part of the first volume of the &lt;a href="http://practicingruby.com"&gt;Practicing Ruby newsletter&lt;/a&gt; on January 22, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/051-issue-19-thoughts-on-mocking.html"&gt;last issue&lt;/a&gt;, I encouraged everyone to read Martin Fowler&amp;#8217;s classic article &lt;a href="http://martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks Aren&amp;#8217;t Stubs&lt;/a&gt;. Since this article is a bit dated and leans heavily towards Java style development practices, I also offered my own commentary to hopefully bridge the gap between Fowler&amp;#8217;s insights and the modern Ruby world. Now that we have the theories behind us, today we can focus on putting these ideas into practice.&lt;/p&gt;
&lt;p&gt;There is a style of behavior driven development that encourages mocking everything except the object under test. Fowler calls folks who follow this methodology &lt;em&gt;mockists&lt;/em&gt;, and more-or-less presents this approach as a completely valid alternative to classic &lt;span class="caps"&gt;TDD&lt;/span&gt;, in which test doubles of any variety are only used when absolutely necessary. While I think that such an assessment is valid in the context Fowler originally wrote his article (2004/Java), I personally feel that the &lt;em&gt;mockist&lt;/em&gt; style that Fowler describes has no place in modern Ruby development.&lt;/p&gt;
&lt;p&gt;That having been said, when used in moderation, mocking frameworks can make testing a whole lot easier. Today, I&amp;#8217;ll be sharing my thoughts on when to use mocks and when not to. While these are not meant to be taken as strict rules to follow, they may shed some light on a middle ground between Fowler&amp;#8217;s classicist and mockist categories.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: I&amp;#8217;m using &lt;a href="http://github.com/citrusbyte/contest"&gt;citrusbye/contest&lt;/a&gt; and &lt;a href="https://github.com/floehopper/mocha"&gt;mocha&lt;/a&gt; in the tests shown in this article, but the ideas should apply to any testing framework + mocking system.&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Good uses for mocks&lt;/h3&gt;
&lt;p&gt;When I think back on my testing habits, I find that virtually all of my use of mock objects falls into one or more of the following three categories:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Testing code which depends on an external resource of some sort (a web service, the filesystem, mail server, etc.)&lt;/li&gt;
	&lt;li&gt;Testing code which would involve a large amount of non-reusable setup and fixture data if you didn&amp;#8217;t mock at a high level.&lt;/li&gt;
	&lt;li&gt;Testing code which relies on features which are particularly computationally expensive.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each of these scenarios has their caveats, but odds are, most moderate to large size projects I work on hit at least one of them, and it isn&amp;#8217;t entirely uncommon to deal with all three of these issues simultaneously. That alone tells me that having a good understanding of how to use mocks is a key part of &lt;span class="caps"&gt;TDD&lt;/span&gt; I&amp;#8217;ll now share some examples that hopefully help drive that point home.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Isolation from external resources&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;It would be great if our projects were completely self-contained, not having to deal with any shared resources, but this isn&amp;#8217;t realistic. Most projects need to deal with at least some external resources and may even have to tackle some systems integration problems. This often makes automating testing considerably more challenging than we would like it to be.&lt;/p&gt;
&lt;p&gt;Thankfully, mock objects provide some shortcuts for us. While they won&amp;#8217;t help us with testing the code that needs to interact with the outside world, they can easily be used as stand-ins for our integration points when we are testing code that depends on outside resources. This makes it possible to test our high level logic without having to access whatever external resources our code needs to integrate with.&lt;/p&gt;
&lt;p&gt;To demonstrate how useful this can be, we&amp;#8217;ll look at some simple tests from a tool I built which uses &lt;em&gt;win32ole&lt;/em&gt; to integrate with some Windows based truck routing software. Below, you can see a bit of test code that ensures a particular error gets raised when an invalid stop is added to the trip object.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
test "trip must be able to detect an invalid stop" do 
  trip = MilesDavis::Trip.create
  expect_an_invalid_stop

  error = assert_raises(MilesDavis::InvalidStopError) do 
    trip.stops &amp;lt;&amp;lt; "Fakeville, FK" 
  end

  assert_equal "Cannot Find: Fakeville, FK", error.message
end 
&lt;/pre&gt;
&lt;p&gt;If you guessed that the &lt;tt&gt;expect_an_invalid_stop&lt;/tt&gt; method introduces a mock into these tests, you were right! While it might look a bit like magic on a first glance, I usually try to separate all but the most trivial mock logic into its own helper methods to make the tests easier to read and maintain.Below you can see what &lt;tt&gt;expect_an_invalid_stop&lt;/tt&gt; actually does.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def expect_an_invalid_stop
  server = mock()
  server.expects(:CheckPlaceName).returns(0)
  MilesDavis.expects(:server).returns(server)
end
&lt;/pre&gt;
&lt;p&gt;To tie everything together, we should now take a look at the implementation code that these tests run against. It is a simple module that gets mixed into the stops array when a new &lt;tt&gt;Trip&lt;/tt&gt; is created.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module StopValidation
  def &amp;lt;&amp;lt;(place)
    unless MilesDavis.server.CheckPlaceName(place) &amp;gt; 0
      raise InvalidStopError, "Cannot Find: #{place}"
    end

    super
  end
end
&lt;/pre&gt;
&lt;p&gt;If you go back and re-read the test and mock code, it should be pretty clear what is going on here. When this system is actually running in production, &lt;tt&gt;MilesDavis.server&lt;/tt&gt; refers to a &lt;em&gt;win32ole&lt;/em&gt; object, which explains the crappy camel case method names. But when running this particular test, we swap out the server call to return a mock object of our own creation.&lt;/p&gt;
&lt;p&gt;By crafting our tests to mock out any interaction with the server, our test suite still works fine outside of the production environment. Even though the core purpose of this library is to integrate with a proprietary bit of Windows code running on a particular machine, we were able to develop all but the lowest layer entirely within our Linux and Mac-based development environments without needing any direct access to the software we were integrating with.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s worth mentioning that although this use case was extracted directly from a real world project, it was hand picked to demonstrate the value of mocks in this sort of context. Other interactions with external resources are not so black and white. For example, if you&amp;#8217;re doing something like manipulating files on a system, it might make more sense to use temporary files than it would be to introduce mock objects. There are many other scenarios like this, so it&amp;#8217;s usually best to weigh out the costs and benefits before going full steam ahead with mocks.&lt;/p&gt;
&lt;p&gt;That having been said, mocking external resources is almost always a valid use case, if not the most optimal one in certain situations.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Avoiding complex setup + fixtures&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The main reason why integration with external resources is a pain is because it often requires lots of configuration and setup just to get things running. A similar phenomenon occurs internally when projects get large enough to have some complex object relations and/or advanced datastructures.&lt;/p&gt;
&lt;p&gt;What follows is a bit of test code for a decorator that we built to wrap some low level geospatial data that we were storing via PostGIS.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
test "retreive a valid US postal area" do
  expect_postal_area_search("06511")
  geom = GeoRegion.by_postal("06511")
  assert_equal :postal, geom.interpreted_type
end
&lt;/pre&gt;
&lt;p&gt;The mocking actually happens in &lt;tt&gt;expect_postal_area_search&lt;/tt&gt;, which is implemented via the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def expect_postal_area_search(zip)
  PostalArea.expects(:find_by_zcta).with(zip).returns(record_stub)
end
&lt;/pre&gt;
&lt;p&gt;This mock emulates a simple &lt;tt&gt;ActiveRecord&lt;/tt&gt; search, returning a stubbed out record which implements the bare minimum functionality required by our &lt;tt&gt;GeoRegion&lt;/tt&gt; class. While somewhat uninteresting, below is the definition of &lt;tt&gt;record_stub()&lt;/tt&gt;, for those curious.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def record_stub
  stub(:the_geom =&amp;gt; Object.new)
end
&lt;/pre&gt;
&lt;p&gt;The guts of &lt;tt&gt;GeoRegion&lt;/tt&gt; are actually a little bit complex, but our test was only meant to show that &lt;tt&gt;GeoRegion.by_postal&lt;/tt&gt; returns an object that responds to &lt;tt&gt;interpreted_type()&lt;/tt&gt; and returns the value &lt;tt&gt;:postal&lt;/tt&gt;. This means we can focus on just that part of things without losing anything important.&lt;/p&gt;
&lt;p&gt;The part of the code that does the geometry lookup is a simple delegator to &lt;tt&gt;PostalArea.find_by_zcta&lt;/tt&gt;, which is what &lt;tt&gt;expect_postal_area_search&lt;/tt&gt; mocks out for us. The stubbed out record it returns ends up being used in a helper method that defines the &lt;tt&gt;interpreted_type&lt;/tt&gt; on the record via a mixin and then sets its value.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def geom_for(record, type = nil)
  geom = (record &amp;amp;&amp;amp; record.the_geom) or 
    raise UnknownFormatError, "Not a valid #{type}"

  geom.extend(Meta)
  geom.interpreted_type = type 
  geom.record = record 
  
  return geom 
end         
&lt;/pre&gt;
&lt;p&gt;I won&amp;#8217;t bother tracing the longish execution path that lies on either side of this helper method, but the key takeaway here is that we&amp;#8217;re able to avoid to skip two layers of complexity by mocking out our call to &lt;tt&gt;PostalArea&lt;/tt&gt; and stubbing out the actual geometry object that is associated with that &lt;tt&gt;PostalArea&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;We could have loaded fixture data into our testing environment which actually provided the relevant geospacial data to perform the sort of search we needed for this feature, but doing so would certainly be more complicated than the two simple lines we used to create our mock and stub.&lt;/p&gt;
&lt;p&gt;Part of the reason mocks work out well here is that they allow you to focus on the behavior of &lt;tt&gt;GeoRegion&lt;/tt&gt; rather than its implementation details. Even though under the hood a bunch of complex object manipulation is going on, we only really care about a very narrow set of functionality that &lt;tt&gt;GeoRegion&lt;/tt&gt;&amp;#8217;s adds as metadata to the geometry objects looked up through its search methods. If we had to actually populate the database with geometry data and concern ourselves with the messy relationships between these objects, our tests would be far less clear.&lt;/p&gt;
&lt;p&gt;Of course, this technique only really makes sense when understanding and maintaining the mock object&amp;#8217;s interface is easier than creating the necessary setup code and fixtures to run the tests with real objects. Often times, the scales are tipped in the other direction, which I&amp;#8217;ll talk about a little later in this article. But before we get into the bad ideas, we have one more good one to cover.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Mocking for performance reasons&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The first two techniques both had something in common: They made life easier by preventing certain code from actually being run. If we take that idea and apply it to performance, we find that running less code is usually faster than running more code.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s consider the following simple code that sends an email message to a group each time a new member is added.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Group

  def initialize(name, admin)
    @name    = name
    @admin   = admin
    @members = []
  end

  attr_reader :members, :name, :admin

  def &amp;lt;&amp;lt;(new_user)
    raise if members.include?(new_user)

    members &amp;lt;&amp;lt; new_user
    broadcast("New user added", "#{new_user} joined the #{name} "+
              "group on #{Date.today}.")
  end

  def broadcast(title, content)
    mail = Mail.new

    mail.from(admin)
    mail.to(members)
    mail.subject(title)
    mail.body(content)

    mail.deliver
  end

end
&lt;/pre&gt;
&lt;p&gt;Because &lt;tt&gt;Group#broadcast&lt;/tt&gt; is almost entirely calls to the external Mail library, it arguably doesn&amp;#8217;t need unit tests, and instead could be covered by integration tests that set up a test mail server or something like that. However, &lt;tt&gt;Group#&amp;lt;&amp;lt;&lt;/tt&gt; is a different story.&lt;/p&gt;
&lt;p&gt;If we focus on the behavior of appending a user to the group, we don&amp;#8217;t actually need to focus on how &lt;tt&gt;broadcast()&lt;/tt&gt; is implemented, we only need to verify that it is called. The following test demonstrates how to apply that line of thinking.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
test "adding users" do
  group = Group.new("Practicing Ruby", "greg@practicingruby.com")

  expect_broadcast(group, 2)

  group &amp;lt;&amp;lt; "joe@example.com"
  group &amp;lt;&amp;lt; "matz@example.com"

  assert_equal ["joe@example.com", "matz@example.com"], group.members
end
&lt;/pre&gt;
&lt;p&gt;The most simple mock that reasonably covers the necessary functionality for &lt;tt&gt;expect_broadcast()&lt;/tt&gt; is shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def expect_broadcast(group, count)
  group.expects(:broadcast).times(count)
end
&lt;/pre&gt;
&lt;p&gt;We could actually go much farther here and verify the particular subject and content being passed to &lt;tt&gt;broadcast()&lt;/tt&gt;, but as I said in &lt;a href="http://blog.rubybestpractices.com/posts/gregory/050-issues-18-testing-dogma.html"&gt;issue #18&amp;#8217;s mini-rant on testing&lt;/a&gt;, I don&amp;#8217;t particularly like testing presentation logic that needs to be hand verified due to frequent superficial change. But personal preferences aside, even with a more complex set of expectations, using a mock object here is sure to be faster than actually sending an email.&lt;/p&gt;
&lt;p&gt;This is a bit contrived example, but imagine a group object with many more methods that send broadcast emails. Add to that all the email enabled features across an application, and you&amp;#8217;ll quickly see the clock ticking longer and longer even if you do have a mail server that pipes everything to &lt;em&gt;/dev/null&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This sort of scenario will come up in a number of different domains, and whenever it does, mock objects might be the right way to go. The main downside of using this sort of approach is that it eliminates the possibility of using your tests as a performance benchmark for your project. It is also worth noting that without proper integration tests, your mocks will happily go green in places that your real code may never be able to run. But since these issues tend to get spotted very quickly in manual testing and ordinary application use, it&amp;#8217;s usually okay to wait until this becomes a problem before worrying about it.&lt;/p&gt;
&lt;p&gt;The three types of scenarios I&amp;#8217;ve covered so far pretty much completely describe the valid use cases for mocks that have come up in my work. It isn&amp;#8217;t likely to be an exhaustive list, but I&amp;#8217;ve working in a fairly large amount of projects across diverse domains and have yet to see another need for mocks that I didn&amp;#8217;t cover here. I did run up against a couple anti-patterns though, so let&amp;#8217;s take a look at those now before we wrap up.&lt;/p&gt;
&lt;h3&gt;Bad uses for mocks&lt;/h3&gt;
&lt;p&gt;The two worst uses of mocks I know of are often listed as the secret sauce that makes them so useful.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Using mocks for complete isolation of internal dependencies&lt;/li&gt;
	&lt;li&gt;Using mocks as contracts for unwritten objects&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now to be sure, there are fairly strong arguments for each of these ideas, Fowler alone goes to great lengths making the case for them, and he is a moderate on these issues. But I&amp;#8217;d argue the line of thinking is really geared towards languages that punish users from creating lots of objects with simple APIs connecting them together, such as Java. Let&amp;#8217;s take a look at some Ruby examples so that we can consider that point.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Using mocks for complete isolation of internal dependencies&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Consider this simple variation on the theme of a user group, in which &lt;tt&gt;Group#&amp;lt;&amp;lt;&lt;/tt&gt; constructs Person objects for each new member of a group.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Group
  def initialize
    @members = []
  end

  attr_reader :members

  def &amp;lt;&amp;lt;(person_name)
    members &amp;lt;&amp;lt; Person.new(person_name)
  end

  def member_names
    members.map { |e| e.name }
  end
end
&lt;/pre&gt;
&lt;p&gt;A mockist would not think about whether &lt;tt&gt;Person&lt;/tt&gt; has external dependencies, complex setup requirements, or performance issues. They would just have started with a mock right away, perhaps something like this.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class GroupTest &amp;lt; Test::Unit::TestCase
  test "adding members to a group" do
    group = Group.new

    expect_new_member("Gregory Brown")
    group &amp;lt;&amp;lt; "Gregory Brown"

    expect_new_member("Jia Wu")
    group &amp;lt;&amp;lt; "Jia Wu"

    assert_equal ["Gregory Brown", "Jia Wu"], group.member_names
  end

  def expect_new_member(member_name)
    Person.expects(:new).returns(stub(:name =&amp;gt; member_name))
  end
end
&lt;/pre&gt;
&lt;p&gt;The neat thing about the code above is that it really does create some major isolation, in that it will still allow you to test &lt;tt&gt;Group#&amp;lt;&amp;lt;&lt;/tt&gt; and &lt;tt&gt;Group#member_names&lt;/tt&gt; with nothing more than a bare class definition for &lt;tt&gt;Person&lt;/tt&gt;. If we wanted to be hardcore, you could even create a &lt;tt&gt;Group#new_person&lt;/tt&gt; method and mock that instead, and then you wouldn&amp;#8217;t even need a defined &lt;tt&gt;Person&lt;/tt&gt; constant!&lt;/p&gt;
&lt;p&gt;But before we get too excited, let&amp;#8217;s assume &lt;tt&gt;Person&lt;/tt&gt; is just a trivial container method, such as the one shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Person
  def initialize(name)
    @name = name
  end

  attr_reader :name
end
&lt;/pre&gt;
&lt;p&gt;This code doesn&amp;#8217;t require any complex setup, it isn&amp;#8217;t using any external resources, and it doesn&amp;#8217;t have any performance intensive characteristics to it. That means that in order to test it directly, all we need to do is remove a bunch of lines from our previous test case.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
test "adding members to a group" do
  group = Group.new

  group &amp;lt;&amp;lt; "Gregory Brown"
  group &amp;lt;&amp;lt; "Jia Wu"

  assert_equal ["Gregory Brown", "Jia Wu"], group.member_names
end
&lt;/pre&gt;
&lt;p&gt;By comparison, the above code is much more simple. But some smart folks still write it the other way. This is not without reason, and in fact has something to do with what happens when a change is made that causes tests to fail. To illustrate this, suppose that Person has a simple test that looks something like this.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
test "a user has a name attribute" do
  user = User.new("Gregory Brown")
  assert_equal "Gregory Brown", user.name
end
&lt;/pre&gt;
&lt;p&gt;With the code we&amp;#8217;ve seen so far, this test easily passes. But consider what happens when the implementation of User is changed to something like the code below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Person
  def initialize(name)
    @name = name.upcase
  end

  attr_reader :name
end
&lt;/pre&gt;
&lt;p&gt;The version of our test suite which uses mock objects will have one failure in the test case that is specifically checking what &lt;tt&gt;Person#name&lt;/tt&gt; returns. It will not cause our &lt;tt&gt;Group&lt;/tt&gt; tests to fail, because a stubbed person object is used there instead. I&amp;#8217;ve included the output of a test run using that approach so you can see what that looks like.&lt;/p&gt;
&lt;pre&gt;
  1) Failure:
test_adding_members_to_a_group(GroupTest)
&amp;lt;["Gregory Brown", "Jia Wu"]&amp;gt; expected but was
&amp;lt;["GREGORY BROWN", "JIA WU"]&amp;gt;.

  2) Failure:
test_a_user_has_a_name_attribute(PersonTest)
&amp;lt;"Gregory Brown"&amp;gt; expected but was
&amp;lt;"GREGORY BROWN"&amp;gt;.
&lt;/pre&gt;
&lt;p&gt;This is exactly what mockists don&amp;#8217;t like to see. The argument is that as your programs get more complex, the dependencies between objects get larger and larger and you end up with tens or hundreds of failing tests all because of a change in one place. This phenomena can and does occur, and it happens in smaller projects than you might think.&lt;/p&gt;
&lt;p&gt;But still, doesn&amp;#8217;t something smell fishy?  The mock objects that are now being constructed in the tests for &lt;tt&gt;Group#member_names&lt;/tt&gt; are now completely out of synchronization with the real specifications of the application. It isn&amp;#8217;t possible to get the output they test against in real uses of the application, and so while they adequately test the behavior of &lt;tt&gt;Group#member_names&lt;/tt&gt;, the isolation has caused the mocks to diverge from reality, making them untrustworthy as &amp;#8216;living documentation&amp;#8217; for the real system.&lt;/p&gt;
&lt;p&gt;Personally, when I make a change that has potential system-wide affects, I prefer my tests to be verbose. Testing objects directly prevents this sort of out of sync representation of object behavior from being even possible, and so increases the reliability of the tests as both an integration testing safety net and as a documentation source.&lt;/p&gt;
&lt;p&gt;As for sifting through the sea of information that gets spit out when you &lt;strong&gt;don&amp;#8217;t&lt;/strong&gt; use mocks, there are ways of effectively sifting through it so as to not have problems even in very complex applications. But that is a topic more related to general debugging and may be better off described in another article.&lt;/p&gt;
&lt;p&gt;We still have one more point to cover before we wrap up here, and this is now edging on being a massive article, so let&amp;#8217;s get to it.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Using mocks as contracts for unwritten objects&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;When writing code test first, it is possible to use mock objects as stand ins for objects that have not been defined yet. As I had mentioned before, with minor alterations we wouldn&amp;#8217;t even need to have a &lt;tt&gt;Person&lt;/tt&gt; class defined in order to effectively test &lt;tt&gt;Group#&amp;lt;&amp;lt;&lt;/tt&gt; and &lt;tt&gt;Group#member_names&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;This is sort of neat, because it forces a radical form of behavior driven development. Since you&amp;#8217;re not working with the real collaborator objects at all in your tests, you are absolutely forced to work with their expected behaviors and not their implementations.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ve already hinted at some of the downsides of this approach though, in particular, that it is possible for our mocks can get out of sync with reality. We&amp;#8217;ve seen an example of tests that don&amp;#8217;t fail, even though they describe invalid output from &lt;tt&gt;User#name&lt;/tt&gt;. Now let&amp;#8217;s see an example of a change that does cause our original mock-based tests to fail, even though there is nothing wrong with the code itself.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
# replace the Person object with this definition, which simply renames
# Person#name to Person#full_name
#
class Person
  def initialize(full_name)
    @full_name = full_name
  end

  attr_reader :full_name
end

class Group
  # update to call the renamed Person#full_name method
  def member_names
    members.map { |e| e.full_name }
  end
end
&lt;/pre&gt;
&lt;p&gt;When we run the non-mocked version of our tests, nothing fails, because it never explicitly mentions the name attribute on &lt;tt&gt;Person&lt;/tt&gt;. But the same cannot be said for our mocked code, which explicitly creates stubs with a name attribute, as shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def expect_new_member(member_name)
    Person.expects(:new).returns(stub(:name =&amp;gt; member_name))
  end
&lt;/pre&gt;
&lt;p&gt;You can see the test output below as evidence that our mock is now indeed broken.&lt;/p&gt;
&lt;pre&gt;
  1) Failure:
test_adding_members_to_a_group(GroupTest)
    [/home/sandal/devel/practicing-ruby/group.rb:14:in `member_names'
     /home/sandal/devel/practicing-ruby/group.rb:14:in `map'
    ...
unexpected invocation: #&amp;lt;Mock:0x7ff71166e6c0&amp;gt;.full_name()
satisfied expectations:
- expected exactly once, already invoked once: Person.new(any_parameters)
- expected exactly once, already invoked once: Person.new(any_parameters)
- allowed any number of times, not yet invoked:
  #&amp;lt;Mock:0x7ff71166e6c0&amp;gt;.name(any_parameters)
- allowed any number of times, not yet invoked:
  #&amp;lt;Mock:0x7ff71166aac0&amp;gt;.name(any_parameters)
&lt;/pre&gt;
&lt;p&gt;So here we see the knife cuts both ways. While it&amp;#8217;s true that our mocked code doesn&amp;#8217;t need to worry about the actual implementations of anything except the object under test, it sure does tightly bind to the interface, even when changes to those interfaces don&amp;#8217;t affect the object under test.&lt;/p&gt;
&lt;p&gt;This allows us to make the same argument that mockists make about cascading errors, from the other side of the fence. As projects grow bigger, the amount of red tests due to brittle mock objects grows larger and larger, making it harder to see what is actually broken and what needs to be changed. But unlike the problem of noisy directly tested objects, these sort of failures only indicate a problem with the tests, not the code.&lt;/p&gt;
&lt;p&gt;In languages where creating new objects is hard and time consuming, such a trade is probably worth considering. If we had to hand tune a Makefile, set up headers, declare variables, and consider memory management just to add a Person object like we might in C++, there might be a strong argument for how using mocks for driving tests helps you be more agile.&lt;/p&gt;
&lt;p&gt;But in Ruby, in which our first tests can be made to pass with just a single line like the one below, you have to wonder whether the juice is worth the squeeze.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  Person = Struct.new(:name)
&lt;/pre&gt;
&lt;p&gt;One important thing to note is that despite my criticisms, there are folks out there who use very elegant design techniques and testing practices that can minimize the problems I have pointed out. But personally, I feel like these folks succeed in spite of the path they&amp;#8217;ve chosen rather than because of it. The idea that using mocks to force you to think about design may work well as a gateway drug, but then once you&amp;#8217;ve learned how to think about object design on its own, you can chuck out the training wheels and just focus on writing good code.&lt;/p&gt;
&lt;p&gt;The examples I&amp;#8217;ve shown here might be a bit biased towards demonstrating my arguments, but at least should give a starting point for considering these issues on your own.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;ve simultaneously shown in this article that mock objects are both really damn useful and ridiculously annoying at the same time. Personally, I tend to shy away from tooling that requires you to swallow a large amount of dogma and a boatload of theory before you can even make use of it, and that is the main reason why I&amp;#8217;m concerned about the whole mockist approach to things. From what I&amp;#8217;ve seen, while a stereotypical &lt;em&gt;classicist&lt;/em&gt; is hard to come by, these &lt;em&gt;mockist&lt;/em&gt; folks that Fowler describes do exist and in my opinion, do more harm than good in getting folks to write clear, easy to understand Ruby code.&lt;/p&gt;
&lt;p&gt;Mocking frameworks are big guns, and should be treated as such. They can be lifesavers in times where all your other options suck, but can cause you to pull your hair out if you use them inappropriately.&lt;/p&gt;
&lt;p&gt;In summary, it&amp;#8217;s a bad idea to swallow bad tasting medicine with the abstract promise that it will be better for you in the end. If you can see clear benefits from the use of mocks and have weighed them out on a case by case basis against your other options, you should be fine. But if you are mostly using them because the RSpec team tells you to, you&amp;#8217;re basically screwed :)&lt;/p&gt;
&lt;p&gt;My final disclaimer about what I&amp;#8217;ve said here is that it is entirely based on my own experiences. You&amp;#8217;ve probably worked on different problems in different environments than I have, and I&amp;#8217;d love to know how those experiences have influenced your own thoughts on mocking. So, what do you think?&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Did you enjoy this article? If so, you should consider signing up &lt;a href="http://practicingruby.com"&gt;Practicing Ruby: Volume 2&lt;/a&gt;, which features my more recent work.&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Mon, 15 Aug 2011 20:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/052-issue-20-thoughts-on-mocking.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/052-issue-20-thoughts-on-mocking.html</guid></item><item><title>Issue #19: Thoughts on Mocking (1 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on January 19, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;When I originally published my &lt;a
href="http://blog.rubybestpractices.com/posts/gregory/050-issues-18-testing-dogma.html"&gt;small rant on testing&lt;/a&gt;, it generated a spirited discussion about a number of different topics. It even lead &lt;a href="http://twitter.com/bryanl"&gt;Bryan Liles&lt;/a&gt; to post a great set of &lt;a href="https://gist.github.com/785610"&gt;testing guidelines&lt;/a&gt; to balance out my unfocused rant. But the topic that overshadowed almost everything else was that of best practices regarding mock objects. In this two part article, we&amp;#8217;ll try to shine some light on that topic, because it is clearly still a point of confusion and occasionally even controversy within our community.&lt;/p&gt;
&lt;p&gt;In Issue #20, I will go over some examples of when I use mock objects and when I don&amp;#8217;t, and try to come up with some guidelines for building test suites that do their job without becoming too brittle. But before we can really discuss practices, we need to establish a baseline level of theory and background knowledge, which is what this post is all about.&lt;/p&gt;
&lt;p&gt;Rather than doing the heavy lifting myself, I will point you to what is essentially &lt;strong&gt;the&lt;/strong&gt; article to read to better understand mock objects. It was originally written in 2004 (which is about the time that I first read it), and then revised in 2007. It is of course, Martin Fowler&amp;#8217;s essay &lt;a href="http://martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks Aren&amp;#8217;t Stubs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The article is long, somewhat dry, and includes large amounts of Java code. Don&amp;#8217;t let that discourage you from reading the whole thing from end to end, and if necessary, reading it again. Despite the title, Fowler goes into much deeper topics than mocks vs. stubs, and hits on many of the key ideas that separate &amp;#8216;mockists&amp;#8217; from &amp;#8216;classicist&amp;#8217;. Personally, I feel this is a false dichotomy, but you&amp;#8217;ll still be hard pressed to find a better article that gives the historical background of the design ideas that motivated the creation of testing and mocking frameworks in the first place.&lt;/p&gt;
&lt;p&gt;I find Fowler&amp;#8217;s assessment to be reasonable fair, incredibly comprehensive, and a very useful place to start from if you are to form any argument about one approach vs. another when it comes to mocking. That having been said, I am critical of certain aspects of this essay, partly because I am looking at it with a 2011 perspective, and partly because I didn&amp;#8217;t come to Ruby from Java. For this reason, I&amp;#8217;ve included my commentary on Fowler&amp;#8217;s article below. I encourage you to read his article in full before reading my comments, as they&amp;#8217;ll make much more sense that way.&lt;/p&gt;
&lt;h3&gt;Commentary on Fowler&amp;#8217;s &amp;#8220;Mocks Aren&amp;#8217;t Stubs&amp;#8221;&lt;/h3&gt;
&lt;p&gt;Fowler explores two different concepts in this article: behavior vs. state based verification, and classical vs. mockist &lt;span class="caps"&gt;TDD&lt;/span&gt;. While he doesn&amp;#8217;t directly draw the lines between them, he sort of implies that mockists are always focusing on behavior verification and that classical &lt;span class="caps"&gt;TDD&lt;/span&gt; leans heavily towards state based verification. There are some issues with this line of thinking.&lt;/p&gt;
&lt;p&gt;Claiming that mockists inherently focus on behavior is valid. The idea of mocking everything except the object under test means that purists would not be able to work with &amp;#8216;real objects&amp;#8217; to perform state verification on. But this sort of practice does not actually require mocking everything except the object under test, what it requires is more carefully written tests.&lt;/p&gt;
&lt;p&gt;Fowler claims that classicists tend towards writing single tests that explicitly test large clusters of code simultaneously, which requires them to produce a large amount of fixture data just to get their tests to run. But in a post-&lt;span class="caps"&gt;BDD&lt;/span&gt; world, most people know how to isolate their test cases so that they focus on one behavior at a time, whether or not they&amp;#8217;re utilizing mock objects. We also know to write comprehensive tests at both the higher and lower levels of our project, and so it isn&amp;#8217;t necessary to worry about exercising all the possible paths through our low level objects when calling them through a high level interface.&lt;/p&gt;
&lt;p&gt;Personally, when I&amp;#8217;m testing a feature that is towards the top layer of my stack, I try to make it so it requires as little configuration as possible to initialize. It shouldn&amp;#8217;t be necessary to load up fixture data for low level features I won&amp;#8217;t use, so really, I only need to trace a single path of execution and provide the right data to make it a valid path. I weigh the cost of this against using a mock object, and whenever the two are comparable, I prefer the former. Clearly this doesn&amp;#8217;t make me a mockist, but does it fit with Fowler&amp;#8217;s definition of a classicist? I don&amp;#8217;t know.&lt;/p&gt;
&lt;p&gt;I was never deeply involved in Java programming, but from my limited experience with it, I feel that a lot of the arguments Fowler formed in this essay were and probably still are more relevant in the Java world. In Java, because you don&amp;#8217;t have things like mixins, indirection is much more common than in Ruby. You might need to create 6 objects just to do one small simple thing. In such an environment, mock objects must seem like a godsend, as when you multiply that phenomena across your entire project, the cost of maintaining mocks would be far, far, less than the cost of building complex setups for all those objects. But odds are, if you&amp;#8217;re experiencing the same sorts of problems in Ruby, you have a horrible design for your project.&lt;/p&gt;
&lt;p&gt;In Ruby, it is possible and often recommendable to build systems that don&amp;#8217;t have very deep object nesting. For this reason, the ability to focus only on mocking direct neighbors of an object under test isn&amp;#8217;t as much of a selling point. If we take away the complex object systems component, we are mostly left with the idea that mockists prefer to write mocks so that they can focus on driving the object under test, and then go back to use their mocks as a contract for the next object they need to create. Again, something that makes a lot of sense in languages that punish you for creating new objects. Ruby is not like that.&lt;/p&gt;
&lt;p&gt;In almost every scenario I can imagine, it&amp;#8217;s better to just go ahead and create a skeleton version of an object you need than it is to form a mock that is sort of floating in space. It will likely take less time, and working with the real object will give better insight into its design than trying to dream it up through a cumbersome mock interface. Fowler does touch on this approach being a valid one but claims that the mockist approach provides more design guidance. I don&amp;#8217;t see any evidence to support this claim, as the two are essentially functionally equivalent with respect to the object under test.&lt;/p&gt;
&lt;p&gt;Fowler does an excellent job of covering the arguments about test isolation, and I don&amp;#8217;t have too much to add there except to say that I am firmly in favor of watching my whole test suite go up in smoke when I make a far reaching change. The false-positives that mocks give are downright dangerous in these scenarios, and arguments about it being difficult to find what caused the breakage are most likely an indication of some deeper problem: I&amp;#8217;ve never had that issue even on my most complex projects.&lt;/p&gt;
&lt;p&gt;Fowler&amp;#8217;s entire discussion about Design Style for classicists vs. mockists misses the mark. It probably had a lot of truth to it at the time he wrote the article, and may still have some truth outside of Ruby. But really, what he is describing here is the distinction between old fashioned regression-suite style &lt;span class="caps"&gt;TDD&lt;/span&gt; and what we now call Behavior Driven Development. In my opinion, &lt;span class="caps"&gt;BDD&lt;/span&gt; is just a new style of &lt;span class="caps"&gt;TDD&lt;/span&gt; that is more principled and focused on design as a first class component of writing testable code. So when Fowler says that mockists favor role based systems, I think this actually applies more generally to anyone practicing modern &lt;span class="caps"&gt;TDD&lt;/span&gt;.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;As I said at the very beginning of this article, I think the distinction between mockists and classicists is a false dichotomy. I do agree that there is a wide chasm to cross between the original purpose of test frameworks and the new way of looking at things. But really, once you&amp;#8217;ve decided that tests are more than just a safety net for dealing with regressions, you have already fallen outside of Fowler&amp;#8217;s classicist point of view. In my opinion, there is room for people who focus on behavior rather than state, but don&amp;#8217;t necessarily feel like mock objects are a good tool to be using by default. These folks are just as concerned about design and driving code through tests, but do not subscribe to absolutist viewpoints that require a single technique to be used at all times.&lt;/p&gt;
&lt;p&gt;Since I consider myself to be in the third category that I&amp;#8217;ve wedged between Fowler&amp;#8217;s two groups, I will need to share some examples of what that means in practical terms. The next article should help with that, because it provides an outline of how I decide when to mock and when to use real objects instead. Until then, I&amp;#8217;d be happy to hear your thoughts on this topic, especially what you think of Fowler&amp;#8217;s article.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 29 Jul 2011 14:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/051-issue-19-thoughts-on-mocking.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/051-issue-19-thoughts-on-mocking.html</guid></item><item><title>Issue #18: Dirty Little Secrets about Testing</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on January 14, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Today I have a small rant about the test obsessed culture of Ruby. This area in particular is one in which the incredible enthusiasm surrounding the methodology seems to have outpaced the individual practitioner&amp;#8217;s ability to evaluate its utility. I&amp;#8217;m not just talking about beginners here, but seasoned professionals as well. The sheer volume of rapidly changing testing tools and techniques is an indicator that we&amp;#8217;re nowhere near convergence, but that doesn&amp;#8217;t stop many from describing automated testing, particularly in the &lt;em&gt;Behavior Driven Development&lt;/em&gt; style, as if it were some sort of magic bullet. It isn&amp;#8217;t.&lt;/p&gt;
&lt;p&gt;Speaking only from personal experience, I can tell you that most projects that I&amp;#8217;ve spent more than a few hours hacking on end up better with some automated testing than they would with no testing at all. But I can also tell you that uses all the latest and greatest tools and techniques, and attempting to &amp;#8220;test all the fucking time&amp;#8221; on large scale, long running projects, has actually hurt rather than helped me at times.&lt;/p&gt;
&lt;p&gt;Some tests are a waste of time, and other tests can be actively harmful. If you have ever experienced the pain of refactoring a test suite in which someone overzealously mocked everything except for the object under test, simply to &amp;#8216;decouple the tests from the implementation code&amp;#8217;, you probably have an idea of what I mean. If you happen to be one of those folks who are still writing tests that way, you should study a bit more about what mocks are actually meant to be used for.&lt;/p&gt;
&lt;p&gt;Personally, I&amp;#8217;ve found testing to get in the way when I&amp;#8217;m first exploring a new project. I almost always spend a couple hours writing absolutely crappy code without any tests at all. Test-first development does help drive interfaces and forces you to think about &lt;span class="caps"&gt;API&lt;/span&gt; design continuously, but it can only really be used to attain a local maximum. When you&amp;#8217;re trying to get a feel for a whole new concept, what you really need is a lantern, not a laser. Not even story frameworks like Cucumber or Steak give me the level of flexibility I need, so I just go without testing frameworks entirely in my initial spikes. The closest thing I get to &amp;#8216;automated testing&amp;#8217; in the first few hours of a project is a few lines of example code combined with some printlining to let me see what my code is doing. Pretty much everything else gets done through poking and prodding in irb.&lt;/p&gt;
&lt;p&gt;Typically, the tradeoff of velocity for code quality that I make in a spike fairly quickly catches up with me, and that causes me to start to think about adding tests and basically just starting from scratch, only using code if I feel it&amp;#8217;s good enough to refactor into something more permanent. With enough ideas generated, and a decent high level sense of what my goals are, the laser-like quality of unit testing becomes more useful. But there is still a lot of things I don&amp;#8217;t test, even once a project is under way.&lt;/p&gt;
&lt;p&gt;I don&amp;#8217;t test complex interactions with users within a system, unless I begin to frequently write code that has system-wide effects. I&amp;#8217;ve definitely been in situations in which integration tests have been vital, but they&amp;#8217;ve been far and few in between. Part of this is because the projects I work on tend to be deeper than they are wide, but it&amp;#8217;s also because I just trust my design capabilities enough to not introduce too many changes that could break more than one part of my application at a time. I feel like the majority of integration testing goes into way too much detail about the expected paths through a system, and as a result, forces a bunch of false-negatives as minor changes that shouldn&amp;#8217;t affect users end up breaking tests.&lt;/p&gt;
&lt;p&gt;Similarly, I don&amp;#8217;t place too much emphasis on testing things that I will invariably need to manually inspect. So for example, if I&amp;#8217;m generating a &lt;span class="caps"&gt;PDF&lt;/span&gt; report, I don&amp;#8217;t typically bother testing my output in an automated fashion. What I will do instead is make it dead easy for me to generate that &lt;span class="caps"&gt;PDF&lt;/span&gt; so that I can look at it whenever I need to, and I&amp;#8217;ll keep a copy of the expected output around so that I can track down issues when they come up by doing some manual comparisons. Things would change somewhat if generating &lt;span class="caps"&gt;PDF&lt;/span&gt; reports was the core purpose of my application, but as a single feature, I feel automated testing would be mostly a waste of time.&lt;/p&gt;
&lt;p&gt;There are other areas about testing that concern me, but I&amp;#8217;ll leave them for another day. For now, I&amp;#8217;ll try to end things on a positive note by sharing some of the areas where I do think testing is really, really helpful.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Dealing with regressions: In most scenarios, once you&amp;#8217;ve created a minimal example to reproduce a bug, it&amp;#8217;s a small step to convert it into a unit test. As long as you introduce the test as far down the stack as possible, this minor investment is well worth the effort, as it will catch the bug and draw your attention to it if it ever gets reintroduced into your project.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Documenting project requirements: When written properly, tests say a lot more about intentions than implementation code does. Some folks feel that something like Cucumber and/or RSpec does a better job at expressing requirements than more low level testing frameworks, but this is primarily an aesthetic argument. No matter what framework you use, the purpose of a test is to describe how some code should be expected to work, which makes test suites a great way to learn about a project.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Safeguarding against harmful changes: For long running projects or projects with many developers, automated testing helps detect changes that have undesireable or unexpected side effects on the overall system. This is something that can also be dealt with by being well organized and fairly disciplined, but tests sure don&amp;#8217;t hurt. Of course, this effect is only accomplished if there is sufficient test coverage to catch those unexpected changes, an investment that may or may not be worth the effort.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that in the above, I didn&amp;#8217;t imply that testing results in writing better code. I also specifically avoided claiming that tests will help you avoid defects in the first place. While I think that occasionally testing contributes to accomplishing these two things, it really depends on the project as well as the individual developer&amp;#8217;s skill level and coding style. I also didn&amp;#8217;t claim that writing tests saves time or money. I don&amp;#8217;t think it actually does, and I wouldn&amp;#8217;t trust that claim until I saw some concrete evidence.&lt;/p&gt;
&lt;p&gt;Even with these caveats, I think the gains listed in the bullet points above make the juice worth the squeeze, in most cases. I can also say that automated testing does make you think about software development in a very different way, and that the change in perspective might make you a better programmer. But testing is just one of many things that will help you improve your craft.&lt;/p&gt;
&lt;p&gt;So my humble advice to all Rubyists, newbies and seasoned professionals alike, is to cool your jets when it comes to testing. If we remember our main goal is to produce useful software, we can find room to make use of helpful techniques without letting them take center stage. This I think would be a huge step in the right direction.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Mon, 25 Jul 2011 14:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/050-issues-18-testing-dogma.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/050-issues-18-testing-dogma.html</guid></item><item><title>Issue #17: Interesting Ruby Hacker-Writers</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on January 7, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;a
href="http://blog.rubybestpractices.com/posts/gregory/048-issue-16-interesting-ruby-hackers.html"&gt;last issue&lt;/a&gt; we covered five folks who are working on really interesting projects. In this issue, I&amp;#8217;d like to pay some attention to five other Ruby hackers who have caught my attention through their excellent technical writing. As a technical author myself, I am particularly moved by folks who educate and inform our community through more than just code, but through written words as well.&lt;/p&gt;
&lt;p&gt;Similar to Issue #16&amp;#8217;s list, this lineup is in no particular order, is far too short to do the community justice, and is purely subjective in nature. None of these folks knew I was going to write about them and my recommendations are completely unsolicited.&lt;/p&gt;
&lt;h3&gt;Magnus Holm (&lt;a href="http://twitter.com/judofyr"&gt;@judofyr&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;If you&amp;#8217;ve been on Twitter or Hacker News lately, you probably have run into an article or two from &lt;a href="http://timelessrepo.com"&gt;Timeless&lt;/a&gt;, a blog that Magnus is running with help from Steve Klabnik. The topics here range from the philosophical (see: &lt;a href="http://timelessrepo.com/there-is-no-talent"&gt;There is No Talent&lt;/a&gt;), to the obscure low-level Ruby hack (see: &lt;a href="http://timelessrepo.com/tailin-ruby"&gt;Tailin&amp;#8217; Ruby&lt;/a&gt;), but the quality is consistently well above that of your run of the mill blog.&lt;/p&gt;
&lt;p&gt;As it turns out, the reason why Timeless feels different than your average blog is because it is designed to be that way. Magnus describes the project as &lt;i&gt;&amp;#8220;an attempt at creating a new kind of blog with a focus on frequently updated, quality content which last longer than the beta of your favorite framework&amp;#8221;&lt;/i&gt;. With so much web content being incredibly ephemeral in nature, the notion of timeless content is attractive, even if it means a considerable challenge for the author.&lt;/p&gt;
&lt;p&gt;As a small historical note, Magnus is also one of the five folks who has written articles for the Ruby Best Practices blog. While he only wrote a total of &lt;a href="http://blog.rubybestpractices.com/posts/judofyr/index.html"&gt;two articles&lt;/a&gt; for us, both were very well received by our readers. So even if I can&amp;#8217;t take credit for his recent work, I can at least claim that I knew his potential years ago :)&lt;/p&gt;
&lt;h3&gt;Jeff Kreeftmeijer (&lt;a href="http://twitter.com/jkreeftmeijer"&gt;@jkreeftmeijer&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Jeff is another blogger who has rapidly produced &lt;a href="http://jeffkreeftmeijer.com/archive/"&gt;a ton of great material&lt;/a&gt; over the last year or so. The topics on his blog bounce around fairly frequently, though he seems to take a particular interest in developer practices, particularly surrounding git, gem packaging, and testing tools.&lt;/p&gt;
&lt;p&gt;What interests me about Jeff&amp;#8217;s writing is that while he&amp;#8217;s talking about fairly common topics, he typically focuses on just a single discussion point, which makes each of his posts a conversation waiting to happen. The process of watching Jeff make a point about some opinion he has formed about a given tool or technique and then seeing the community respond to that point has been a really enjoyable experience so far. For a nice example of what I&amp;#8217;ve just described, check out Jeff&amp;#8217;s &lt;a href="http://jeffkreeftmeijer.com/2010/be-awesome-write-your-gemspec-yourself/"&gt;Be awesome, write your gemspec yourself&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Jeff&amp;#8217;s conversational approach to writing really leads to some productive conversations across a broad cross-section of the Ruby community. It&amp;#8217;s easy to see this phenomenon by either looking at the comments on the article above, or any other article of Jeff&amp;#8217;s that has gained some widespread attention (read: most of them).&lt;/p&gt;
&lt;p&gt;Similar to Magnus, I admire Jeff for breaking out of the standard &amp;#8216;blogging template&amp;#8217; and developing his own writing style that seems to work quite well.&lt;/p&gt;
&lt;h3&gt;MenTaLguY (&lt;a href="http://twitter.com/#!/MenTaLguY"&gt;@MenTaLguY&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;I may be showing my long, flowing Ruby beard by recommending MenTaLguY on a list of bloggers who impressed me in 2010, since the bulk of the materials of his that interested me were published in 2005-2006. But two articles posted in 2010 really caught my eye, and are worth recognition on their own: &lt;a href="http://moonbase.rydia.net/mental/blog/programming/atomic-operations-in-ruby.html
"&gt;Atomic Operations in Ruby&lt;/a&gt; and &lt;a href="http://moonbase.rydia.net/mental/blog/programming/the-biggest-mistake-everyone-makes-with-closures.html"&gt;The Biggest Mistake Everyone Makes With Closures&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;These two articles are guaranteed to expose edge cases that will surprise all but the most diligent Rubyists, and are representative of the two things MenTaLguY has historically been known for: concurrency and functional programming.&lt;/p&gt;
&lt;p&gt;His blog is written in the old fashioned &amp;#8216;everything in one pot&amp;#8217; style, and due to a mixture of non-technical and technical content, can be very challenging to dig through. For this reason, I&amp;#8217;ve gone way back to his 2006 and 2007 writing and pulled a few articles worth checking out, which may intice you to dig even deeper.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/blog/programming/simple-lazy-streams.html"&gt;Simple Lazy Streams&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/blog/programming/currying-in-ruby.html"&gt;Currying in Ruby&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/"&gt;Monads in Ruby&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/blog/programming/concise-memoization.html"&gt;Concise Memoization&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions.html"&gt;Eavesdropping on Expressions&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/blog/programming/ruby-symbols-explained.html"&gt;Ruby Symbols Explained&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://moonbase.rydia.net/mental/blog/programming/concurrency-five-ways.html"&gt;Concurrency Five Ways&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The reason I&amp;#8217;ve included MenTaLguY in this lineup is because I feel like he is a representation of an unapologetically deeply technical person that we are seeing less and less of in a more commercialized ecosystem that tends to value shiny tools and productivity tips over deep knowledge and theory. While the new world isn&amp;#8217;t a bad one because we all can find jobs in it, I remember a different community that leaned more towards MenTaLguY&amp;#8217;s direction as recent as five years ago.&lt;/p&gt;
&lt;p&gt;So this is my hat-tip to old school Rubyists, and to MenTaLguY for frequently being ahead of his time by focusing on concurrency and functional programming before it was cool.&lt;/p&gt;
&lt;h3&gt;Aaron Patterson (&lt;a href="http://twitter.com/tenderlove"&gt;@tenderlove&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Aaron is another Ruby hacker who has been in the Ruby community for a long time and has worked on a number of cool projects, including Nokogiri and Mechanize. I&amp;#8217;ve been a friend of Aaron&amp;#8217;s for a while, and have always admired his abilities as both a hacker and story teller. But the thing that really impressed me about Aaron&amp;#8217;s work in 2010 is his writing for the AT&amp;amp;T Interactive Engineering.&lt;/p&gt;
&lt;p&gt;Recently, Aaron has been performance tuning Rails 3 by rewriting the low level ARel relational algebra library. Doing major refactorings of Rails internals is no easy task, but you might think otherwise if you read Aaron&amp;#8217;s great posts about his work on ARel, due to their incredible clarity. It may just be the giant fonts or the pretty graphs, but the articles Aaron has written about his work on ARel have been the most easy to understand resources on performance tuning in Ruby that I have ever seen.&lt;/p&gt;
&lt;p&gt;Aaron brushes up against a couple other Rails and Ruby 1.9.2 topics on the AT&amp;amp;T blog as well, approaching them in a similar clear and light-hearted style. But those looking for a lot more can dig through the archives of tenderlovemaking.com, which similarly has no shortage of great Ruby content.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.20: Looks like the AT&amp;amp;T interactive blog is down right now, so I couldn&amp;#8217;t provide any meaningful links above. But you can still find Aaron writing on his personal blog, &lt;a href="http://tenderlovemaking.com"&gt;Tender Lovemaking&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Peter Cooper&lt;/h3&gt;
&lt;p&gt;It may be a bit &amp;#8220;meta&amp;#8221; to acknowledge Peter, since he&amp;#8217;s typically writing about people who are writing code, or writing about people who are writing about writing code, but nonetheless, he is someone that deserves both attention and appreciation for the valuable service he&amp;#8217;s been offering our community for years.&lt;/p&gt;
&lt;p&gt;While I find a lot of new Ruby resources over Hacker News or Twitter, or just by working with my friends and colleagues, &lt;a href="http://rubyinside.com"&gt;Ruby Inside&lt;/a&gt; remains the place I go when I want to put my finger on the pulse of the Ruby community. While small things may go unnoticed by Peter, most major new releases of important projects are covered by Ruby Inside. RI also is the place to go to see announcements about big upcoming events, or just to get a general feel for what Ruby hackers are up to.&lt;/p&gt;
&lt;p&gt;A number of my own projects, including Practicing Ruby itself, have gained a lot more attention by being featured on Ruby Inside than they would have on their own. Over the years, I&amp;#8217;ve seen this site evolve from a time in which the existence of a Ruby news site seemed a bit ridiculous since everyone in the community already pretty much knew each other to the present day in which we absolutely need to have someone sifting through the endless stream of new content so that we know what things we just can&amp;#8217;t afford to miss.&lt;/p&gt;
&lt;p&gt;For better or for worse, Peter has done a ton to help Ruby gain exposure in the broader technical community, and has helped those within our community find their way to some really great resources. The high degree of professionalism and consistency that Ruby Inside showcases does a lot to create a good first impression for our community as a whole.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.20: Ruby Inside is great, but less frequently updated these days. Be sure to check out other things Peter stewards, such as &lt;a href="http://rubyweekly.com/"&gt;Ruby Weekly&lt;/a&gt; and &lt;a href="http://rubyflow.com"&gt;Ruby Flow&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;I must admit, I actually don&amp;#8217;t spend a lot of time reading technical books or blogs. I find that I&amp;#8217;m so busy writing and actively practicing that hooking myself up to the firehose of new information seems like it would cause me to burst. In general, I think we have a community that is too obsessed with the consumption of information, and for that reason, it makes it hard for me to come up with a list of folks who I&amp;#8217;d say can&amp;#8217;t be missed.&lt;/p&gt;
&lt;p&gt;That having been said, a little bit of well placed information goes a long way, and the five people I have acknowledged in this post have really given me some major new insights at one point or another in my career. There are others I could say the same about, but I&amp;#8217;ll leave it to you to find them on your own.&lt;/p&gt;
&lt;p&gt;Who is a great hacker-writer that you think is worth knowing about?&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 21 Jul 2011 13:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/049-issues-17-interesting-ruby-writers.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/049-issues-17-interesting-ruby-writers.html</guid></item><item><title>Issue #16: Interesting Ruby Hackers</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on January 3, 2011. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#8217;ve listed five people worth knowing about if you&amp;#8217;re involved in Ruby. If you&amp;#8217;re reading this in July 2011, please note that I wrote this article over 7 months ago, and so the descriptions you see below are slightly outdated. That having been said, I still think these five people are on the top of my list when it comes to interesting folks in our community.&lt;/p&gt;
&lt;h3&gt;Wayne Seguin (&lt;a href="http://twitter.com/wayneeseguin"&gt;@wayneeseguin&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Wayne gave us &lt;a href="http://rvm.beginrescueend.com"&gt;&lt;span class="caps"&gt;RVM&lt;/span&gt;&lt;/a&gt;, the Ruby enVironment Manager. This tool quickly evolved from a bunch of crude shell script hacks to something that makes working with multiple Ruby versions and implementations a breeze. A tool which simply allowed manually switching between versions and implementations of Ruby would be useful on its own, but the thing that makes &lt;span class="caps"&gt;RVM&lt;/span&gt; special are all the shiny extras that come with it.&lt;/p&gt;
&lt;p&gt;In addition to basic version switching, &lt;span class="caps"&gt;RVM&lt;/span&gt; provides gemsets which are sandboxes for your gem installation environment. This makes it possible for each of your projects to have its own gemset, eliminating concerns about different projects having dependencies that clash with one another. While this is a problem that can often be solved by version locking, having an extra layer of protection and organization is great.&lt;/p&gt;
&lt;p&gt;Another neat feature of &lt;span class="caps"&gt;RVM&lt;/span&gt; is the ability to include a &lt;tt&gt;.rvmrc&lt;/tt&gt; in any of your project roots, which causes &lt;tt&gt;rvm&lt;/tt&gt; to automatically switch to the desired ruby version, implementation, and gemset that you specify in that file. This reduces the amount of manual switching needed, and makes commands like &lt;tt&gt;ruby&lt;/tt&gt;, &lt;tt&gt;irb&lt;/tt&gt;, &lt;tt&gt;rake&lt;/tt&gt;, and &lt;tt&gt;gem&lt;/tt&gt; &amp;#8216;just work&amp;#8217; without having to think about what context you are in.&lt;/p&gt;
&lt;p&gt;Another thing that is amazing about &lt;span class="caps"&gt;RVM&lt;/span&gt; is the amount of support Wayne offers for it. He is nearly infamous for his availability on &lt;span class="caps"&gt;IRC&lt;/span&gt;, and he seems to genuinely want to help anyone who is trying to use &lt;span class="caps"&gt;RVM&lt;/span&gt;. I&amp;#8217;ve seen him cornered at least a few times at Ruby conferences by folks asking questions about how to do this or that with &lt;span class="caps"&gt;RVM&lt;/span&gt;, and he always seems to handle those situations gracefully. This is exactly the kind of spirit that makes me appreciate someone&amp;#8217;s work and makes me want to keep watching them to see what great things they&amp;#8217;ll come up with.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.19: You should also check out Wayne&amp;#8217;s &lt;a href="http://bdsm.beginrescueend.com"&gt;&lt;span class="caps"&gt;BDSM&lt;/span&gt; framework&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Eleanor McHugh (&lt;a href="http://twitter.com/feyeleanor"&gt;@feyeleanor&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Eleanor McHugh is an incredibly clever and entertaining hacker who has a deep interest in concurrency and low level &lt;span class="caps"&gt;UNIX&lt;/span&gt; plumbing. She spent a lot of time in 2010 working on &lt;a href="http://github.com/feyeleanor/GoLightly"&gt;GoLightly&lt;/a&gt;, a lightweight virtual machine running on top of the Go programming language. Her original goal was to re-build miniruby on top of Go, but building the vm became a priority in of itself rather than just a stepping stone once she had a chance to dig into the problem.&lt;/p&gt;
&lt;p&gt;What interests me about Eleanor is that she is the kind of person that decides to work on a project first and then figure out how to make it all come together later. I know she has been making some significant personal sacrifices so that she can work on GoLightly, and that sort of attitude is something I really like to see.&lt;/p&gt;
&lt;p&gt;Eleanor was one of the guest speakers at Mendicant University in 2010, doing a Q&amp;amp;A session with me and the students. We touched on how pretty much every modern language handles concurrency, and then somehow deviated to discussing Eleanor&amp;#8217;s background in avionics, in which we collectively decided that &lt;span class="caps"&gt;TDD&lt;/span&gt; in that field worked something like &amp;#8220;Whoops, the plane crashed, guess that&amp;#8217;s red.&amp;#8221; This of course lead us to a more serious discussion about testing and testability, but was a pretty hilarious diversion along the way.&lt;/p&gt;
&lt;p&gt;Where one can really learn a ton from Eleanor is in a small group or one on one conversation. She is the ideal person to catch up with on the hallway track of a conference, or to grab a drink with after an event. Each time I&amp;#8217;ve met up with her I&amp;#8217;ve been consistently entertained and inspired by her stories, and find myself fortunate to be able to call her a friend.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.19: Eleanor, like me, spends most of her time hacking on community projects. &lt;a href="http://pledgie.com/campaigns/15689"&gt;She can use some help with her travel expenses&lt;/a&gt;, so if you like what she&amp;#8217;s doing, please do contribute what you can.&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Brian Ford (&lt;a href="http://twitter.com/brixen"&gt;@brixen&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Brian is one of the key &lt;a href="http://rubini.us"&gt;Rubinius&lt;/a&gt; team members and also was instrumental in the creation and adoption of the &lt;a href="http://github.com/rubyspec/rubyspec"&gt;RubySpec&lt;/a&gt;, an executable specification of the Ruby language written in RSpec-like syntax.&lt;/p&gt;
&lt;p&gt;While I do not closely follow the development of Rubinius, I studied it a bit when researching for a talk on Ruby versions and implementations. In the process, I came to learn about RubySpec and the specialized testing framework they&amp;#8217;ve built for it called &lt;a href="https://github.com/rubyspec/mspec"&gt;mspec&lt;/a&gt;. This stuff is seriously cool.&lt;/p&gt;
&lt;p&gt;As you can imagine, building a testing framework to test Ruby itself is a harder problem than simply testing code you write using Ruby. To account for this, mspec does all sorts of neat things, allowing tests to be restricted to particular versions, implementations, and even specific patch levels of different Ruby packages. Another interesting aspect of mspec&amp;#8217;s implementation is that because it&amp;#8217;s designed to help Ruby implementers test their work, the code for implementing the testing framework intentionally uses a minimal subset of Ruby functionality. As someone interested in tricky design problems, I found myself consistently impressed by how mspec is implemented. While I&amp;#8217;m not sure exactly how much of this is Brian&amp;#8217;s handiwork, he is one of the key folks who set the project in motion.&lt;/p&gt;
&lt;p&gt;RubySpec itself is really impressive. If you haven&amp;#8217;t looked through it before, I strongly encourage that you do so. It provides comprehensive unit tests for a huge amount of Ruby&amp;#8217;s behavior, covering each feature in minute detail. I guarantee you that if you spend a little time reading through the specs, you&amp;#8217;ll find an edge case about some Ruby feature that you didn&amp;#8217;t know about, no matter how solid your understanding of Ruby is.&lt;/p&gt;
&lt;p&gt;&lt;strike&gt;While we haven&amp;#8217;t officially announced the details, Brian and I will be working together to run Ruby Mendicant University&amp;#8217;s first Free Software Clinic. This will be a chance for some of our students to work with me as we contribute something interesting that should make RubySpec even more useful than it already is. More information will come about this topic soon.&lt;/strike&gt;&lt;/p&gt;
&lt;p&gt;In addition to his work on Rubinius and RubySpec, Brian happens to be an incredible teacher. While most of my interactions with him have been over &lt;span class="caps"&gt;IRC&lt;/span&gt;, he is capable of explaining complex and deep computer science topics in a way that makes them feel natural and manageable. I finally had a chance to see him give a talk in person at RubyConf 2010, and by watching &lt;a href="http://confreaks.net/videos/454-rubyconf2010-poisoning-rubinius-the-_why-and-how"&gt;this video&lt;/a&gt;, I think you&amp;#8217;ll get a sense of what I mean.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.19: Brian and I haven&amp;#8217;t had a chance to work on open source projects together with the Mendicant University students yet, but I hope we&amp;#8217;ll have a chance to do so some time in the not-too-distant future. I struck the mention of our plans out in the description above to make it clear this original plan didn&amp;#8217;t pan out.&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Tony Arcieri (&lt;a href="http://twitter.com/bascule"&gt;@bascule&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Tony is another Ruby hacker interested in concurrency, particularly the Actor model of concurrency. He has built a number of concurrency tools in Ruby, including &lt;a href="http://github.com/tarcieri/rev"&gt;rev&lt;/a&gt; and &lt;a href="http://github.com/tarcieri/revactor"&gt;revactor&lt;/a&gt;, but eventually decided that what he really wanted was the syntax of Ruby with the baked in concurrency model of Erlang. This lead him to begin work on his own language, &lt;a href="http://github.com/tarcieri/reia"&gt;Reia&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For those who haven&amp;#8217;t seen it before, Reia is a fascinating language, even in its infancy. The syntax does look and feel like Ruby, but everything is Erlang under the hood. The functionality is mapped more towards Erlang than it is towards Ruby, which means that Reia is not aiming to be a feature complete Ruby implementation. Working in Reia is an interesting exercise in wondering what a smaller, more basic subset of Ruby&amp;#8217;s functionality might look like.&lt;/p&gt;
&lt;p&gt;The neat thing about Reia is that a lot of its code is self hosting, similar to Rubinius. This, combined with the fact that you can easily reach down to the Erlang runtime and call functions provided in Erlang&amp;#8217;s core modules, makes it very easy to contribute to Reia&amp;#8217;s high level feature set. During RubyConf 2010 I decided to dip my toe in and help wrap a number of the methods in Erlang&amp;#8217;s List &lt;span class="caps"&gt;API&lt;/span&gt; to make them look and feel like the features provided by Ruby&amp;#8217;s Enumerable module, and I found contributing to the project very easy.&lt;/p&gt;
&lt;p&gt;Tony is another hacker who is gifted at being a bit irreverent towards what are typically considered &amp;#8216;hard problems&amp;#8217;, and like Brian Ford, he is good at helping you understand that building a programming language isn&amp;#8217;t quite as hard as you might think. You can check this out for yourself by watching his &lt;a href="http://confreaks.net/videos/457-rubyconf2010-rev-revactor-reia"&gt;RubyConf 2010 talk&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.19: Tony&amp;#8217;s projects move fast. I wouldn&amp;#8217;t be surprised if everything above is now out of date, but hunt down whatever he&amp;#8217;s working on now and you won&amp;#8217;t be disappointed.&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Eric Hodel (&lt;a href="http://twitter.com/drbrain"&gt;@drbrain&lt;/a&gt;)&lt;/h3&gt;
&lt;p&gt;Eric has been in the Ruby community for as long as I can remember, and as a member of the Seattle Ruby Group, he automatically can be recognized as an insanely capable hacker.&lt;/p&gt;
&lt;p&gt;What I feel Eric lacks is enough appreciation from the community for the very thankless work he was doing. Anyone who was around in Ruby before Rails knows that RubyGems greatly outgrew its initial design a long time ago. The code, originally hacked together at a conference, was never really meant to live in a world in which gem downloads are measured in the millions rather than the hundreds.&lt;/p&gt;
&lt;p&gt;Similar arguments could be made about projects such as RDoc. Being able to autogenerate documentation is an important part of any language&amp;#8217;s infrastructure, but when Dave Thomas first put together RDoc, I doubt he could have anticipated how big Ruby would be and how long that code would still remain in active use.&lt;/p&gt;
&lt;p&gt;Most people didn&amp;#8217;t want to touch RubyGems or RDoc, both because of how outdated the code was, and because any small change to either of them could easily piss off the entire Ruby world. But the more that Ruby&amp;#8217;s ecosystem evolved, the more it became clear that fighting against old, janky architecture was a huge waste of time.&lt;/p&gt;
&lt;p&gt;Little by little, Eric worked towards fixing up both of these projects. Now, both RDoc and RubyGems are much, much better than what they were before. Each have extension systems that make it so that the core code can continue to get smaller and simpler over time, rather than the other way around. In the case of RubyGems, that extension system brought us Gemcutter (now rubygems.org), which is now the official means of distributing gems to the Ruby community. While we have Nick Quaranto to thank for this innovation, we have Eric to thank for making RubyGems better so that Gemcutter could actually come into existence in the first place.&lt;/p&gt;
&lt;p&gt;If there is one person in the Ruby community that deserves thanks for taking our old and busted tooling and making it serviceable again, it&amp;#8217;s Eric.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2011.07.19: Even despite the RubyGems turbulence over the last several months, I stand by this opinion of Eric&amp;#8217;s contributions 100%&lt;/i&gt;&lt;/p&gt;
&lt;h3&gt;Who&amp;#8217;s interesting to you?&lt;/h3&gt;
&lt;p&gt;These are the folks who caught my interest over the last year or so. Who is someone you think is worth knowing about?&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 20 Jul 2011 13:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/048-issue-16-interesting-ruby-hackers.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/048-issue-16-interesting-ruby-hackers.html</guid></item><item><title>Issue #15: Duck Typing in Practice (2 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 31, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Today, I&amp;#8217;ve got a handful of neat examples to share, each which demonstrates an interesting use of duck typing. We&amp;#8217;ll start by looking a feature built into Ruby&amp;#8217;s core, and then look at a few examples from other open source Ruby projects.&lt;/p&gt;
&lt;h3&gt;Type Coercion, Ruby-style&lt;/h3&gt;
&lt;p&gt;Many dynamically typed languages that offer both integer and floating point arithmetic are smart about doing the right thing based on whether or not any floats are used in a given expression. While I assume Ruby&amp;#8217;s behavior is common knowledge here, the following example demonstrates what I&amp;#8217;ve just described.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; 3/2
=&amp;gt; 1
&amp;gt;&amp;gt; 3/2.0
=&amp;gt; 1.5
&lt;/pre&gt;
&lt;p&gt;This is an obvious candidate for implementation level special casing, but since all the primitive numeric types in Ruby are actually objects, Ruby prefers something a bit more flexible and consistent. What actually happens when an arithmetic operation is performed on a Ruby number is that a method called &lt;tt&gt;coerce()&lt;/tt&gt; is called to do any necessary type modifications so that the computations work as expected. The irb session shown below demonstrates calling &lt;tt&gt;coerce()&lt;/tt&gt; directly on both a &lt;tt&gt;Fixnum&lt;/tt&gt; and a &lt;tt&gt;Float&lt;/tt&gt;.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; 3.coerce(2)
=&amp;gt; [2, 3]
&amp;gt;&amp;gt; 3.coerce(2.0)
=&amp;gt; [2.0, 3.0]
&amp;gt;&amp;gt; 3.0.coerce(3)
=&amp;gt; [3.0, 3.0]
&amp;gt;&amp;gt; 3.0.coerce(2.0)
=&amp;gt; [2.0, 3.0]
&lt;/pre&gt;
&lt;p&gt;Note that &lt;tt&gt;Fixnum#coerce&lt;/tt&gt; only returns an array of Float values when its argument is a Float, but that &lt;tt&gt;Float#coerce&lt;/tt&gt; always does this conversion. While what is shown above only demonstrates how floating point coercion works, we can actually create our own objects that duck type to Ruby numbers by simply defining a &lt;tt&gt;coerce()&lt;/tt&gt; method on them.&lt;/p&gt;
&lt;p&gt;To demonstrate this, I have created a partial implementation of a &lt;tt&gt;BinaryInteger&lt;/tt&gt; object. A &lt;tt&gt;BinaryInteger&lt;/tt&gt; is meant to act similar to Ruby&amp;#8217;s &lt;tt&gt;Fixnum&lt;/tt&gt; objects but display itself to the user in binary notation. Here&amp;#8217;s an example of how such an object might be used:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; int = BinaryInteger.new(40)
=&amp;gt; 0b101000
&amp;gt;&amp;gt; 2 + int
=&amp;gt; 0b101010
&amp;gt;&amp;gt; 2.5 + int
TypeError: BinaryInteger can't be coerced into Float
	from ./binary_integer.rb:49:in `+'
	from (irb):4
	from :0
&lt;/pre&gt;
&lt;p&gt;The following class definition does not quite produce a complete &lt;tt&gt;Numeric&lt;/tt&gt; work-alike but it is sufficient for making the previous example work as shown. It also serves to demonstrate that &lt;tt&gt;coerce()&lt;/tt&gt; is indeed the magic that ties all of Ruby&amp;#8217;s arithmetic operations together.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class BinaryInteger
  def initialize(value)
    @value = value
  end

  attr_accessor :value

  def integer?
    true
  end

  def +(other)
    a,b = coerce(other) # use our own coerce here
    self.class.new(a.value + b.value)
  end

  def coerce(other)
    raise TypeError unless other.integer? 

    if other.respond_to?(:value)
      [self, other] # no coercion needed
    else
      [self, self.class.new(other)]
    end
  end

  def inspect
    "0b#{@value.to_s(2)}"
  end
end
&lt;/pre&gt;
&lt;p&gt;While it can be a bit tricky to puzzle through how &lt;tt&gt;coerce()&lt;/tt&gt; should work, since you can&amp;#8217;t know in advance what the calling object will be, it is certainly a whole lot more dynamic than enforcing class based typing. Getting in the practice of thinking in terms of the interactions between the objects in your project rather than their static definitions can lead to some very good design insights.&lt;/p&gt;
&lt;p&gt;In addition to the &lt;tt&gt;coerce()&lt;/tt&gt; method for arithmetic, Ruby uses a whole score of other coercion hooks, including &lt;tt&gt;to_int&lt;/tt&gt;, &lt;tt&gt;to_str&lt;/tt&gt;, and &lt;tt&gt;to_ary&lt;/tt&gt;. These methods are called on the arguments passed to a number of &lt;tt&gt;Fixnum&lt;/tt&gt;, &lt;tt&gt;String&lt;/tt&gt;, and &lt;tt&gt;Array&lt;/tt&gt; methods. The neat thing is that there is no strict requirement that these methods actually return &lt;tt&gt;Fixnum&lt;/tt&gt;, &lt;tt&gt;String&lt;/tt&gt;, or &lt;tt&gt;Array&lt;/tt&gt; objects, as long as they act close enough to the real thing where it counts (i.e. for whatever messages that get sent to them).&lt;/p&gt;
&lt;p&gt;We could probably spend all day going through other examples of where Ruby uses duck typing for coercion, for extension points, and tons of other uses. This is especially true when you consider that almost every mixin relies on a form of duck typing. For example, all functionality in &lt;tt&gt;Enumerable&lt;/tt&gt; can work with anything that implements a sensible &lt;tt&gt;each()&lt;/tt&gt; method. Similarly a suitable &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt; operator unlocks all that &lt;tt&gt;Comparable&lt;/tt&gt; has to offer. In both the core and standard library, you will find plenty of examples of this sort of design.&lt;/p&gt;
&lt;p&gt;The key point to take away from these observations is that duck-typed APIs aren&amp;#8217;t some obscure edge case for the extensibility-obsessed, but instead, something baked into Ruby&amp;#8217;s philosophy from the ground up. This means that you can and should imitate this style in your own libraries when it makes sense to do so.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ll now take a look at a pair of examples from the wild, one from my own project (Prawn), and another from Aaron Patterson&amp;#8217;s Rails 3.1 performance tuning adventures. Both involve the use of duck typing not for the purpose of infinite flexibility, but for addressing practical problems that come up in most moderately complex projects.&lt;/p&gt;
&lt;h3&gt;Duck typing to avoid scope creep&lt;/h3&gt;
&lt;p&gt;The first example of duck typing in actual Ruby projects that I want to share is actually quite similar to the contrived &lt;tt&gt;read_data()&lt;/tt&gt; example I shared on Tuesday. Today, rather than showing you the usage code first, I want you to take a look at the implementation and try to spot the usage of duck typing and guess at what it gains us before reading on..&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def image(file, options={})
  Prawn.verify_options [:at, :position, :vposition, :height,
                        :width, :scale, :fit], options

  if file.respond_to?(:read)
    image_content = file.read
  else
    raise ArgumentError, "#{file} not found" unless File.file?(file)
    image_content = File.binread(file)
  end

  # additional implementation details omitted.
end

# FULL IMPLEMENTATION OF image() at:
# https://github.com/sandal/prawn/blob/master/lib/prawn/images.rb#L65
&lt;/pre&gt;
&lt;p&gt;If you guessed this code is used to make it so that the &lt;tt&gt;image()&lt;/tt&gt; method can be called with either a file name or a file handle, you had the right idea. It does all of the things we discussed yesterday, allowing the use of this code with &lt;tt&gt;StringIO&lt;/tt&gt;, &lt;tt&gt;Tempfile&lt;/tt&gt;, any mock object that implements a &lt;tt&gt;read()&lt;/tt&gt; method, etc. But the really interesting use case is the one that we actually wrote this feature for, shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "open-uri"

Prawn::Document.generate("remote_images.pdf") do
  image open("http://prawn.majesticseacreature.com/images/prawn.png")
end
&lt;/pre&gt;
&lt;p&gt;Through the use of &lt;tt&gt;open-uri&lt;/tt&gt;, our duck-typed image method provides a pretty nice way of rendering remote content! While this might not have been an easy feature to guess without knowing a bit about Prawn, it does represent the elegant compromise that such an implementation affords us. Adding support for remote images was something that our users often asked for, but we wanted to avoid giving people the impression that Prawn was web-aware, and didn&amp;#8217;t want to support a special case for this sort of logic, as it&amp;#8217;d require either an &lt;span class="caps"&gt;API&lt;/span&gt; change or an ugly hack to try to determine whether the provided string was either a &lt;span class="caps"&gt;URI&lt;/span&gt; or a file name.&lt;/p&gt;
&lt;p&gt;The approach of accepting anything with a &lt;tt&gt;read()&lt;/tt&gt; method combined with Ruby&amp;#8217;s standard library &lt;tt&gt;open-uri&lt;/tt&gt; made for something that is easy to document and easy for our users to remember. While a simple hack, I was very satisfied with how this design turned out because it seemed to mostly eliminate the problem for our users while simultaneously avoiding some overly complex implementation code that might be brittle and hard to test.&lt;/p&gt;
&lt;p&gt;These sort of tough design decisions are certainly not unique to Prawn, so we can now turn our eyes to Aaron Patterson&amp;#8217;s performance optimization work on Rails 3.1.&lt;/p&gt;
&lt;h3&gt;Duck typing for performance tuning&lt;/h3&gt;
&lt;p&gt;One area Aaron Patterson found was a hotspot for many Rails apps are &lt;tt&gt;ActiveRecord&lt;/tt&gt; scopes, which allow the users to create custom filters. For example, consider the following example which filters by email address.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Comment &amp;lt; ActiveRecord::Base
  scope :with_email, lambda { |email|
    where(:email =&amp;gt; email)
  }
end

# Above code provides functionality shown below
User.with_email("gregory.t.brown@gmail.com").count #=&amp;gt; 1
&lt;/pre&gt;
&lt;p&gt;The block syntax is nice and clean for simple things, but can get a bit unwieldy for complex logic. For example, if we wanted to throw in validations for the entered email addresses, our block would end up getting a bit ugly unless we implemented some private class methods to help out. If you&amp;#8217;re thinking that private class methods sound weird and might be a bit of a code smell, they are, and that&amp;#8217;s one indication that this &lt;span class="caps"&gt;API&lt;/span&gt; needs to be a bit more flexible than what it is.&lt;/p&gt;
&lt;p&gt;That said, Aaron was on a performance tuning mission, not an &lt;span class="caps"&gt;API&lt;/span&gt; overhaul. The problem he found with the &lt;span class="caps"&gt;API&lt;/span&gt; was initially not an aesthetic one but an implementation detail: Executing code stored in a &lt;tt&gt;Proc&lt;/tt&gt; object is considerably more computationally expensive than an ordinary method call. While this isn&amp;#8217;t likely to be a bottleneck in ordinary situations, it is common for high traffic Rails applications to really hammer on their scopes, since they&amp;#8217;re used for filtering the data that is presented to users. The key insight Aaron had was that making some other object quack like a &lt;tt&gt;Proc&lt;/tt&gt; is no more complicated than implementing a &lt;tt&gt;call()&lt;/tt&gt; method.&lt;/p&gt;
&lt;p&gt;Shown below is the one line patch that changes the behavior of &lt;tt&gt;scope()&lt;/tt&gt; to no longer explicitly expect a &lt;tt&gt;Proc&lt;/tt&gt; object and allows the use of anything that implements a meaningful &lt;tt&gt;call()&lt;/tt&gt; method.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
# BEFORE
options = filter.is_a?(Proc) ? filter.call(*args) : filter

# AFTER
options = filter.respond_to?(:call) ? filter.call(*args) : filter
&lt;/pre&gt;
&lt;p&gt;With this nearly microscopic change, we can write a faster &lt;tt&gt;with_email()&lt;/tt&gt; scope that also leaves room for complex logic such as validations in its own neatly defined namespace. The following definition is functionally equivalent to our original code that passes a &lt;tt&gt;Proc&lt;/tt&gt; to &lt;tt&gt;scope()&lt;/tt&gt;, but has a lot more potential for future growth.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class EmailFilter 
  def initialize(model_class)
    @model_class = model_class
  end

  def call(email)
    validate_address(email)
    @model_class.where(:email =&amp;gt; email)
  end

  private

  def validate_address(email)
    # do some validation magic here
  end
end

class User &amp;lt; ActiveRecord::Base
  scope :with_email, EmailFilter.new(self)
end
&lt;/pre&gt;
&lt;p&gt;The nice thing about this patch is that nothing is lost by doing things this way. Often times, when moving from explicit class checking to behavior based checks, the only overhead is that debugging can be a bit more complicated since there is no easy way to verify that an object implementing &lt;tt&gt;call()&lt;/tt&gt; actually does so in a sensible way. However, with adequate unit tests and decent documentation, this kind of fuzziness is rarely a big enough problem in practical applications to outweigh the benefits that come along with utilizing this technique.&lt;/p&gt;
&lt;p&gt;Aside from the superficial improvements that come from converting &lt;tt&gt;Proc&lt;/tt&gt; calls to method calls, the general approach behind writing duck typed interfaces tends to increase the potential for further performance improvements. When code is written to explicitly avoid assuming too much about how objects are implemented, it is easy to swap out objects that are more performant in edge cases, or implement aggressive caching where appropriate. While it may seem counterintuitive, the same dynamic nature that makes Ruby slow at the implementation level makes a wide range of algorithmic improvements possible. We unfortunately won&amp;#8217;t be exploring this topic today, but it would be a good topic for a future issue.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 19 Jul 2011 13:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/047-issue-15-duck-typing-2.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/047-issue-15-duck-typing-2.html</guid></item><item><title>Issue #14: Duck Typing in Practice (1 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 21, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Type systems are a fundamental part of every programming language. In fact, the way a language designer approaches typing goes a long way towards outlining the way that thoughts are expressed in that language.&lt;/p&gt;
&lt;p&gt;Statically typed languages like C++ and Java make us tend to think of objects as abstract data structures that fit within a neatly defined hierarchy. In these languages, there isn&amp;#8217;t a major distinction between an object&amp;#8217;s class and its type, as the two concepts are tied together directly at the implementation level. But the marriage of class and type found in these languages is not a universal law shared by all object oriented programming languages.&lt;/p&gt;
&lt;p&gt;By contrast, Ruby&amp;#8217;s dynamic nature facilitates a style of type system known as duck typing. In particular, duck typing breaks the strong association between an object&amp;#8217;s class and its type by defining types based on what an object can do rather than what class it was born from. This subtle shift in semantics changes virtually everything about how you need to think about designing object oriented systems, making it a great topic for Practicing Ruby to cover.&lt;/p&gt;
&lt;p&gt;While duck typing is possible in many other languages, Ruby is designed from the ground up to support this style of objected oriented design. In this issue, we will cover some of the options that are available to us for doing Ruby-style type checking.&lt;/p&gt;
&lt;h3&gt;Type Checking Techniques&lt;/h3&gt;
&lt;p&gt;There are basically three ways to do type checking in Ruby, two of which are a form of duck typing, and one that is not. Here&amp;#8217;s an example of the approach that does &lt;strong&gt;not&lt;/strong&gt; involve duck typing.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def read_data(source)
  case file
  when String
    File.read(source)
  when IO
    source.read
  end
end
&lt;/pre&gt;
&lt;p&gt;Odds are if you&amp;#8217;ve been coding Ruby for a while, you&amp;#8217;ve either read or written some code that did type checking in this fashion. Ruby&amp;#8217;s case statement is very powerful, and makes this sort of logic easy to write. Our &lt;tt&gt;read_data()&lt;/tt&gt; function will work just fine under the common scenarios shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
filename = "foo.txt"
read_data(filename) #=&amp;gt; reads the contents of foo.txt by calling 
                    #   File.read()


input = File.open("foo.txt")
read_data(input) #=&amp;gt; reads the contents of foo.txt via 
                 #   the passed in file handle
&lt;/pre&gt;

&lt;p&gt;But things begin to fall apart a bit when we decide we&amp;#8217;d like &lt;tt&gt;read_data()&lt;/tt&gt; to work with a &lt;tt&gt;Tempfile&lt;/tt&gt;, or with a &lt;tt&gt;StringIO&lt;/tt&gt; object, or perhaps with a mock object we&amp;#8217;ve defined in our tests. We have managed to bake into our logic the assumption that the input is always either a descendent of &lt;tt&gt;String&lt;/tt&gt; or a descendent of &lt;tt&gt;IO&lt;/tt&gt;. The purpose of duck typing is to remove these restrictions by focusing only on the messages that are being passed back and forth between objects rather than what class they belong to. The code below demonstraties one way you can do that.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def read_data(source)
  return source.read if source.respond_to?(:read)
  return File.read(source.to_str) if source.respond_to?(:to_str)
  raise ArgumentError
end
&lt;/pre&gt;
&lt;p&gt;With this modification, our method expects far less of its input. The passed in object simply needs to implement either a meaningful &lt;tt&gt;read()&lt;/tt&gt; or &lt;tt&gt;to_str()&lt;/tt&gt; method. In addition to being backwards compatible with our non-duck-typed code, this new approach gives us access to the full range of things we were missing: &lt;tt&gt;StringIO&lt;/tt&gt;, &lt;tt&gt;Tempfile&lt;/tt&gt;, mock objects for testing, and any user defined objects that are either IO-like or String-like but not a descendent of either.&lt;/p&gt;
&lt;p&gt;However, the following contrived example illustrates a final corner case that calls for a bit of extreme duck typing to resolve. Try to spot the problem before reading about how to solve it.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class FileProxy
  def initialize(tempfile)
    @tempfile = tempfile
  end

  def method_missing(id, *args, &amp;amp;block)
    @tempfile.send(id, *args, &amp;amp;block)
  end
end
&lt;/pre&gt;
&lt;p&gt;This code implements a proxy which forwards all of its messages to the wrapped &lt;tt&gt;tempfile&lt;/tt&gt; object. However, like many hastily coded proxy objects in Ruby, it does not properly forward &lt;tt&gt;respond_to?()&lt;/tt&gt; calls to the object it wraps. The irb session below illustrates the resulting false negative in our test.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
# Populate our tempfile through the proxy

&amp;gt;&amp;gt; proxy = FileProxy.new(Tempfile.new("foo.txt"))
=&amp;gt; #&amp;lt;FileProxy:0x39461c @tempfile=#&amp;lt;File:/var/f..foo.txt.7910.3&amp;gt;&amp;gt;
&amp;gt;&amp;gt; proxy &amp;lt;&amp;lt; "foo bar baz"
=&amp;gt; #&amp;lt;File:/var/folders/sJ/sJo0IkPYFWCY3t5uH+gi0++++TQ/-Tmp-/foo.txt.7910.3&amp;gt;
&amp;gt;&amp;gt; proxy.rewind
=&amp;gt; 0

# Unsuccessfully test for presence of read() method

&amp;gt;&amp;gt; proxy.respond_to?(:read)
=&amp;gt; false

# But read() works as expected!

&amp;gt;&amp;gt; proxy.read
=&amp;gt; "foo bar baz"
&lt;/pre&gt;
&lt;p&gt;This issue will cause &lt;tt&gt;read_data()&lt;/tt&gt; to raise an &lt;tt&gt;ArgumentError&lt;/tt&gt; when passed a &lt;tt&gt;FileProxy&lt;/tt&gt;. In this case, the best solution is to fix &lt;tt&gt;respond_to?()&lt;/tt&gt; so that it works as expected, but since you may often encounter libraries with bad behaviors like this, it&amp;#8217;s worth knowing what the duck typing fundamentalist would do in this situation.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def read_data(source)
  begin 
    return source.read 
  rescue NoMethodError
    # do nothing, just catch the specific error you'd expect if
    # read() was not present.
  end

  begin
    File.read(source.to_str)
  rescue NoMethodError
    raise ArgumentError # now we've run out of valid cases, so let's
                        # raise a meaningful error
   end
end
&lt;/pre&gt;
&lt;p&gt;With this final version, we preserve all the benefits of the previous duck typing example, but we can work with objects that have dishonest &lt;tt&gt;respond_to?()&lt;/tt&gt; methods. Unfortunately, the cost for such flexibility includes code that is a bit less pleasant to read and is almost certainly going to run slower than either of our previous implementations. Using the exception system for control flow doesn&amp;#8217;t come cheap, even if theoretically this is the most &amp;#8216;pure&amp;#8217; form of type checking we can do.&lt;/p&gt;
&lt;p&gt;While we&amp;#8217;ve talked about the benefits and drawbacks of each of these approaches, I haven&amp;#8217;t given any direct advice on whether one way of doing type checking is better than the others, simply because there is no simple answer to that question.&lt;/p&gt;
&lt;p&gt;I will try to paint a clearer picture in the next article by showing several realistic examples of why duck typing can come in handy. Until then, I&amp;#8217;d like to leave you with a few things to think about.&lt;/p&gt;
&lt;h3&gt;Questions / Study Topics&lt;/h3&gt;
&lt;ul&gt;
	&lt;li&gt;Is explicit class checking ever absolutely necessary? Are their situations in which even if other options are available, checking the class of an object is still the best thing to do?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Name something weird that can happen when you write your contracts on the messages your objects respond to rather than what class of object they are.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Try to identify some feature of Ruby that relies on duck typing either for its basic functionality or as an extension point meant to be customized by application programmers.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Share a bit of code which does explicit class comparison that you think would be very difficult to convert to a duck-typing style.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Share a bit of code (either your own or from a &lt;span class="caps"&gt;OSS&lt;/span&gt; project you like) that you feel uses duck typing effectively.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Feel free to leave a response in the comments section below if any of the above topics interest you.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Sun, 17 Jul 2011 13:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/046-issue-14-duck-typing.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/046-issue-14-duck-typing.html</guid></item><item><title>Issue #13: Obfuscations of Christmas Past</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 23, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Rather than always focusing on &lt;span class="caps"&gt;SERIOUS&lt;/span&gt; &lt;span class="caps"&gt;BUSINESS&lt;/span&gt;, I&amp;#8217;d like share something a little more light hearted today. Whether you celebrate Christmas or not, I think you&amp;#8217;ll find this little holiday themed hack a great deal of fun to play with.&lt;/p&gt;
&lt;h3&gt;Christian Neukirchen&amp;#8217;s Christmas Hack&lt;/h3&gt;
&lt;p&gt;When I first started programming in Ruby, the ruby-talk mailing list was the best place to interact with the community and keep up with other active Ruby hackers. But because there were a lot more hobbyists in 2004 than there were people doing Ruby as a full time job, the posts focused on sharing fun hacks just as often as they did on discussing practical issues.&lt;/p&gt;
&lt;p&gt;One of my favorites was &lt;a href="http://twitter.com/#!/chneukirchen"&gt;Christian Neukirchen&lt;/a&gt;&amp;#8216;s obfuscated Christmas message to the Ruby community in 2004. I&amp;#8217;ve copied the source code below, and I encourage you to run it and see that it is indeed a valid Ruby program!&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
s="IyBUaGFua3MgZm9yIGxvb2tpbmcgYXQgbXkgY29kZ
S4KIwojIENvcHlyaWdodCAoQykgMjAwMiAgQ2hyaXN0a
WFuI      E       5       l       d     Wtpc                                
mNoZ  W       4      gP       G       N obmV
1a2l      y       Y 2hlb  k       B     nbWF
pbC5  j       b    20+CiM     K       I yBUa
GlzI      H       Byb2dyYW        0     gaXM
gZnJ  l       Z  SBzb2Z0d2F   y       Z Tsge
W91I      G     NhbiByZWRpc3      R     yaWJ
1dGU  g        aXQgYW5kL29yCi M       g bW9k
aWZ5      I   Gl0IHVuZGVyIHRoZ    S     B0ZX
Jtcy  B      vZiB0aGUgR05VIEdlb       m VyYW
wgUH      V      ibGljIExpY       2     Vuc2
UuCg  p       T VERPVVQuc3lu  Y       y A9IH
RydW      U    KZDEsIGQyID0gM     C     4xNS
wgMC  4       wNgpzID0gIk1lcnJ        5 IGNo
cmlz      d  G1hcywgLi4uIGFuZCB   h     IGhh
cHB5  I     G5ldyB5ZWFyIgptID0gJ      X d7LC
AuID       ogISArICogMCBPIEB9CnUg P     SAiI
CIgK  i   BzLnNpemUKCnByaW50ICJcci    A gI3t
1fVx      y   IjsKCigwLi4ocy5z    a     XplL
TEpK  S      50b19hLnNvcnRfYnkg       e yByY
W5kI      H 0uZWFjaCB7IHxyfAogIH  N     sZWV
wIGQ  x    CiAgbmV4dCBpZiBzW3JdID     0 9ICI
gIls      wXQogIG0uZWFjaCB7IHxrfAo      gICA
gdVt  y  XSA9IGsKICAgIHByaW50ICIgIC   N 7dX1
cciI    KICAgIHNsZWVwIGQyCiAgfQogIHV    bcl0
gPSB   zW3JdCiAgcHJpbnQgIiAgI3t1fVxyI g p9Cg
pzbG  VlcCBkMgpwcmludCAiICAje3V9IVxyI   jsKc
2xlZ  X       A    gMwpwc     m       l udCA
iICA      j        e3V9IS A       g     LS1j
aHJp  c       z    JcbiI7     C       g ojIG
ZpbG      x        lciBzc G       F     jZSA
jIyM  j       I    yMjIyM     j       I yMjI
yMjI      y       M       j       I     yMjI
yMK";eval s.delete!(" \n").unpack("m*")[0]##
### Copyright (C) 2004  Christian Neukirchen
&lt;/pre&gt;
&lt;p&gt;When run, this code prints out &lt;i&gt;&amp;#8220;Merry christmas, &amp;#8230; and a happy new year! &amp;#8212;chris2&amp;#8221;&lt;/i&gt; by randomly filling in each character in a little command line animation. After some folks commented on how cool this hack was, someone inevitably asked how it was done, which lead another Ruby hacker Michael Neumann to post his guess to the list. Here is what he said:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Pretty easy (except drawing the tree :). Write the source-code first, then &lt;tt&gt;base64&lt;/tt&gt; encode it, and insert newlines/whitespace to make the picture.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;At the time, I was way too much of a beginner with Ruby to fully appreciate the solution discussion, and mostly just chalked it up to magic. But now, the above statement is immediately obvious to me, and since it wasn&amp;#8217;t really further explained in the mailing list thread, I can give an example for those who are in the same shoes now that I was in a few years ago.&lt;/p&gt;
&lt;p&gt;What I didn&amp;#8217;t know at the time is that &lt;tt&gt;Base64&lt;/tt&gt; is an encoding that allows you to translate any binary data into purely printable characters by converting the contents into a string of characters that uses basic alphanumeric values. I would have known that if I read the documentation for Ruby&amp;#8217;s &lt;tt&gt;Base64&lt;/tt&gt; standard library, but again, I was a newbie at the time. :)&lt;/p&gt;
&lt;p&gt;It turns out that the idea for &lt;tt&gt;Base64&lt;/tt&gt; encoding was extracted from how &lt;span class="caps"&gt;MIME&lt;/span&gt; attachments in email are implemented. This is all stuff you can find on wikipedia, so rather than digging into the gory details, let&amp;#8217;s see how it relates to the problem at hand.&lt;/p&gt;
&lt;p&gt;The following small snippet should clear things up a bit.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; source = "puts 'hello world'"
=&amp;gt; "puts 'hello world'"
&amp;gt;&amp;gt; encoded_source = Base64.encode64(source)
=&amp;gt; "cHV0cyAnaGVsbG8gd29ybGQn\n"
&amp;gt;&amp;gt; Base64.decode64(encoded_source)
=&amp;gt; "puts 'hello world'"
&amp;gt;&amp;gt; eval Base64.decode64(encoded_source)
hello world
=&amp;gt; nil
&lt;/pre&gt;
&lt;p&gt;Another way of decoding &lt;tt&gt;Base64&lt;/tt&gt; encoded strings is via the &lt;tt&gt;String#unpack&lt;/tt&gt; method, using the template &lt;tt&gt;&amp;#8220;m*&amp;#8221;&lt;/tt&gt;. You can see this in Christian&amp;#8217;s code, which is probably what tipped Michael off in the first place. With that in mind, we can build a tiny obfuscated &amp;#8220;Hello World&amp;#8221; program.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
s = 
"c  H   V0cyA 
 n  a     G
 VsbG     8
 g  d     2
 9  y    bGQn"

eval s.delete(" \n").unpack("m*")[0]
&lt;/pre&gt;
&lt;p&gt;In the end, Michael was right when he said this was pretty easy to do. As long as you understand a couple basic concepts about string manipulation and how to decode a &lt;tt&gt;base64&lt;/tt&gt; encoded string, you could use this technique to render your code as pretty much any arbitrary &lt;span class="caps"&gt;ASCII&lt;/span&gt; art.&lt;/p&gt;
&lt;p&gt;Of course, one would expect that the guy who eventually would go on to create something as clever and useful as the &lt;a href="https://github.com/rack/rack"&gt;Rack web server interface&lt;/a&gt; would have an extra trick or two up his sleeve. Not to disappoint, Christian confirmed Michael&amp;#8217;s explanation was valid, but in the process revealed that he felt it&amp;#8217;d be too fragile and tedious to manually format the code himself into the desired ascii art.&lt;/p&gt;
&lt;p&gt;For those curious about how he got around this problem, you can check out his &lt;a href="http://groups.google.com/group/comp.lang.ruby/msg/aa5b4f8eaa85e6b8?dmode=source"&gt;full solution&lt;/a&gt;&lt;br /&gt;
 which implements a simple code generator that fills in a template with the &lt;tt&gt;base64&lt;/tt&gt; encoded source.&lt;/p&gt;
&lt;p&gt;While the code should be pretty easy to follow with a little effort, feel free to post questions here if you need help figuring things out. It&amp;#8217;s a really neat bit of code and is worth exploring, so I don&amp;#8217;t mind giving some hints where needed.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;Writing this article reminded me of two important lessons that I sometimes forget, even to this day.&lt;/p&gt;
&lt;p&gt;The first lesson is that you can&amp;#8217;t judge the complexity of something by simply scratching its surface. When I saw this code posted to ruby-talk back in 2004, even though I was a newbie at the time, I could have figured it out if I only took a bit of time to study the topics that were being discussed. But since I saw a bunch of obscure binary data in the shape of a Christmas tree being passed to &lt;tt&gt;eval()&lt;/tt&gt;, I judged the snippet as being too complicated for me, appreciated it for its magic, and moved on. That sort of lack of self-confidence can really prevent you from stumbling upon interesting new ideas, tools, and techniques.&lt;/p&gt;
&lt;p&gt;The second lesson is that hacking doesn&amp;#8217;t always have to be &lt;span class="caps"&gt;SERIOUS&lt;/span&gt; &lt;span class="caps"&gt;BUSINESS&lt;/span&gt;. Because I&amp;#8217;m working on things I feel are super important most of the time, it&amp;#8217;s easy for me to forget to be playful and generally curious. Sometimes I feel like I&amp;#8217;m too busy to do something just for the joy of the hack. and that worries me a bit. Writing this article reminded that I should resist this temptation, and make more time and space in my life for playful discovery, because it is a great way to learn and have fun at the same time.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 11 May 2011 16:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/045-issue-14-obfuscations.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/045-issue-14-obfuscations.html</guid></item><item><title>Issue #12: Rapid Prototyping</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 21, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Ruby makes it easy to quickly put together a proof-of-concept for almost any kind of project, as long as you have some experience in rapid application development. In this article, I will go over how I build prototypes, sharing the tricks that have worked well for me.&lt;/p&gt;
&lt;p&gt;Today we&amp;#8217;ll be walking through a bit of code that implements a small chunk of a falling blocks game that is similar to Tetris. If you&amp;#8217;re not familiar with Tetris, head over to &lt;a href="http://freetetris.org"&gt;freetetris.org&lt;/a&gt; and play it a bit before reading this article.&lt;/p&gt;
&lt;p&gt;Assuming you&amp;#8217;re now familiar with the general idea behind the game, I&amp;#8217;ll walk you through the thought process that I went through from the initial idea of working on a falling blocks game to the small bit of code I have written for this issue.&lt;/p&gt;
&lt;h3&gt;The Planning Phase&lt;/h3&gt;
&lt;p&gt;After running through a few ideas, I settled on a falling blocks game as a good example of a problem that&amp;#8217;s too big to be tackled in a single sitting, but easy enough to make some quick progress on.&lt;/p&gt;
&lt;p&gt;The next step for me was to come up with a target set of requirements for my prototype. To prevent the possibilities from seeming endless, I try to set a time limit up front to make this decision making process easier. I find that anywhere from an hour to half a day can get you far in Ruby, so I settled on trying to come up with something I felt I could build within an hour or two.&lt;/p&gt;
&lt;p&gt;I knew right away this meant that I wasn&amp;#8217;t going to make an interactive demo. Synchronizing user input and screen output is something that may be easy for folks who do it regularly, but my concurrency knowledge is very limited, and I&amp;#8217;d risk spending several hours on that side of things and coming up empty if I went down that path. Fortunately, even without an event loop, there are still a lot of options for building a convincing demo.&lt;/p&gt;
&lt;p&gt;In my initial optimism, I thought what I&amp;#8217;d like to be able to do is place a piece on the screen, and then let gravity take over, correctly eliminating any completed lines as it fell into place. But this would require me to implement collision detection, something I didn&amp;#8217;t want to tackle right away.&lt;/p&gt;
&lt;p&gt;Eventually, I came up with the idea of just implementing the action that happens when a piece collides with the junk on the grid. This process involved turning the active piece into inactive junk, and then removing any completed rows from the grid. This is something that I felt fit within the range of what I could do within an hour or two, so I decided to sleep on it and see if any unknowns bubbled up to the surface.&lt;/p&gt;
&lt;p&gt;I could have just started hacking right away, but ironically that&amp;#8217;s a practice I typically avoid when putting together rapid prototypes. If this were a commercial project and I quoted the customer 2-4 hours, I&amp;#8217;d want to use their money in the best possible way, and picking the wrong scope for my project would be a surefire way to either blow the budget or fail to produce something interesting. I find a few hours of passive noodling helps me see unexpected issues before they bite me.&lt;/p&gt;
&lt;p&gt;Fortunately, this idea managed to pass the test of time, and I set out to begin coding by turning the idea into a set of requirements.&lt;/p&gt;
&lt;h3&gt;The Requirements Phase&lt;/h3&gt;
&lt;p&gt;A good prototype does not come from a top-down or bottom-up design, but instead comes from starting in the middle and building outwards. By taking a small vertical slice of the problem at hand, you are forced to think about many aspects of the system, but not in a way that requires you consider the whole problem all at once. This allows most of your knowledge and often a good chunk of your code to be re-used when you approach the full project.&lt;/p&gt;
&lt;p&gt;The key is to start with a behavior the user can actually observe. This means that you should be thinking in terms of features rather than functions and objects. Some folks use story frameworks such as Cucumber to help them formalize this sort of inside-out thinking, but personally, I prefer just to come up with a good, clear example and not worry about shoehorning it into a formal setting.&lt;/p&gt;
&lt;p&gt;To do this, I created a simple text file filled with ascii art that codified two cases: One in which a line was cleared, and where no lines were cleared. Both cases are shown below.&lt;/p&gt;
&lt;h3&gt;&lt;span class="caps"&gt;CASE&lt;/span&gt; 1: &lt;span class="caps"&gt;REMOVING&lt;/span&gt; &lt;span class="caps"&gt;COMPLETED&lt;/span&gt; &lt;span class="caps"&gt;LINES&lt;/span&gt;&lt;/h3&gt;
&lt;pre&gt;
==========
           
           
           
           
           
           
   #       
   #|    | 
  |#||  ||
|||#||||||
==========
&lt;/pre&gt;
&lt;p&gt;&lt;span class="caps"&gt;BECOMES&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;
==========
           
           
           
           
           


   |       
   ||    | 
  ||||  ||
==========
&lt;/pre&gt;
&lt;h3&gt;&lt;span class="caps"&gt;CASE&lt;/span&gt; 2: &lt;span class="caps"&gt;COLLISION&lt;/span&gt; &lt;span class="caps"&gt;WITHOUT&lt;/span&gt; &lt;span class="caps"&gt;ANY&lt;/span&gt; &lt;span class="caps"&gt;COMPLETED&lt;/span&gt; &lt;span class="caps"&gt;LINES&lt;/span&gt;&lt;/h3&gt;
&lt;pre&gt;
==========
           
           
           
           
           
          
  #       
  ##|    |
  |#||  ||
||| ||||||
==========
&lt;/pre&gt;
&lt;p&gt;&lt;span class="caps"&gt;BECOMES&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;
==========
           
           
           
           
           
          
  |       
  |||    | 
  ||||  ||
||| ||||||
==========
&lt;/pre&gt;
&lt;hr /&gt;
&lt;p&gt;With the goals for the prototype clearly outlined, I set out to write a simple program that would perform the necessary transformations.&lt;/p&gt;
&lt;h3&gt;The Coding Phase&lt;/h3&gt;
&lt;p&gt;One thing I&amp;#8217;ll openly admit is that when prototyping something that will take me less than a half day from end to end, I tend to relax my standards on both testing and writing clean code. The reason for this is that when I&amp;#8217;m trying to take a nose-dive into a new problem domain, I find my best practices actually get in the way until I have at least a basic understanding of the project.&lt;/p&gt;
&lt;p&gt;What I&amp;#8217;ll typically do instead is write a single file that implements both the objects I need and an example that gets me closer to my goal. For this project, I started with a canvas object for rendering output similar to what I outlined in my requirements.&lt;/p&gt;
&lt;p&gt;Imagining this canvas object already existed, I wrote some code for generating the very first bit out output we see in the requirements.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
canvas = FallingBlocks::Canvas.new

(0..2).map do |x|
  canvas.paint([x,0], "|")
end

canvas.paint([2,1], "|")

(0..3).map do |y|
  canvas.paint([3,y], "#")
end

(4..9).map do |x|
  canvas.paint([x,0], "|")
end

[4,5,8,9].map do |x|
  canvas.paint([x,1], "|")
end

canvas.paint([4,2], "|")
canvas.paint([9,2], "|")

puts canvas 
&lt;/pre&gt;
&lt;p&gt;While I use a few loops for convenience, it&amp;#8217;s easy to see that this code does little more than put symbols on a plain text grid at the specified (x,y) coordinates. Once &lt;tt&gt;FallingBlocks::Canvas&lt;/tt&gt; is implemented, we&amp;#8217;d expect the following output from this example:&lt;/p&gt;
&lt;pre&gt;
==========
           
           
           
           
           
           
   #       
   #|    | 
  |#||  ||
|||#||||||
==========
&lt;/pre&gt;
&lt;p&gt;What we have done is narrowed the problem down to a much simpler task, making it easier to get started. The following implementation is sufficient to get the example working, and is simple enough that we probably don&amp;#8217;t need to discuss it further.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module FallingBlocks
  class Canvas
    SIZE = 10

    def initialize
      @data = SIZE.times.map { Array.new(SIZE) }
    end

    def paint(point, marker)
      x,y = point
      @data[SIZE-y-1][x] = marker
    end

    def to_s
      [separator, body, separator].join("\n")
    end

    def separator
      "="*SIZE
    end

    def body
      @data.map do |row|
        row.map { |e| e || " " }.join
      end.join("\n")
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;However, things get a little more hairy once we&amp;#8217;ve plucked this low hanging fruit. So far, we&amp;#8217;ve built a tool for painting the picture of what&amp;#8217;s going on, but that doesn&amp;#8217;t tell us anything about the underlying structure. This is a good time to start thinking about what Tetris pieces actually are.&lt;/p&gt;
&lt;p&gt;While a full implementation of the game would require implementing rotations and movement, our prototype looks at pieces frozen in time. This means that a piece is really just represented by a collection of points. If we define each piece based on an origin of [0,0], we end up with something like this for a vertical line:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
line = FallingBlocks::Piece.new([[0,0],[0,1],[0,2],[0,3]])
&lt;/pre&gt;
&lt;p&gt;Similarly, a bent S-shaped piece would be defined like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
bent = FallingBlocks::Piece.new([[0,1],[0,2],[1,0],[1,1]])
&lt;/pre&gt;
&lt;p&gt;In order to position these pieces on a grid, what we&amp;#8217;d need as an anchor point that could be used to translate the positions occupied by the pieces into another coordinate space.&lt;/p&gt;
&lt;p&gt;We could use the origin at [0,0], but for aesthetic reason, I didn&amp;#8217;t like the mental model of grasping a piece by a position that could potentially be unoccupied. Instead, I decided to define the anchor as the top-left position occupied by the piece, which could later be translated to a different position on the canvas. This gives us an anchor of [0,3] for the line, and an anchor of [0,2] for the bent shape. I wrote the following example to outline how the &lt;span class="caps"&gt;API&lt;/span&gt; should work.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt; 
line = FallingBlocks::Piece.new([[0,0],[0,1],[0,2],[0,3]])
p line.anchor #=&amp;gt; [0,3]

bent = FallingBlocks::Piece.new([[0,1],[0,2],[1,0],[1,1]])
p bent.anchor #=&amp;gt; [0,2]
&lt;/pre&gt;
&lt;p&gt;Once again, a simple example gives me enough constraints to make it easy to write an object that implements the desired behavior.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Piece
  def initialize(points)
    @points = points
    establish_anchor
  end

  attr_reader :points, :anchor

  # Gets the top-left most point
  def establish_anchor
    @anchor = @points.max_by { |x,y| [y,-x] }
  end
end
&lt;/pre&gt;
&lt;p&gt;As I was writing this code, I stopped for a moment and considered that this logic, as well as the logic written earlier that manipulates (x,y) coordinates to fit inside a row-major data structure are the sort of things I really like to write unit tests for. There is nothing particularly tricky about this code, but the lack of tests makes it harder to see what&amp;#8217;s going on at a glance. Still, this sort of tension is normal when prototyping, and at this point I wasn&amp;#8217;t even 30 minutes into working on the problem, so I let the feeling pass.&lt;/p&gt;
&lt;p&gt;The next step was to paint these pieces onto the canvas, and I decide to start with their absolute coordinates to sanity check my shape definitions. The following example outlines the behavior I&amp;#8217;d expect.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
canvas = FallingBlocks::Canvas.new

bent_shape = FallingBlocks::Piece.new([[0,1],[0,2],[1,0],[1,1]])
bent_shape.paint(canvas)

puts canvas
&lt;/pre&gt;
&lt;p&gt;&lt;span class="caps"&gt;OUTPUTS&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;
==========
          
          
          
          
          
          
          
#         
##        
 #        
==========
&lt;/pre&gt;
&lt;p&gt;Getting this far was easy, the following definition of &lt;tt&gt;Piece&lt;/tt&gt; does the trick:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Piece
   SYMBOL = "#"

  def initialize(points)
    @points = points
    establish_anchor
  end

  attr_reader :points, :anchor

  # Gets the top-left most point
  def establish_anchor
    @anchor = @points.min_by { |x,y| [y,-x] }
  end

  def paint(canvas)
    points.each do |point|
      canvas.paint(point, SYMBOL)
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;This demonstrates to me that the concept of considering pieces as a collection of points can work, and that my basic coordinates for a bent piece are right. But since I need a way to translate these coordinates to arbitrary positions of the grid for this code to be useful, this iteration was only a stepping stone. A new example pushes us forward.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
canvas = FallingBlocks::Canvas.new

bent_shape = FallingBlocks::Piece.new([[0,1],[0,2],[1,0],[1,1]])

canvas.paint_shape(bent_shape, [2,3])

puts canvas
&lt;/pre&gt;
&lt;p&gt;&lt;span class="caps"&gt;OUTPUTS&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;
==========
          
          
          
          
          
          
  #       
  ##      
   #      
          
==========
&lt;/pre&gt;
&lt;p&gt;As you can see in the code above, I decided that my &lt;tt&gt;Piece#paint&lt;/tt&gt; method was probably better off as &lt;tt&gt;Canvas#paint_shape&lt;/tt&gt;, just to collect the presentation logic in one place. Here&amp;#8217;s what the updated code ended up looking like.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Canvas
 # ...

 def paint_shape(shape, position)
   shape.translated_points(position).each do |point|
     paint(point, Piece::SYMBOL)
   end
 end
end
&lt;/pre&gt;
&lt;p&gt;This new code does not rely directly on the &lt;tt&gt;Piece#points&lt;/tt&gt; method anymore, but instead, passes a position to the newly created &lt;tt&gt;Piece#translated_points&lt;/tt&gt; to get a set of coordinates anchored by the specified position.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Piece
  #...
  
  def translated_points(new_anchor)
    new_x, new_y = new_anchor
    old_x, old_y = anchor

    dx = new_x - old_x
    dy = new_y - old_y
    
    points.map { |x,y| [x+dx, y+dy] }
  end
end
&lt;/pre&gt;
&lt;p&gt;While this mapping isn&amp;#8217;t terribly complex, it&amp;#8217;s yet another point where I was thinking &amp;#8216;gee, I should really be writing tests&amp;#8217;, and a couple subtle bugs that cropped up while I was writing this confirmed my gut feeling. But with the light visible at the end of the tunnel, I decided to press on by writing an example that&amp;#8217;d unify piece objects with the junk left on the grid from previous moves.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
game = FallingBlocks::Game.new
bent_shape = FallingBlocks::Piece.new([[0,1],[0,2],[1,0],[1,1]])
game.piece = bent_shape
game.piece_position = [2,3]
game.junk += [[0,0], [1,0], [2,0], [2,1], [4,0],
              [4,1], [4,2], [5,0], [5,1], [6,0],
              [7,0], [8,0], [8,1], [9,0], [9,1],
              [9,2]]

puts game
&lt;/pre&gt;
&lt;p&gt;&lt;span class="caps"&gt;OUTPUTS&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;
==========






  #
  ##|    |
  |#||  ||
||| ||||||
==========
&lt;/pre&gt;
&lt;p&gt;The key component that tied this all together is the &lt;tt&gt;Game&lt;/tt&gt; object, which essentially is just a container that knows how to use a &lt;tt&gt;Canvas&lt;/tt&gt; object to render itself.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Game
  def initialize
    @junk = []
    @piece = nil
    @piece_position = []
  end

  attr_accessor :junk, :piece, :piece_position

  def to_s
    canvas = Canvas.new

    junk.each do |pos|
      canvas.paint(pos, "|")
    end

    canvas.paint_shape(piece, piece_position, "#")

    canvas.to_s
  end
end
&lt;/pre&gt;
&lt;p&gt;I made a small change to &lt;tt&gt;Canvas#paint_shape&lt;/tt&gt; so that the symbol used to display pieces on the grid was parameterized rather than stored in &lt;tt&gt;Piece::&lt;span class="caps"&gt;SYMBOL&lt;/span&gt;&lt;/tt&gt;. This isn&amp;#8217;t a major change and was just another attempt at moving display code away from the data models.&lt;/p&gt;
&lt;p&gt;After all this work, we&amp;#8217;ve made it back to the output we were getting out of our first example, but without the smoke and mirrors. Still, the model is not as solid as I&amp;#8217;d hoped for, and some last minute changes were needed to bridge the gap before this code was ready to implement the two use cases I was targeting.&lt;/p&gt;
&lt;p&gt;Since the last iteration would be a bit cumbersome to describe in newsletter form, please just &lt;a href="http://is.gd/jbvdB"&gt;check out my final commit&lt;/a&gt; for this project on github. With this new code, it&amp;#8217;s possible to get output identical to our target story through the following two examples.&lt;/p&gt;
&lt;h3&gt;&lt;span class="caps"&gt;CASE&lt;/span&gt; 1: line_shape_demo.rb&lt;/h3&gt;
&lt;pre name = "code" class = "ruby"&gt;
require_relative "falling_blocks"

game = FallingBlocks::Game.new
line_shape = FallingBlocks::Piece.new([[0,0],[0,1],[0,2],[0,3]])
game.piece = line_shape
game.piece_position = [3,3]
game.add_junk([[0,0], [1,0], [2,0], [2,1], [4,0],
              [4,1], [4,2], [5,0], [5,1], [6,0],
              [7,0], [8,0], [8,1], [9,0], [9,1],
              [9,2]])

puts game

puts "\nBECOMES:\n\n"

game.update_junk
puts game
&lt;/pre&gt;
&lt;h3&gt;&lt;span class="caps"&gt;CASE&lt;/span&gt; 2: bended_shape_demo.rb&lt;/h3&gt;
&lt;pre name = "code" class = "ruby"&gt;
require_relative "falling_blocks"

game = FallingBlocks::Game.new
bent_shape = FallingBlocks::Piece.new([[0,1],[0,2],[1,0],[1,1]])
game.piece = bent_shape
game.piece_position = [2,3]
game.add_junk([[0,0], [1,0], [2,0], [2,1], [4,0],
              [4,1], [4,2], [5,0], [5,1], [6,0],
              [7,0], [8,0], [8,1], [9,0], [9,1],
              [9,2]])

puts game

puts "\nBECOMES:\n\n"

game.update_junk
puts game
&lt;/pre&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;Once I outlined the story by drawing some ascii art, it took me just over 1.5 hours to produce working code that performs the transformations described. Overall, I&amp;#8217;d call that a success.&lt;/p&gt;
&lt;p&gt;That having been said, working on this problem was not without hurdles. While it turns out that removing completed lines and turning pieces into junk upon collision is surprisingly simple, I am still uneasy about my final design. It seems that there is considerable duplication between the grid maintained by &lt;tt&gt;Game&lt;/tt&gt; and the &lt;tt&gt;Canvas&lt;/tt&gt; object. But a refactoring here would be non-trivial, and I wouldn&amp;#8217;t want to attempt it without laying down some tests to minimize the amount of time hunting down subtle bugs.&lt;/p&gt;
&lt;p&gt;For me, this is about as far as I can write code organically in a single sitting without either writing tests, or doing some proper design in front of whiteboard, or a combination of the two. I think it&amp;#8217;s important to recognize this limit, and also note that it varies from person to person and project to project. The key to writing a good prototype is getting as close to that line as you can without flying off the edge of a cliff.&lt;/p&gt;
&lt;p&gt;In the end though, what I like about this prototype is that it isn&amp;#8217;t just an illusion. With a little work, it&amp;#8217;d be easy enough to scale up to my initial ambition of demonstrating a free falling piece. By adding some tests and doing some refactoring, it&amp;#8217;d be possible to evolve this code into something that could be used in production rather than just treating it as throwaway demo-ware.&lt;/p&gt;
&lt;p&gt;Hopefully, seeing how I decomposed the problem, and having a bit of insight into what my though process was like as I worked on this project has helped you understand what goes into making proof-of-concept code in Ruby. I&amp;#8217;ve not actually taught extensively about this process before, so describing it is a bit of an experiment for me. Let me know what you think!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 05 May 2011 20:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/044-issue-12-rapid-prototyping.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/044-issue-12-rapid-prototyping.html</guid></item><item><title>Issue #11: Uses for Modules (4 of 4)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 21, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Today we&amp;#8217;re going to wrap up this series on modules by looking at how mixins can be useful for implementing custom behavior on individual objects. In particular, we&amp;#8217;ll be looking at how modules can be used both as a replacement for monkey patching, as well as for constructing systems that can be extended without the need for monkey patching. While neither of these techniques are going to be something you&amp;#8217;ll use every day, they really come in handy when you run into a situation that calls for them.&lt;/p&gt;
&lt;h3&gt;Modules instead of Monkey Patches&lt;/h3&gt;
&lt;p&gt;Back in the bad old days before Prawn, I was working on a reporting framework called Ruby Reports (Ruport), which generated &lt;span class="caps"&gt;PDF&lt;/span&gt; reports via &lt;tt&gt;&lt;span class="caps"&gt;PDF&lt;/span&gt;::Writer&lt;/tt&gt;. At the time, &lt;tt&gt;&lt;span class="caps"&gt;PDF&lt;/span&gt;::Writer&lt;/tt&gt; was quite buggy, and essentially abandoned, but was the only game in town when it came to &lt;span class="caps"&gt;PDF&lt;/span&gt; generation.&lt;/p&gt;
&lt;p&gt;One of the bugs was something fairly critical: Memory consumption for outputting simple &lt;span class="caps"&gt;PDF&lt;/span&gt; tables would balloon like crazy, causing a document with more than a few pages to take anywhere from several minutes to several &lt;strong&gt;hours&lt;/strong&gt; to run.&lt;/p&gt;
&lt;p&gt;The original author of the library had a patch laying around that inserted a hook which did some caching that greatly reduced the memory consumption, but he had not tested it extensively and did not want to want to cut a release. I had talked to him about possibly monkey patching &lt;tt&gt;&lt;span class="caps"&gt;PDF&lt;/span&gt;::Document&lt;/tt&gt; in Ruport&amp;#8217;s code to add this patch, but together, we came up with a better solution: wrap the patch in a module.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module PDFWriterMemoryPatch
  unless self.class.instance_methods.include?("_post_transaction_rewind")
    def _post_transaction_rewind
      @objects.each { |e| e.instance_variable_set(:@parent,self) }
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;In Ruport&amp;#8217;s &lt;span class="caps"&gt;PDF&lt;/span&gt; formatter code, we did something like the following to apply our patch:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
@document = PDF::Document.new
@document.extend(Ruport::PDFWriterMemoryPatch)
&lt;/pre&gt;
&lt;p&gt;Throughout our application, whenever someone interacted with a &lt;tt&gt;&lt;span class="caps"&gt;PDF&lt;/span&gt;::Document&lt;/tt&gt; instance we created, they had a patched instance that fixed the memory leak. This meant from the Ruport user&amp;#8217;s perspective, the bug was fixed. So what makes this different from monkey patching?&lt;/p&gt;
&lt;p&gt;Because we were only manipulating the individual objects that we created in our library, we were not making a global change that might surprise people. For example if someone was building an application that only implicitly loaded Ruport as a dependency, and they created a &lt;tt&gt;&lt;span class="caps"&gt;PDF&lt;/span&gt;::Document&lt;/tt&gt; instance, our patch would not be loaded. This prevented us from causing unexpected behavior in any code that lived outside of Ruport itself.&lt;/p&gt;
&lt;p&gt;While this approach didn&amp;#8217;t shield us from the risks that a future change to &lt;tt&gt;&lt;span class="caps"&gt;PDF&lt;/span&gt;::Writer&lt;/tt&gt; could potentially break our patch in Ruport, it did prevent any risk of global consequences. Anyone who&amp;#8217;s ever spent a day scratching their head because of some sloppy monkey patch in a third party dependency will immediately be able to see the value of this sort of isolation.&lt;/p&gt;
&lt;p&gt;The neat thing is that a similar approach can be used for core extensions as well. Rather than re-opening Ruby core classes, you can imbue individual instances with custom behavior, getting many of the benefits of monkey patching without the disadvantages. For example, suppose you want to add the &lt;tt&gt;sum)()&lt;/tt&gt; and &lt;tt&gt;average()&lt;/tt&gt; methods to Array. If we were monkey patching, we&amp;#8217;d write something like this&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Array
  def sum
    inject(0) { |s,e| s + e }
  end

  def average
    sum.to_f / length
  end
end

obj = [1,3,5,7]
obj.sum     #=&amp;gt; 16
obj.average #=&amp;gt; 4
&lt;/pre&gt;
&lt;p&gt;The danger here of course is that you&amp;#8217;d be globally stomping anyone else&amp;#8217;s definition of &lt;tt&gt;sum()&lt;/tt&gt; and &lt;tt&gt;average()&lt;/tt&gt;, which can lead to ugly conflicts. All these problems can be avoided with a minor modification.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module ArrayMathHelpers
  def sum
    inject(0) { |s,e| s + e }
  end

  def average
    sum.to_f / length
  end
end

obj = [1,3,5,7]
obj.extend(ArrayMathHelpers)
obj.sum     #=&amp;gt; 16
obj.average #=&amp;gt; 4
&lt;/pre&gt;
&lt;p&gt;By explicitly mixing in the &lt;tt&gt;ArrayMathHelpers&lt;/tt&gt; module, we isolate our changes just to the objects we&amp;#8217;ve created ourselves. With slight modification, this technique can also be used with objects passed into functions, typically by making a copy of the object before working on it.&lt;/p&gt;
&lt;p&gt;An interesting thing about this approach is that since the modules mixed into an instance of an object are looked up before the methods defined by the object&amp;#8217;s class, you can actually use this technique for modifying existing behavior of an object as well. The example below demonstrates modifying &lt;tt&gt;&amp;lt;&amp;lt;&lt;/tt&gt; on strings so that it allows appending arbitrary objects to a string through coercion.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module LooseStringAppend
  def &amp;lt;&amp;lt;(value)
    super
  rescue TypeError
    super(value.to_s)
  end
end

a = "foo"
a.extend(LooseStringAppend)
a &amp;lt;&amp;lt; :bar &amp;lt;&amp;lt; :baz #=&amp;gt; "foobarbaz"
&lt;/pre&gt;
&lt;p&gt;Of course this (like most core modifications), is a horrible idea. But speaking as a pure technique, this is far better than the alternative global monkey patch shown below:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class String
  alias_method :old_append, :&amp;lt;&amp;lt;
  
  def &amp;lt;&amp;lt;(value)
    old_append(value)
  rescue TypeError
    old_append(value.to_s)
  end
end
&lt;/pre&gt;
&lt;p&gt;When using per-object mixins as an alternative to monkey patching, what you gain is essentially two things: A first class seat in the lookup path allowing you to make use of &lt;tt&gt;super()&lt;/tt&gt;, and isolation on a per-object behavior so that consumers of your code don&amp;#8217;t curse you for patching things in unexpected ways. While this approach isn&amp;#8217;t always available, it is definitely preferable whenever you can choose it over monkey patching.&lt;/p&gt;
&lt;p&gt;In Ruby 2.0, we may end up with even better option for this sort of thing called refinements, which are also module based. But for now, if you must hack other people&amp;#8217;s objects, this approach is a civil way to do it.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ll now take a look at how to produce libraries and applications that actively encourage extensions to be done this way.&lt;/p&gt;
&lt;h3&gt;Modules as Extension Points&lt;/h3&gt;
&lt;p&gt;This last section is not so much about practical advice as it is about taking what we&amp;#8217;ve learned so far and really stretching it as far as possible into new territories. In essence, what follows are my own experiments with ideas that I&amp;#8217;m not fully sure are good, but find interesting enough to share with you.&lt;/p&gt;
&lt;p&gt;In previous Practicing Ruby issues, I&amp;#8217;ve shown some code from a command line client we&amp;#8217;ve used for time tracking in my consulting work. The tool itself never quite matured far enough to be release ready, but I used it as a testing ground for new design ideas, so it is a good conversation starter at least.&lt;/p&gt;
&lt;p&gt;Today, I want to show how we implemented commands for it. Essentially, I want to walk through what happens when someone types the following command into their console:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
$ turbine start
Timer started at Wed Dec 15 17:55:37 -0500 2010
&lt;/pre&gt;
&lt;p&gt;Because we knew this tool would evolve over time, we wanted to make it as hackable as possible. To do this, we set up a system in which commands get installed into a hidden folder in each project, making it trivial to modify existing commands or add new ones. Here&amp;#8217;s a quick directory listing to show what that structure looks like:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
$ ls .turbine/commands/standard/
add.rb		project.rb	rewind.rb	status.rb commit.rb push.rb		
staged.rb	stop.rb drop.rb	reset.rb start.rb
&lt;/pre&gt;
&lt;p&gt;As you might expect, start.rb defines the start command. Here&amp;#8217;s what the source for it looks like.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
Turbine::Application.extension(:start_command) do
  def start
    timer = Turbine::Timer.new
    if timer.running?
      prompt.say "Timer already started, please stop or rewind first"
    else
      timer.write_timestamp
      prompt.say "Timer started at #{Time.now}"
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;You&amp;#8217;ll notice that all our commands are essentially direct mappings to method calls, which are responsible for doing all the work. While I&amp;#8217;ve significantly simplified the following &lt;tt&gt;Turbine::Application&lt;/tt&gt; definition to remove some domain specific callbacks and things like options parsing, you can see the basic harness which registers these commands in the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Turbine
  class Application
    def self.extensions
      @extensions ||= {}
    end

    def self.extension(key, &amp;amp;block)
      extensions[key] = Module.new(&amp;amp;block)
    end

    def initialize
      self.class.extensions.each do |_, extension|
        extend(extension)
      end
    end
  
    def run(command)
      send(command)
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;From this, we see that &lt;tt&gt;Turbine::Application&lt;/tt&gt; stores a Hash of anonymous modules which are created on the fly whenever the &lt;tt&gt;extension()&lt;/tt&gt; is called. The interesting thing about this design is that the commands aren&amp;#8217;t applied globally to &lt;tt&gt;Turbine::Application&lt;/tt&gt;, but instead, are mixed in at the individual instance level. The interesting thing about this approach is that it allows us to selectively disable features, or completely replace them with alternative implementations.&lt;/p&gt;
&lt;p&gt;For example, consider a custom command that gets loaded after the standard commands, which is implemented like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
Turbine::Application.extension(:start_command) do
  def go
    puts "Let's go!"
  end
end
&lt;/pre&gt;
&lt;p&gt;Because the module defining the &lt;tt&gt;go()&lt;/tt&gt; method would replace the original module in the extensions hash, the original module ends up getting completely wiped out. In retrospect, for my particular use case, this approach seems to be like using a thermonuclear weapon where a slingshot would do, but you can&amp;#8217;t argue that this fails to take extensibility to whole new limits.&lt;/p&gt;
&lt;p&gt;Eventually, when someone falls off the deep end in their study of modules, they ask &amp;#8216;is it possible to uninclude them?&amp;#8217;, and the short answer to that question is &amp;#8220;No&amp;#8221;, promptly followed up with &amp;#8220;Why would you want to do that?&amp;#8221;. But what we&amp;#8217;ve shown here is a good approximation for unincluding a module, even if we haven&amp;#8217;t quite figured out the answer to the &amp;#8216;why&amp;#8217; part yet.&lt;/p&gt;
&lt;p&gt;But sometimes, we have to explore just for the fun of it, right? :)&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;I have had a blast writing to you all about modules and answering your questions as they come up. Unfortunately, the topic is even bigger than I thought, and there are at least two full articles I could write on the topic,which might actually be more practical and immediately relevant than the materials I&amp;#8217;ve shared today. In particular, we didn&amp;#8217;t cover things like the &lt;tt&gt;included()&lt;/tt&gt; and &lt;tt&gt;extended()&lt;/tt&gt; hooks, which can be quite useful and are worth investigating on your own.&lt;/p&gt;
&lt;p&gt;Moving forward, my goals for Practicing Ruby are to be able to hit a wide range of topics, so we&amp;#8217;ll probably move away from the fundamentals of Ruby&amp;#8217;s object system and go back to some more problem-solving oriented topics in the coming weeks. But if you like this kind of format, please let me know.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 03 May 2011 13:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/043-issue-11-uses-for-modules.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/043-issue-11-uses-for-modules.html</guid></item><item><title>Supporting the Ruby Mendicant experiment</title><description>&lt;p&gt;&lt;i&gt;tl; dr; Since December 1, 2010 I&amp;#8217;ve been working full time working on community projects for software developers, primarily focusing on building out &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;, but also working on some writing and open source code as well. I&amp;#8217;m asking for small monthly payments from the community so that I can keep living this way. There is a subscription button and a list of benefits at the bottom of this post. Over 40 people have subscribed in the first 72 hours!&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Lots of people contribute to open source, but few labor at it. In a way, it&amp;#8217;s amazing how much progress we can make with what folks can do in the spare time. However, certain projects can&amp;#8217;t even get off the ground as a side project. Those are the kinds of problems I like to focus on.&lt;/p&gt;
&lt;h3&gt;My Story&lt;/h3&gt;
&lt;p&gt;In early 2008, I was able to take 22 weeks off of work to focus on building the &lt;span class="caps"&gt;PDF&lt;/span&gt; generation library Prawn for the Ruby community, because 70 individual Ruby developers along with RubyCentral helped me raise over $11,000 in funding in just a few weeks time. At the time they contributed, they did not know exactly what I was going to work on, but knew that I wanted to focus on big, hard problems that might not get solved in the traditional way of hacking in one&amp;#8217;s spare time. This initial investment from the community kickstarted development on Prawn and now I am proud to say the library has been downloaded over 130,000 times and has a core team of five people, with patches accepted from over 50 contributors.&lt;/p&gt;
&lt;p&gt;In June 2010, I decided to tackle a new project by founding Ruby Mendicant University. Through pledgie, I was able to raise a few thousand dollars to help with the initial build-out of the online school, but quickly realized that it would be come a much bigger task than I initially anticipated. At this point in time, I spend 40+ hours a week focusing on running Ruby Mendicant University, with 15-25 of that going directly to mentoring students during our courses. But the fruits of our labor have been tremendous: with my guidance, our students have produced a total of &lt;a href="http://university.rubymendicant.com/resources/student_projects.html"&gt;40 new contributions&lt;/a&gt; to open source software, and I&amp;#8217;ve produced over &lt;a href="http://university.rubymendicant.com/resources/learning_materials.html"&gt;25 exercises&lt;/a&gt; that have been released to the public. Additionally, the entire inspiration behind the Practicing Ruby articles you&amp;#8217;ve been reading on this blog have come from RbMU.&lt;/p&gt;
&lt;p&gt;Since December 1, 2010 I&amp;#8217;ve been dedicating myself to RbMU full time, and have only a trickle of income coming in from project management consulting work, the occasional paid training, and some of my paid writing work. While we&amp;#8217;ve managed to get by between that and what my wife makes, I&amp;#8217;m burning money faster than I can make it. I need a way to plug the leak that isn&amp;#8217;t a huge distraction to me, and so I&amp;#8217;m reaching out to the community for help.&lt;/p&gt;
&lt;h3&gt;Helping me work for the community full time&lt;/h3&gt;
&lt;p&gt;What I&amp;#8217;d like is for you to do is sign up for a small subscription payment to me, so that I can continue doing my work and not focus on monetization right now. By paying a monthly fee of as low as $4/mo, you&amp;#8217;ll be helping me do the following things:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Teach roughly two classes a month at Ruby Mendicant University&lt;/li&gt;
	&lt;li&gt;Continue to edit and release Practicing Ruby articles to this blog&lt;/li&gt;
	&lt;li&gt;Work on my new experimental book publishing toolchain: &lt;a href="https://github.com/sandal/bookie"&gt;Bookie&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Work on two books that will be released under a &amp;#8216;pay-what-you-want-or-not-at-all&amp;#8217; model; one on software design, the other on the Ruby object system.&lt;/li&gt;
	&lt;li&gt;Run field studies where I lead a team of RbMU alumni on making contributions to important open source projects. Currently RubySpec, rubygems.org, and HacketyHack are on our radar.&lt;/li&gt;
	&lt;li&gt;Run monthly public Q&amp;amp;A sessions with special guests.&lt;/li&gt;
	&lt;li&gt;Work on some sandbox learning environments that could be fun open source projects, such as my tower defense game engine &lt;a href="https://github.com/rmu/ivory_tower"&gt;IvoryTower&lt;/a&gt; and the elevator simulator &lt;a href="https://github.com/rmu/ups_and_downs"&gt;UpsAndDowns&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Anything else that comes up that&amp;#8217;s important and of benefit to the community.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In return, I&amp;#8217;d want to give you a couple things as well:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;A weekly newsletter about what I&amp;#8217;ve been working on, occassionally outlining interesting tools or techniques I&amp;#8217;ve come across as I work on things.&lt;/li&gt;
	&lt;li&gt;Access to a mailing list and &lt;span class="caps"&gt;IRC&lt;/span&gt; channel where you can ask me about the things I&amp;#8217;m working on and my plans for the future.&lt;/li&gt;
	&lt;li&gt;Access to a weekly office hours session where I spend a couple hours answering questions or giving code reviews to those who are supporting me through this program (with some restrictions depending on demand)&lt;/li&gt;
	&lt;li&gt;Provide a weekly updated report of my revenue from this program, so you can compare what I&amp;#8217;m taking in to what I&amp;#8217;m producing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are a couple caveats to keep in mind:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Supporting me through this program doesn&amp;#8217;t give you any direct influence over the projects I&amp;#8217;m working on, but you&amp;#8217;re free to cancel your subscription at any time, if I diverge from things you find valuable.&lt;/li&gt;
	&lt;li&gt;This is not a tax-deductible donation. I do want to sent up a &lt;span class="caps"&gt;NPO&lt;/span&gt; for running Ruby Mendicant University, but that will be several months from now. Instead, this is essentially a group-sourced investment in community projects.&lt;/li&gt;
	&lt;li&gt;I may cancel this program at any time, if it isn&amp;#8217;t working out.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;i&gt;If you&amp;#8217;re okay with those things and think this sounds promising, please go ahead and subscribe via the button below. I had 40 subscribers within the first 72 hours, but we need a whole lot more to make this work!&lt;/i&gt;&lt;/p&gt;
&lt;form action="https://www.paypal.com/cgi-bin/webscr" method="post"&gt;
&lt;input type="hidden" name="cmd" value="_s-xclick"&gt;
&lt;input type="hidden" name="hosted_button_id" value="KXTB8JV2ZDEEL"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type="hidden" name="on0" value="How much do you want to help?"&gt;How much do you want to help?&lt;/td&gt;&lt;p&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;select name="os0"&gt;&lt;br /&gt;
	&lt;option value="A lot"&gt;A lot : $8.00USD &amp;#8211; monthly&lt;/option&gt;&lt;br /&gt;
	&lt;option value="A whole lot"&gt;A whole lot : $16.00USD &amp;#8211; monthly&lt;/option&gt;&lt;br /&gt;
	&lt;option value="A ton"&gt;A ton : $32.00USD &amp;#8211; monthly&lt;/option&gt;&lt;br /&gt;
	&lt;option value="A little"&gt;A little : $4.00USD &amp;#8211; monthly&lt;/option&gt;&lt;/p&gt;
&lt;p&gt;&lt;/select&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/p&gt;
&lt;/table&gt;
&lt;input type="hidden" name="currency_code" value="USD"&gt;
&lt;input type="image" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/btn/btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"&gt;
&lt;p&gt;&lt;img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/scr/pixel.gif" width="1" height="1"&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;p&gt;I may have a more permanent home for this button at some point in the future, but right now want to see who&amp;#8217;s interested and take it from there.&lt;/p&gt;
&lt;h3&gt;Questions?&lt;/h3&gt;
&lt;p&gt;I know that this is a little different than what you might usually expect in the ways of community funding, and it might seem strange to you. I will answer any question you leave in the comments here!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 29 Apr 2011 09:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/042-mendicant-supporters.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/042-mendicant-supporters.html</guid></item><item><title>Issue #10.5: Addendum to Uses For Modules, Part 3</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 15, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/040-issue-10-uses-for-modules.html"&gt;last issue&lt;/a&gt;, we discussed the use of &lt;tt&gt;extend self&lt;/tt&gt; in great detail, but neglected to cover a pair of alternatives that seem on the surface to be functionally equivalent. While I don&amp;#8217;t want to spend too much time rehashing an old topic, I want to at least provide an example of each approach and comment on their quirks.&lt;/p&gt;
&lt;h3&gt;Defining methods at the module level&lt;/h3&gt;
&lt;p&gt;Occasionally folks ask whether mixing a module into itself via &lt;tt&gt;extend()&lt;/tt&gt; is equivalent to the code shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  def self.hello
    "hi"
  end
end
&lt;/pre&gt;
&lt;p&gt;The short answer to that question is &amp;#8220;no&amp;#8221;, but it is easy to see where the confusion comes from, because calling &lt;tt&gt;Greeter.hello&lt;/tt&gt; does indeed work as expected. But the important distinction is that methods defined in this way are simply directly defined on the module itself and so cannot be mixed into anything at all. There is really very little difference between the above code and the example below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
obj = Object.new

def obj.hello
  "hi"
end
&lt;/pre&gt;
&lt;p&gt;Consider our earlier example of Ruby&amp;#8217;s &lt;tt&gt;Math&lt;/tt&gt; or &lt;tt&gt;FileUtils&lt;/tt&gt; modules. With both of these modules, you can envision scenarios in which you would call the functions on the modules themselves. But there are also cases where using these modules as mixins would make a lot of sense. For example, Ruby itself ships with a math mode (-m) for irb which mixes in the &lt;tt&gt;Math&lt;/tt&gt; module at the top level so you can call its functions directly.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
$ irb -m
&amp;gt;&amp;gt; sin(Math::PI/2)
=&amp;gt; 1.0
&lt;/pre&gt;
&lt;p&gt;In the above example, if &lt;tt&gt;sin()&lt;/tt&gt; were implemented by simply defining the method directly on the &lt;tt&gt;Math&lt;/tt&gt; module, there would be no way to mix it into anything. While sometimes it might make sense to force a module to never be used as a mixin, that use case is rare, and so very little is gained by directly defining methods on modules rather than using the &lt;tt&gt;extend self&lt;/tt&gt; technique.&lt;/p&gt;
&lt;h3&gt;Using &lt;tt&gt;module_function&lt;/tt&gt;&lt;/h3&gt;
&lt;p&gt;Before people got in the habit of mixing modules into themselves, they often relied on a more specialized feature called &lt;tt&gt;module_function&lt;/tt&gt; to accomplish the same goals.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  module_function

  def hello
    "hi"
  end
end
&lt;/pre&gt;
&lt;p&gt;This code allows the direct calling of &lt;tt&gt;Greeter.hello&lt;/tt&gt;, and does not prevent &lt;tt&gt;Greeter&lt;/tt&gt; from being mixed into other objects. It also has a neat alternative syntax that allows you to selectively choose certain methods to be module functions while leaving others accessible via mixin only.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  def hello
    "hi"
  end

  def goodbye
    "bye"
  end

  module_function :hello
end
&lt;/pre&gt;
&lt;p&gt;With this modified definition, it is still possible to call &lt;tt&gt;Greeter.hello&lt;/tt&gt;, but attempting to call &lt;tt&gt;Greeter.goodbye&lt;/tt&gt; would raise a &lt;tt&gt;NoMethodError&lt;/tt&gt;. This sort of sounds like it offers the benefits of extending a module with itself, but with some added granularity. Unfortunately, there is something about &lt;tt&gt;module_function&lt;/tt&gt; that makes it quite weird to work with.&lt;/p&gt;
&lt;p&gt;As it turns out, &lt;tt&gt;module_function&lt;/tt&gt; works very different under the hood than self-mixins do. This is because &lt;tt&gt;module_function&lt;/tt&gt; actually doesn&amp;#8217;t manipulate the method lookup path, but instead, it makes a direct copy of the specified methods and attaches them to the module itself. If that sounds too weird to be true, check out the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt; 
module Greeter
  def hello
    "hi"
  end

  module_function :hello

  def hello
    "howdy"
  end
end

Greeter.hello #=&amp;gt; "hi"

class Foo
  include Greeter
end

Foo.new.hello #=&amp;gt; "howdy"
&lt;/pre&gt;
&lt;p&gt;Pretty weird behavior, right? You may find it interesting to know that I was not actually aware that &lt;tt&gt;module_function&lt;/tt&gt; made copies of methods until I wrote Issue #10 and was tipped off about this by one of our readers. However, I did know about one of the consequences of &lt;tt&gt;module_function&lt;/tt&gt; being implemented in this way: private methods cannot be used in conjunction with &lt;tt&gt;module_function&lt;/tt&gt;. That means that the following example cannot be literally translated to use &lt;tt&gt;module_function&lt;/tt&gt;.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module MinimalAnswer
  extend self

  def match?(pattern, input)
    pattern.split(/,/).any? do |e|
      normalize(input) =~ /\b#{normalize(e)}/i
    end
  end

  private

  def normalize(input)
    input.downcase.strip.gsub(/\s+/," ").gsub(/[?.!\-,:'"]/, '')
  end
end 
&lt;/pre&gt;
&lt;p&gt;From these examples, we see that &lt;tt&gt;module_function&lt;/tt&gt; is more flexible than defining methods directly on your modules, but not nearly as versatile as extending a module with itself. While the ability to selectively define which methods can be called directly on the module is nice in theory, I&amp;#8217;ve yet to see a use case for it where it would lead to a much better design.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;With the alternatives to &lt;tt&gt;extend self&lt;/tt&gt; having unpleasant quirks, it&amp;#8217;s no surprise that they&amp;#8217;re quickly falling out of fashion in the Ruby world. But since no technical decision should be made based on dogma or a blind-faith acceptance of community conventions, these notes hopefully provide the necessary evidence to help you make good design decisions on your own.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 21 Apr 2011 20:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/041-issue-10.5-uses-for-modules.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/041-issue-10.5-uses-for-modules.html</guid></item><item><title>Issue #10: Uses for Modules (3 of 4)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 14, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In the last two issues, we covered mixins and namespacing, two of the most common uses for modules. In the second half of this series, we&amp;#8217;ll look at some other ways to use modules that are not quite so obvious.&lt;/p&gt;
&lt;p&gt;Today, we&amp;#8217;ll focus on the question that caused me to write this series in the first place. Many readers were confused by my use of &lt;tt&gt;extend self&lt;/tt&gt; within earlier Practicing Ruby articles, and this lead to a number of interesting questions on the mailing list at the time these articles were originally published. While I tried my best to answer them directly, I think we&amp;#8217;re in much better shape for studying this topic now that the last two articles have laid a bit of a foundation for us.&lt;/p&gt;
&lt;h3&gt;Review of how &lt;tt&gt;extend()&lt;/tt&gt; works&lt;/h3&gt;
&lt;p&gt;To understand this trick of mixing modules into themselves, one first must understand how &lt;tt&gt;extend()&lt;/tt&gt; works. We covered this concept at the end of the last article, but we can touch on it again for good measure. Start by considering the trivial module shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  def hello
    "hi"
  end
end 
&lt;/pre&gt;
&lt;p&gt;We had shown that unlike &lt;tt&gt;include()&lt;/tt&gt; which is especially designed for augmenting class definitions so that a mixin can add instance methods to some target class, &lt;tt&gt;extend()&lt;/tt&gt; has a much more simple behavior and works with any object.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
obj = Object.new
obj.extend(Greeter)
obj.hello #=&amp;gt; "hi"
&lt;/pre&gt;
&lt;p&gt;From this, we can see that mixing in a module by using extend simply mixes the methods defined by the module directly at that object&amp;#8217;s level. In this way, the methods defined by the module are mixed into the receiver, no matter what that object is.&lt;/p&gt;
&lt;p&gt;In Ruby, classes and modules are ordinary objects. We can confirm this by doing a tiny bit of introspection on &lt;tt&gt;Greeter&lt;/tt&gt;.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; Greeter.object_id
=&amp;gt; 212500
&amp;gt;&amp;gt; Greeter.class
=&amp;gt; Module
&amp;gt;&amp;gt; Greeter.respond_to?(:extend)
=&amp;gt; true
&lt;/pre&gt;
&lt;p&gt;While this may be a mental leap for some, you might be able to find peace with it by considering the ordinary module definition syntax to be a bit of sugar that is functionally equivalent to the following bit of code.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
Greeter = Module.new do
  def hello
    "hi"
  end
end
&lt;/pre&gt;
&lt;p&gt;When written in this way, it becomes far more obvious that &lt;tt&gt;Greeter&lt;/tt&gt; is actually just an instance of the class Module, making it an ordinary Ruby object at its core. Once you feel that you understand this point, consider what happens when the following line of code is run.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
Greeter.extend(Greeter)
&lt;/pre&gt;
&lt;p&gt;If we compare this to previous examples of &lt;tt&gt;extend()&lt;/tt&gt;, it should be clear now that despite the seemingly circular reference, this line does exactly what it would if called on any other object: It mixes the methods defined by &lt;tt&gt;Greeter&lt;/tt&gt; directly into the &lt;tt&gt;Greeter&lt;/tt&gt; object itself. A simple test confirms this to be true.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
Greeter.hello #=&amp;gt; "hi"
&lt;/pre&gt;
&lt;p&gt;If we unravel things a bit, we find that we could have written our &lt;tt&gt;extend()&lt;/tt&gt; call slightly differently, by doing it from within the module definition itself:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  extend Greeter

  def hello
    "hi"
  end
end
&lt;/pre&gt;
&lt;p&gt;The reason &lt;tt&gt;extend()&lt;/tt&gt; works here is because &lt;tt&gt;self == Greeter&lt;/tt&gt; in this context. Noticing this detail allows us to use slightly more dynamic approach, resulting in the code shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  extend self

  def hello
    "hi"
  end
end
&lt;/pre&gt;
&lt;p&gt;You&amp;#8217;ll find this new code to be functionally identical to the previous example, but slightly more flexible. Now, if we change the name of our module, we won&amp;#8217;t need to update our &lt;tt&gt;extend()&lt;/tt&gt; call. This is why folks tend to write &lt;tt&gt;extend self&lt;/tt&gt; rather than &lt;tt&gt;extend TheCurrentModule&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Hopefully by now, it is clear that this trick does not involve any sort of special casing for modules, and is an ordinary application of the &lt;tt&gt;extend()&lt;/tt&gt; method provided by every Ruby object. The only thing that might be confusing is the seemingly recursive nature of the technique, but this issue disappears when you recognize that modules are not mixed into anything by default, and that modules themselves are not directly related to the methods they define. If you understand the difference between class and instance methods in Ruby, this isn&amp;#8217;t a far stretch from that concept.&lt;/p&gt;
&lt;p&gt;While the inner workings of modules are an interesting academic topic, my emphasis is always firmly set on practical applications of programming techniques rather than detached conceptual theory. So now that we&amp;#8217;ve answered &amp;#8216;how does this work?&amp;#8217;, let&amp;#8217;s focus on the much more interesting &amp;#8216;how can I use it?&amp;#8217; topic.&lt;/p&gt;
&lt;h3&gt;Self-Mixins as Function Bags&lt;/h3&gt;
&lt;p&gt;A fascinating thing about Ruby is the wide range of different software design paradigms it supports. While object-oriented design is heavily favored, Ruby can do a surprisingly good job of emulating everything from procedure programming to prototype-based programming. But the one area that Ruby overlaps most with is functional programming.&lt;/p&gt;
&lt;p&gt;Now, before you retire your parenthesis for good and herald Ruby as a replacement for &lt;span class="caps"&gt;LISP&lt;/span&gt;, be warned: There is a lot about Ruby&amp;#8217;s design that makes it a horrible language for functional programming. But when used sparingly, techniques from the functional world fit surprisingly well in Ruby programs. The technique I find most useful is the ability to organize related functions together under a single namespace.&lt;/p&gt;
&lt;p&gt;When we create class definitions, we tend to think of the objects we&amp;#8217;re building as little structures which manage state and provide behaviors which manipulate that state. But sometimes, a more stateless model makes sense. The closer you get to pure mathematics, the more a pure functional model makes sense. We need to look no farther than Ruby&amp;#8217;s own &lt;tt&gt;Math&lt;/tt&gt; module for an example:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; Math.sin(Math::PI/2.0)
=&amp;gt; 1.0
&amp;gt;&amp;gt; Math.log(Math::E)
=&amp;gt; 1.0
&lt;/pre&gt;
&lt;p&gt;It seems unlikely that we&amp;#8217;d ever want to create an instance of a &lt;tt&gt;Math&lt;/tt&gt; object, since it doesn&amp;#8217;t really deal with any state that persists beyond a single function call. But it might be desirable to mix this functionality into another object so that you can call math functions without repeating the &lt;tt&gt;Math&lt;/tt&gt; constant all over the place. For this reason, Ruby implements &lt;tt&gt;Math&lt;/tt&gt; as a module.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; Math.class
=&amp;gt; Module
&lt;/pre&gt;
&lt;p&gt;For another great example of modular code design in Ruby itself, be sure to check out the &lt;tt&gt;FileUtils&lt;/tt&gt; standard library, which allows you to basic *nix file operations as if they were just ordinary function calls.&lt;/p&gt;
&lt;p&gt;After seeing how Ruby is using this technique, I didn&amp;#8217;t find it hard to stumble upon scenarios in my own code that could benefit from a similar design. For example, when I was working on building out the backend for a trivia website, I was given some logic for normalizing user input so that it could be compared against a predetermined pattern.&lt;/p&gt;
&lt;p&gt;While I could have stuck this logic in a number of different places, I decided I wanted to put it within a module of its own, because its logic did not rely on any persistent state and could be defined independently of the way our questions and quizzes were modeled. The following code is what I came up with:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module MinimalAnswer
  extend self

  def match?(pattern, input)
    pattern.split(/,/).any? do |e| 
      normalize(input) =~ /\b#{normalize(e)}/i 
    end
  end

  private

  def normalize(input)
    input.downcase.strip.gsub(/\s+/," ").gsub(/[?.!\-,:'"]/, '')
  end
end
&lt;/pre&gt;
&lt;p&gt;The nice thing about the code above is that using a modular design doesn&amp;#8217;t force you to give up things like private methods. This allows you to keep your user facing &lt;span class="caps"&gt;API&lt;/span&gt; narrow while still being able to break things out into helper methods.&lt;/p&gt;
&lt;p&gt;Here is a simple example of how my &lt;tt&gt;MinimalAnswer&lt;/tt&gt; module is used within the application:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; MinimalAnswer.match?("Cop,Police Officer", "COP")
=&amp;gt; true
&amp;gt;&amp;gt; MinimalAnswer.match?("Cop,Police Officer", "police officer")
=&amp;gt; true
&amp;gt;&amp;gt; MinimalAnswer.match?("Cop,Police Officer", "police office")
=&amp;gt; false
&amp;gt;&amp;gt; MinimalAnswer.match?("Cop,Police Officer", "police officer.")
=&amp;gt; true
&lt;/pre&gt;
&lt;p&gt;Now as I said before, this is a minor bit of functionality and could probably be shelved onto something like a &lt;tt&gt;Question&lt;/tt&gt; object or somewhere else within the system. But the downside of that approach would be that as this &lt;tt&gt;MinimalAnswer&lt;/tt&gt; logic began to get more complex, it would begin to stretch the scope of whatever object you attached this logic to. By breaking it out into a module right away, we give this code its own namespace to grow in, and also make it possible to test the logic in isolation, rather than trying to bootstrap a potentially much more complex object in order to test it.&lt;/p&gt;
&lt;p&gt;So whenever you have a bit of logic that seems to not have many state dependencies between its functions, you might consider this approach. But since stateless code is rare in Ruby, you may wonder if learning about self-mixins really bought us that much.&lt;/p&gt;
&lt;p&gt;As it turns out, the technique can also be used in more stateful scenarios when you recognize that Ruby modules are objects themselves, and like any object, can contain instance data.&lt;/p&gt;
&lt;h3&gt;Self-Mixins for Implementing Singleton Pattern&lt;/h3&gt;
&lt;p&gt;Ruby overloads the term &amp;#8216;singleton object&amp;#8217;, so we need to be careful about terminology here. What I&amp;#8217;m about to show you is how to use these self-mixed modules to implement something similar to the &lt;a href="http://en.wikipedia.org/wiki/Singleton_pattern"&gt;Singleton design pattern&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve found in object design that objects typically need zero, one, or many instances. When an object doesn&amp;#8217;t really need to be instantiated at all because it has no data in common between its behaviors, the modular approach we just reviewed often works best. The vast majority of the remaining cases fall into ordinary class definitions which facilitate many instances. Virtually everything we model fits into this category, so it&amp;#8217;s not worth discussing in detail. However, there are some cases in which a single object is really all we need. In particular, configuration systems come to mind.&lt;/p&gt;
&lt;p&gt;The following example shows a simple &lt;span class="caps"&gt;DSL&lt;/span&gt; I wrote for the trivia application I had mentioned earlier. It may look familiar, and that is because it appeared in our discussion on writing configuration systems some weeks ago. This time around, our focus will be on how this system actually works rather than what purpose it serves.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
AccessControl.configure do
  role "basic",
    :permissions =&amp;gt; [:read_answers, :answer_questions]

  role "premium",
    :parent      =&amp;gt; "basic",
    :permissions =&amp;gt; [:hide_advertisements]

  role "manager",
    :parent      =&amp;gt; "premium",
    :permissions =&amp;gt; [:create_quizzes, :edit_quizzes]

  role "owner",
    :parent      =&amp;gt; "manager",
    :permissions =&amp;gt; [:edit_users, :deactivate_users]
end 
&lt;/pre&gt;
&lt;p&gt;To implement code that allows the definitions above to be modeled internally, we need to consider how this system will be used. While it is easy to imagine roles shifting over time, getting added and removed as needed, it&amp;#8217;s hard to imagine what the utility of having more than one &lt;tt&gt;AccessControl&lt;/tt&gt; object would be.&lt;/p&gt;
&lt;p&gt;For this reason, it&amp;#8217;s fairly safe to say that &lt;tt&gt;AccessControl&lt;/tt&gt; configuration data is essentially global information, and so does not need the data segregation that creating instances of a class provides.&lt;/p&gt;
&lt;p&gt;By modeling &lt;tt&gt;AccessControl&lt;/tt&gt; as a module rather than class, we end up with an object that we can store data on that can&amp;#8217;t be instantiated.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module AccessControl
  extend self

  def configure(&amp;amp;block)
    instance_eval(&amp;amp;block)
  end

  def definitions
    @definitions ||= {}
  end

  # Role definition omitted, replace with a stub if you want to test
  # or refer to Practicing Ruby Issue #4
  def role(level, options={})
    definitions[level] = Role.new(level, options)
  end

  def roles_with_permission(permission)
    definitions.select { |k,v| v.allows?(permission) }.map { |k,_| k }
  end

  def [](level)
    definitions[level]
  end 
end
&lt;/pre&gt;
&lt;p&gt;There are two minor points of potential confusion in this code worth discussing, the first is the use of &lt;tt&gt;instance_eval&lt;/tt&gt; in &lt;tt&gt;configure()&lt;/tt&gt;, and the second is that the &lt;tt&gt;definitions()&lt;/tt&gt; method refers to instance variables. This is where we need to remind ourselves that the scope of methods defined by a module cannot be determined until it is mixed into something.&lt;/p&gt;
&lt;p&gt;Once we recognize these key points, a bit of introspection shows us what is really going on.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; AccessControl.configure { "I am #{self.inspect}" }
=&amp;gt; "I am AccessControl"
&amp;gt;&amp;gt; AccessControl.instance_eval { "I am #{self.inspect}" }
=&amp;gt; "I am AccessControl"
&amp;gt;&amp;gt; AccessControl.instance_variables
=&amp;gt; ["@definitions"]
&lt;/pre&gt;
&lt;p&gt;Since &lt;tt&gt;AccessControl&lt;/tt&gt; is an ordinary Ruby object, it has ordinary instance variables and can make use of &lt;tt&gt;instance_eval&lt;/tt&gt; just like any other object. The key difference here is that &lt;tt&gt;AccessControl&lt;/tt&gt; is a module, not a class, and so cannot be used as a factory for creating more instances. In fact, calling &lt;tt&gt;AccessControl.new&lt;/tt&gt; raises a &lt;tt&gt;NoMethodError&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;In a traditional implementation of Singleton Pattern, you have a class which disables instantiation through the ordinary means, and creates a single instance that is accessible through the class method &lt;tt&gt;instance()&lt;/tt&gt;. However, this seems a bit superfluous in a language in which classes are full blown objects, and so isn&amp;#8217;t necessary in Ruby.&lt;/p&gt;
&lt;p&gt;For cases like the configuration system we&amp;#8217;ve shown here, choosing to use this approach is reasonable. That having been said, the reason why I don&amp;#8217;t have another example that I can easily show you is that with the exception of this narrow application for configuration objects, I find it relatively rare to have a legitimate need for the Singleton Pattern. I&amp;#8217;m sure if I thought long and hard on it, I could dig some other examples up, but upon looking at recent projects I find that variants of the above are all I use this technique for.&lt;/p&gt;
&lt;p&gt;However, if you work with other people&amp;#8217;s code, it is likely that you&amp;#8217;ll run into someone implementing Singleton Pattern this way. Now, rather than scratching your head, you will have a solid understanding of how this technique works, and why someone might want to use it.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;In Issue 11, we&amp;#8217;ll be wrapping up with some even more specialized examples of using modules, showing how they can be used to build plugin systems as well as how they can be used as a replacement for monkey patching. But before we close the books on today&amp;#8217;s lesson, I&amp;#8217;d like to share some thoughts that were rattling around in the back of my mind while I was preparing this article.&lt;/p&gt;
&lt;p&gt;The techniques I&amp;#8217;ve shown today can be useful in certain edge case scenarios where an ordinary class definition might not be the best tool to use. But upon reviewing my own code, I find that I use the first technique of creating function bags often but sparingly in each project, and the second technique of building singleton objects rarely and typically only for configuration systems.&lt;/p&gt;
&lt;p&gt;Upon reflection, I wonder to myself whether the upsides of these techniques outweigh the cost of explaining them. I don&amp;#8217;t really have a definitive answer to that question, but it&amp;#8217;s really something I think about often.&lt;/p&gt;
&lt;p&gt;On the one hand, I feel that users of Ruby should have an ingrained understanding of its object system. Afterall, these are actually fairly straightforward techniques once you understand how things work under the hood. It&amp;#8217;s also true that you can&amp;#8217;t really claim to understand Ruby&amp;#8217;s object system without fully understanding these examples. Having a weak understanding of how Ruby&amp;#8217;s objects work is sure to rob you of the joy of working in Ruby, so for this reason, I feel like &amp;#8216;dumbing down&amp;#8217; our code would be a bad thing.&lt;/p&gt;
&lt;p&gt;On the other hand, I think that for the small gains yielded by using these techniques, we require those who are reading our code to understand a whole score of details that are unique to Ruby. When you consider that by changing a couple lines of code, you can have a design which is not much worse but is understandable by pretty much anyone who has programmed in an OO language before, it&amp;#8217;s certainly tempting to cater to the lowest common denominator.&lt;/p&gt;
&lt;p&gt;But this sort of split-mindedness is inevitable in Ruby, and comes up in many scenarios. The truth of the matter is that it&amp;#8217;s going to take many more years before Ruby is truly understood by the programming community at large. But as more people dive deeper into Ruby, Ruby is starting to come into its own, and the mindset that things should be done as they are in other languages is not nearly as common as it was several years ago. For this reason, it&amp;#8217;s important to stop thinking of Ruby in terms of whatever language you&amp;#8217;ve come from, and start thinking of it as its own thing. As soon as you do that, a whole range of possibilities open up.&lt;/p&gt;
&lt;p&gt;At least, that&amp;#8217;s what I think. What about you?&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 19 Apr 2011 09:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/040-issue-10-uses-for-modules.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/040-issue-10-uses-for-modules.html</guid></item><item><title>What have the RBP bloggers been up to?</title><description>&lt;p&gt;The last few weeks of releasing Practicing Ruby articles have been fun, but part of me still feels a bit of nostalgia for the days when Ruby Best Practices was an active blog on its own. Writing posts alongside Robert Klemme, James Britt, and Magnus Holm was a lot of fun for me, and it&amp;#8217;s a bit of a melancholy feeling to know that those days are behind us. To fight back against that feeling, I&amp;#8217;ve caught up with each of them to ask them what they&amp;#8217;ve been up to, and what they&amp;#8217;re working on now.&lt;/p&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/jamesbritt"&gt;James Britt&lt;/a&gt;: While still using Ruby as my bread-and-butter language I&amp;#8217;ve been trying to avoid getting stuck in a Ruby-rut, so I&amp;#8217;m exploring other technologies, both hard and soft.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m still getting up to speed with &lt;a href="http://www.haskell.org/haskellwiki/Haskell"&gt;Haskell&lt;/a&gt;, and it&amp;#8217;s a real head trip. For the last year I&amp;#8217;ve been buried in a Ruby project, so switching over to Haskell when time allows has not been easy for me.  However, I&amp;#8217;m making progress.  Interested readers should check out the books &lt;a href="http://book.realworldhaskell.org/"&gt;Real World Haskell&lt;/a&gt; and &lt;a href="http://learnyouahaskell.com/"&gt;Learn You a Haskell for Great Good!&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve also been trying to make time for &lt;a href="https://github.com/mirah/mirah"&gt;Mirah&lt;/a&gt;.  Mirah is quite the same sort of leap from Ruby as is Haskell.  In fact, it&amp;#8217;s so similar that I&amp;#8217;ve been using the vim Ruby syntax files for Mirah hacking.  However, it does have some type inferencing, a Haskell-ish quality.  I&amp;#8217;m currently using it to do the heavy lifting in a Monkeybars application that interacts with yet another of my interests: the Microsoft Kinect.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve become increasingly interested in physical computing, wearable technology, and inutuitive gesture-based human-computer interaction. So, in addition to messing with the Kinect I&amp;#8217;ve been doing some Arduino experiementing as well.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/rklemme"&gt;Robert Klemme&lt;/a&gt;: I still do Java development for a living while using Ruby for all sorts of development tools that make my daily work simpler &amp;#8211; or even possible at all.  For example, over time I have created quite a lot of Ruby scripts that automate interactions with the &lt;a href="http://perforce.com"&gt;Perforce revision control system&lt;/a&gt;.  These scripts typically interact with &amp;#8220;p4&amp;#8221; (the Perforce command line client) via IO.popen, parse output and either display results in different ways or further interact with Perforce.&lt;/p&gt;
&lt;p&gt;Currently I am experimenting with &lt;a href="https://github.com/jruby/jruby"&gt;JRuby&lt;/a&gt; integration into one of our products.  I use it to script certain parts of the business logic which need to be configurable at customer sites.  So far it is going quite well and I am impressed by the good integration which allows to augment Java classes with additional mixin Ruby modules.  The JRuby team has really done a great job here!&lt;/p&gt;
&lt;p&gt;I have yet to dive further into &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; which features some interesting concepts.  But this has to wait &amp;#8211; as has my blogging activity which got buried under loads of other matters.  I do hope to return to blogging though.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/judofyr"&gt;Magnus Holm&lt;/a&gt;: I&amp;#8217;ve been working on template engines lately: Both writing a little template compilation framework (&lt;a href="http://github.com/judofyr/temple"&gt;Temple&lt;/a&gt;), contributing to a new template engine (&lt;a href="http://github.com/stonean/slim"&gt;Slim&lt;/a&gt;) and helping &lt;a href="http://github.com/rtomayko/tilt"&gt;Tilt&lt;/a&gt; glue everything together. It&amp;#8217;s surprisingly fun to work with template engines: it&amp;#8217;s kinda like writing a compiler, but you&amp;#8217;re targeting an &amp;#8220;architecture&amp;#8221; that you really know &amp;#8211; Ruby!&lt;/p&gt;
&lt;p&gt;Blogging-wise, I&amp;#8217;ve started &lt;a href="http://timelessrepo.com"&gt;Timeless&lt;/a&gt;, a blog doesn&amp;#8217;t try to be a blog. Timeless turned out how I always wanted the Ruby Best Practices Blog to be: interesting content that&amp;#8217;s easily browsable. I haven&amp;#8217;t been able to write any more articles lately, but I have tons of ideas, so hopefully it&amp;#8217;ll continue to live.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve also tried to not get stuck in Ruby-land and start exploring other technologies, but so far it&amp;#8217;s been way too tempting to just use Ruby. However, I &lt;strong&gt;have&lt;/strong&gt; been able to finally learn myself some C, and been hacking on some data structures. It&amp;#8217;s something very pleasing about reading computer science papers you barely understand, but still being able to extract out the important ideas and implement it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;p&gt;After hearing what the other guys have been up to, you might be wondering what I&amp;#8217;ve been doing too. But since I&amp;#8217;m still maintaining this blog to post the Practicing Ruby issues, my &lt;a href="http://blog.rubybestpractices.com/about/gregory.html"&gt;bio page&lt;/a&gt; is up to date, so just check that out if you want to catch up with what I&amp;#8217;ve been doing.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Mon, 18 Apr 2011 16:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/039-rbp-bloggers-update.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/039-rbp-bloggers-update.html</guid></item><item><title>Issue #9: Uses for Modules (2 of 4)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 10, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h3&gt;Using Mix-ins to Augment Class Definitions&lt;/h3&gt;
&lt;p&gt;Although knowing &lt;a href="http://blog.rubybestpractices.com/posts/gregory/037-issue-8-uses-for-modules.html"&gt;how to use modules for namespacing&lt;/a&gt; is important, it&amp;#8217;s really only a small part of what you can do with modules. What modules do best is providing a convenient way to write code that be mixed into other objects, augmenting their behaviors. Because modules facilitate code sharing in a way that is distinct from both the general OO concept of class inheritance and from things like Java&amp;#8217;s interfaces, they require you to think about your design in a way that&amp;#8217;s a bit different from most other object oriented programming languages.&lt;/p&gt;
&lt;p&gt;While I imagine most of our readers are at least vaguely comfortable with using mixins, I&amp;#8217;ll refer to some basic examples of core Ruby mixins to illustrate their power before moving on to more subtle points.&lt;/p&gt;
&lt;p&gt;Consider the following bit of code which implements lazily evaluated computations:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Computation

  def initialize(&amp;amp;block)
    @action = block
  end

  def result
    @result ||= @action.call
  end

  def &amp;lt;(other)
    result &amp;lt; other.result
  end

  def &amp;gt;(other)
    result &amp;gt; other.result
  end

  def &amp;gt;=(other)
    result &amp;gt;= other.result
  end

  def &amp;lt;=(other)
    result &amp;lt;= other.result
  end

  def ==(other)
    result == other.result
  end

end

a = Computation.new { 1 + 1 }
b = Computation.new { 4*5 }
c = Computation.new { -3 }

p a &amp;lt; b  #=&amp;gt; true
p a &amp;lt;= b #=&amp;gt; true
p b &amp;gt; c  #=&amp;gt; true
p b &amp;gt;= c #=&amp;gt; true
p a == b #=&amp;gt; false
&lt;/pre&gt;
&lt;p&gt;While Ruby makes defining custom operators easy, there is a lot more code here than there needs to be. We can easily clean it up by mixing in Ruby&amp;#8217;s built in &lt;tt&gt;Comparable&lt;/tt&gt; module.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Computation
  include Comparable

  def initialize(&amp;amp;block)
    @action = block
  end

  def result
    @result ||= @action.call
  end

  def &amp;lt;=&amp;gt;(other)
    return  0 if result == other.result
    return  1 if result &amp;gt; other.result
    return -1 if result &amp;lt; other.result
  end
end

a = Computation.new { 1 + 1 }
b = Computation.new { 4*5 }
c = Computation.new { -3 }

p a &amp;lt; b  #=&amp;gt; true
p a &amp;lt;= b #=&amp;gt; true
p b &amp;gt; c  #=&amp;gt; true
p b &amp;gt;= c #=&amp;gt; true
p a == b #=&amp;gt; false
&lt;/pre&gt;
&lt;p&gt;We see that our individual operator definitions have disappeared, and in its place are two new bits of code. The first new thing is just an include statement that tells Ruby to mix the &lt;tt&gt;Comparable&lt;/tt&gt; functionality into the &lt;tt&gt;Computation&lt;/tt&gt; class definition. But in order to make use of the mixin, we need to tell &lt;tt&gt;Comparable&lt;/tt&gt; how to evaluate the sort order of our &lt;tt&gt;Computation&lt;/tt&gt; objects, and that&amp;#8217;s where &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt; comes in.&lt;/p&gt;
&lt;p&gt;The &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt; method, sometimes called the spaceship operator, essentially fills in a template method that allows &lt;tt&gt;Comparable&lt;/tt&gt; to work. It codifies the notion of comparison in an abstract manner by expecting the method to return &lt;tt&gt;-1&lt;/tt&gt; when the current object is considered less than the object it is being compared to, &lt;tt&gt;0&lt;/tt&gt; when the two are considered equal, and &lt;tt&gt;1&lt;/tt&gt; when the current object is considered greater than the object it is being compared to.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re still scratching your head a bit, pretend that rather than being a core Ruby object, that we&amp;#8217;ve implemented &lt;tt&gt;Comparable&lt;/tt&gt; ourselves by writing the following code.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Comparable
  def ==(other)
    (self &amp;lt;=&amp;gt; other) == 0
  end

  def &amp;lt;(other)
    (self &amp;lt;=&amp;gt; other) == -1
  end

  def &amp;lt;=(other)
    self &amp;lt; other || self == other
  end

  def &amp;gt;(other)
    (self &amp;lt;=&amp;gt; other) == 1
  end

  def &amp;gt;=(other)
    self &amp;gt; other || self == other
  end
end
&lt;/pre&gt;
&lt;p&gt;Now, if you imagine these method definitions literally getting pasted into your &lt;tt&gt;Computation&lt;/tt&gt; class when &lt;tt&gt;Comparable&lt;/tt&gt; is included, you&amp;#8217;ll see that it would provide a behavior that is functionally equivalent to our initial example.&lt;/p&gt;
&lt;p&gt;Of course, it wouldn&amp;#8217;t make sense for Ruby to implement such a feature for us without using it in its own structures. As it turns out, Ruby&amp;#8217;s numeric classes all implement &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt;, so we can actually simplify our definition even further by simply delegating our &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt; call to the result of the computations.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Computation
  include Comparable

  def initialize(&amp;amp;block)
    @action = block
  end

  def result
    @result ||= @action.call
  end

  def &amp;lt;=&amp;gt;(other)
    result &amp;lt;=&amp;gt; other.result
  end
end
&lt;/pre&gt;
&lt;p&gt;The only requirement for this code to work as expected is that each &lt;tt&gt;Computation&lt;/tt&gt;&amp;#8217;s result must implement the &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt; method. Since all objects that mix in &lt;tt&gt;Comparable&lt;/tt&gt; have to implement &lt;tt&gt;&amp;lt;=&amp;gt;&lt;/tt&gt;, any comparable object returned as a result should work fine here.&lt;/p&gt;
&lt;p&gt;While not a technically complicated example, there is surprising power in having a primitive built into your programming language which trivializes the implementation of the Template Method design pattern. If you look at Ruby&amp;#8217;s &lt;tt&gt;Enumerable&lt;/tt&gt; module and the powerful features it offers, you might think it would be a much more complicated example to study. But it too hinges on Template Method and requires only an &lt;tt&gt;each()&lt;/tt&gt; method to give you all sorts of complex functionality including things like &lt;tt&gt;select()&lt;/tt&gt;, &lt;tt&gt;map()&lt;/tt&gt;, and &lt;tt&gt;inject()&lt;/tt&gt;. If you haven&amp;#8217;t tried it before, you should certainly try to roll your own &lt;tt&gt;Enumerable&lt;/tt&gt; module to get a sense of just how useful mixins can be.&lt;/p&gt;
&lt;p&gt;We can also invert this relationship by having our class define a template, and then relying on the module that we mix in to provide the necessary details. If we look back at an previous example &lt;tt&gt;TicTacToe&lt;/tt&gt;, we can see a practical example of this technique by looking at the play method in our &lt;tt&gt;TicTacToe::Game&lt;/tt&gt; class.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module TicTacToe
  class Game
    def play
      catch(:finished) do
        loop do
          start_new_turn
          show_board

          check_move { |error_message| puts error_message }
          check_win { puts "#{current_player} wins" }
          check_draw { puts "It's a tie" }
        end
      end
    end

    # ...
  end
end
&lt;/pre&gt;
&lt;p&gt;In this code, we wanted to keep our event loop abstract, and rely on a mixed in module to provide the logic for executing and validating a move as well as checking end game conditions. As a result, we ended up with the &lt;tt&gt;TicTacToe::Rules&lt;/tt&gt; module shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module TicTacToe
  module Rules
    def check_move
      row, col = move_input
      board[row, col] = current_player
    rescue TicTacToe::Board::InvalidRequest =&amp;gt; error
      yield error.message if block_given?
      retry
    end

    def check_win
      return false unless board.last_move

      win = board.intersecting_lines(*board.last_move).any? do |line|
        line.all? { |cell| cell == current_player }
      end

      if win
        yield
        game_over
      end
    end

    def check_draw
      if @board.covered?
        yield
        game_over
      end
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;When we look at this code, we see some basic business logic implementing the rules of Tic Tac Toe, with some placeholder hooks being provided by yield that allows the calling code to inject some logic at certain key points in the process. This is how we manage to split the UI code from the game logic, without creating frivolous adapter classes.&lt;/p&gt;
&lt;p&gt;While this is amore complicated example than our walkthrough of &lt;tt&gt;Comparable&lt;/tt&gt;, the two share a common thread. In both cases, some coupling exists between the module and the object it is being mixed into. This is a common pattern when using mixins, in which the module and the code it is mixed into have to do a bit of a secret handshake to be able to talk to one another, but as long as they agree on that, neither needs to know about the other&amp;#8217;s inner workings. The end result is two components which must agree on an interface but do not need to necessarily understand each other&amp;#8217;s implementations. Code with this sort of coupling is easy to test and easy to refactor.&lt;/p&gt;
&lt;h3&gt;Using Mix-ins to Augment Objects Directly&lt;/h3&gt;
&lt;p&gt;As you probably either already know or can imagine, Ruby&amp;#8217;s mixin capability is not limited to simply including new behavior into a class definition. You can also extend the behavior of a class itself, through the use of the &lt;tt&gt;extend()&lt;/tt&gt; method. We can look to the Ruby standard library &lt;i&gt;forwardable&lt;/i&gt; for a nice example of how this is used. Consider the following trivial &lt;tt&gt;Stack&lt;/tt&gt; implementation.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "forwardable"

class Stack
  extend Forwardable

  def_delegators :@data, :push, :pop, :size, :first, :empty?

  def initialize
    @data = []
  end
end
&lt;/pre&gt;
&lt;p&gt;In this example, we can see that after we extend our &lt;tt&gt;Stack&lt;/tt&gt; class with the &lt;tt&gt;Forwardable&lt;/tt&gt; module, we are provided with a class level method called &lt;tt&gt;def_delegators&lt;/tt&gt; which allows us to easily define methods which delegate to an object stored in the specified instance variable. Playing around with the &lt;tt&gt;Stack&lt;/tt&gt; object a bit should illustrate what this code has done for us.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; stack = Stack.new
=&amp;gt; #&amp;lt;Stack:0x4f09c @data=[]&amp;gt;
&amp;gt;&amp;gt; stack.push 1
=&amp;gt; [1]
&amp;gt;&amp;gt; stack.push 2
=&amp;gt; [1, 2]
&amp;gt;&amp;gt; stack.push 3
=&amp;gt; [1, 2, 3]
&amp;gt;&amp;gt; stack.size
=&amp;gt; 3
&amp;gt;&amp;gt; until stack.empty?
&amp;gt;&amp;gt;   p stack.pop
&amp;gt;&amp;gt; end
3
2
1
&lt;/pre&gt;
&lt;p&gt;As before, it may be helpful to think about how we might implement &lt;tt&gt;Forwardable&lt;/tt&gt; ourselves. The following bit of code shows one way to approach the problem.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module MyForwardable
  def def_delegators(ivar, *delegated_methods)
    delegated_methods.each do |m|
      define_method(m) do |*a, &amp;amp;b|
        obj = instance_variable_get(ivar)
        obj.send(m,*a, &amp;amp;b)
      end
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;While the metaprogramming aspects of this may be a bit noisy to read if you&amp;#8217;re not familiar with them, this is fairly vanilla dynamic Ruby code. If you&amp;#8217;ve got Ruby 1.9.2 installed, you can actually try it out on your own and verify that it does indeed work as expected. But the practical use case of this code isn&amp;#8217;t what&amp;#8217;s important here.&lt;/p&gt;
&lt;p&gt;The key thing to notice about this code is that while it essentially implements a class method, nothing in the module&amp;#8217;s syntax directly indicates this to be the case. The only hint we get that this is meant to be used at the class level is the use of &lt;tt&gt;define_method()&lt;/tt&gt;, but we need to dig into the implementation code to notice that.&lt;/p&gt;
&lt;p&gt;Before we wrap up, we should investigate why this is the case.&lt;/p&gt;
&lt;h3&gt;A Brief Stroking of the Beard&lt;/h3&gt;
&lt;p&gt;The key thing to recognize is that &lt;tt&gt;include()&lt;/tt&gt; mixes methods into the instances of the base object while &lt;tt&gt;extend()&lt;/tt&gt; mixes methods into the base object itself. Notice that this is more general than a class method / instance method dichotomy.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s explore a few differently possibilities using a somewhat contrived example so that we can focus on the mixin mechanics. First, we start with an ordinary module, which is somewhat useless on its own.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module Greeter
  def hello
    "hi"
  end
end
&lt;/pre&gt;
&lt;p&gt;By including &lt;tt&gt;Greeter&lt;/tt&gt; into &lt;tt&gt;SomeClass&lt;/tt&gt;, we make it so that we can now call &lt;tt&gt;hello()&lt;/tt&gt; on instances of &lt;tt&gt;SomeClass&lt;/tt&gt;.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class SomeClass
  include Greeter
end

SomeClass.new.hello #=&amp;gt; "hi"
&lt;/pre&gt;
&lt;p&gt;But as we saw in the &lt;tt&gt;Forwardable&lt;/tt&gt; example, extending &lt;tt&gt;AnotherClass&lt;/tt&gt; with &lt;tt&gt;Greeter&lt;/tt&gt; would allow us to call the hello method directly at the class level, as in the example below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class AnotherClass
  extend Greeter
end

AnotherClass.hello #=&amp;gt; "hi"
&lt;/pre&gt;
&lt;p&gt;Be sure to note at this point that &lt;tt&gt;extend()&lt;/tt&gt; and &lt;tt&gt;include()&lt;/tt&gt; are two totally different operations. Because you did not extend &lt;tt&gt;SomeClass&lt;/tt&gt; with &lt;tt&gt;Greeter&lt;/tt&gt;, you could not call &lt;tt&gt;SomeClass.hello()&lt;/tt&gt;. Similarly, you cannot call &lt;tt&gt;AnotherClass.new.hello()&lt;/tt&gt; without explicitly including Greeter.&lt;/p&gt;
&lt;p&gt;From the examples so far, it might seem as if &lt;tt&gt;include()&lt;/tt&gt; is for defining instance methods, and &lt;tt&gt;extend()&lt;/tt&gt; is for class methods. But that is not quite accurate, and the next bit of code illustrates just how much deeper the rabbit hole goes.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
obj = Object.new
obj.extend(Greeter)
obj.hello #=&amp;gt; "hi"
&lt;/pre&gt;
&lt;p&gt;Before you let this example make you go cross-eyed, let&amp;#8217;s review the key point I made at the beginning of this section: &lt;i&gt;The key thing to recognize is that &lt;tt&gt;include()&lt;/tt&gt; mixes methods into the instances of the base object while &lt;tt&gt;extend()&lt;/tt&gt; mixes methods into the base object itself.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Since not every base object can have instances, not every object can have modules included into them (in fact, only classes can). But &lt;strong&gt;every&lt;/strong&gt; object can be extended by modules. This includes, among other things, classes and modules themselves.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s try to bring the two &lt;tt&gt;extend()&lt;/tt&gt; examples closer together with the following little snippet:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
MyClass = Class.new
MyClass.extend(Greeter)
MyClass.hello #=&amp;gt; "hi"
&lt;/pre&gt;
&lt;p&gt;If you feel like you fully understand the lines above, you&amp;#8217;re ready for the rest of this mini-series. If not, please ponder the following questions and post your thoughts in the comments section.&lt;/p&gt;
&lt;h3&gt;Questions To Consider&lt;/h3&gt;
&lt;ul&gt;
	&lt;li&gt;Why do we have both &lt;tt&gt;include()&lt;/tt&gt; and &lt;tt&gt;extend()&lt;/tt&gt; available to us? Why not just have one way of doing mixins?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;When you write &lt;tt&gt;extend()&lt;/tt&gt; within a class definition, does it do any sort of special casing? Or is it the same as calling &lt;tt&gt;extend()&lt;/tt&gt; on any other object?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Except for mixing in class methods, what is &lt;tt&gt;extend()&lt;/tt&gt; useful for?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Please feel free to ask for hints on any of these if you&amp;#8217;re stumped, or share your answers if you&amp;#8217;d like to help others and maybe get a bit of feedback to check your assumptions against.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 14 Apr 2011 16:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/038-issue-9-uses-for-modules.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/038-issue-9-uses-for-modules.html</guid></item><item><title>Issue #8: Uses for Modules (1 of 4)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 8, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Modules are part of what makes Ruby&amp;#8217;s design beautiful. However, since they do not have a direct analogy in any mainstream programming language, it is easy to get a bit confused about what they should be used for. While most folks quickly encounter at least some of their use cases, typically only very experienced Ruby developers know their true versatilty.&lt;/p&gt;
&lt;p&gt;In this four part article series, I aim to demystify Ruby modules by showing many practical use cases, explaining some tricky details along the way. We&amp;#8217;ll work through some of the fundamentals in the first two issues, and move into more advanced examples in the second two. Today we&amp;#8217;ll kick off this series by looking at the most simple, but perhaps most important ability modules offer us, the creation of namespaces.&lt;/p&gt;
&lt;h3&gt;Modules for Namespacing&lt;/h3&gt;
&lt;p&gt;Imagine that you are writing an &lt;span class="caps"&gt;XML&lt;/span&gt; generation library, and in it, you have a class to generate your &lt;span class="caps"&gt;XML&lt;/span&gt; documents. Perhaps uncreatively, you choose the name &lt;tt&gt;Document&lt;/tt&gt; for your class, creating something similar to what is shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Document
  def generate
    # ...
  end
end
&lt;/pre&gt;
&lt;p&gt;On its own, this seems to make a lot of sense; a user could do something simple like the following to make use of your library.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require "your_xml_lib"
document = Document.new
# do something with document
puts document.generate
&lt;/pre&gt;
&lt;p&gt;But imagine that you were using another library that generates &lt;span class="caps"&gt;PDF&lt;/span&gt; documents, which happens to use similar uncreative naming for its class that does the &lt;span class="caps"&gt;PDF&lt;/span&gt; document generation. Then, the following code would look equally valid.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "their_pdf_lib"
document = Document.new
# do something with document
puts document.generate
&lt;/pre&gt;
&lt;p&gt;As long as the two libraries were never loaded at the same time, there would be no issue. But as soon as someone loaded both libraries, some quite confusing behavior would happen. One might think that defining two different classes with the same name would lead to some sort of error being raised by Ruby, but with open classes, that is not the case. Ruby would actually apply the definitions of &lt;tt&gt;Document&lt;/tt&gt; one after the other, with whatever file was required last taking precedence. The end result would in all likelihood be a very broken &lt;tt&gt;Document&lt;/tt&gt; class that could generate neither &lt;span class="caps"&gt;XML&lt;/span&gt; nor &lt;span class="caps"&gt;PDF&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;But there really is no reason for this to happen, as long as both libraries take care to properly namespace things. Shown below is an example of two &lt;tt&gt;Document&lt;/tt&gt; classes that could co-exist peacefully.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
# somewhere in your_xml_lib

module XML
  class Document
    # ...
  end
end

# somewhere in their_pdf_lib

module PDF
  class Document
    # ...
  end
end
&lt;/pre&gt;
&lt;p&gt;Using both classes in the same application is as easy, as long as you explicitly include the namespace when referring to each library&amp;#8217;s &lt;tt&gt;Document&lt;/tt&gt; class.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
require "your_xml_lib"
require "their_pdf_lib"

# this pair of calls refer to two completely different classes
pdf_document = PDF::Document.new
xml_document = XML::Document.new
&lt;/pre&gt;
&lt;p&gt;The clash has been prevented because each library has nested its &lt;tt&gt;Document&lt;/tt&gt; class within a module, allowing the class to be defined within that namespace rather than at the global level. While this is a relatively straightforward concept, it&amp;#8217;s important to note a few things about what is really going on here.&lt;/p&gt;
&lt;p&gt;Firstly, namespacing actually applies to the way constants are looked up in Ruby in general, not classes in particular. This means that it applies to modules nested within modules as well as ordinary constants as well.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module A
  module B
  end
end

p A::B

module A
  C = 10
end

p A::C
&lt;/pre&gt;
&lt;p&gt;Secondly, this same behavior of using modules as namespaces applies just as well to classes, as in the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
class Blog
  class Comment
    #...
  end
end
&lt;/pre&gt;
&lt;p&gt;Be sure to note that in this example, nesting a class within a class does not in any way make it a subclass or establish any relationship between &lt;tt&gt;Blog&lt;/tt&gt; and &lt;tt&gt;Blog::Comment&lt;/tt&gt; except that &lt;tt&gt;Blog::Comment&lt;/tt&gt; is within the &lt;tt&gt;Blog&lt;/tt&gt; namespace. In the example below, you can see that a class nested within another class looks the same as a class nested within a module.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
blog = Blog.new
comment = Blog::Comment.new
# ...
&lt;/pre&gt;
&lt;p&gt;Of course, this technique is only really useful when you have a desired namespace for your library that also happens matches one of your class names. In all other situations, it makes sense to use a module for namespacing as it would prevent your users from creating instances of an empty and meaningless class.&lt;/p&gt;
&lt;p&gt;Finally, it is important to understand that constants are looked up from the innermost nesting to the outermost, finally searching the global namespace. This can be a bit confusing at times, especially when you consider some corner cases.&lt;/p&gt;
&lt;p&gt;For example, examine the following code:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module FancyReporter
  class Document
    def initialize
       @output = String.new
    end

    attr_reader :output
  end
end
&lt;/pre&gt;
&lt;p&gt;If you load this code into irb and play with a bit on its own, you can inspect an instance of Document to see that its output attribute is a core ruby &lt;tt&gt;String&lt;/tt&gt; object, as shown below:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
&amp;gt;&amp;gt; FancyReporter::Document.new.output
=&amp;gt; ""
&amp;gt;&amp;gt; FancyReporter::Document.new.output.class
=&amp;gt; String
&lt;/pre&gt;
&lt;p&gt;While this seems fairly obvious, it is easy for a bit of unrelated code written elsewhere to change everything. Consider the following code:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module FancyReporter
  module String
    class Formatter
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;While the designer of &lt;tt&gt;FancyReporter&lt;/tt&gt; was most likely just trying to be well organized by offering &lt;tt&gt;FancyReporter::String::Formatter&lt;/tt&gt;, this small change causes immediate headaches because it changes the meaning of &lt;tt&gt;String.new&lt;/tt&gt; in &lt;tt&gt;Document&lt;/tt&gt;&amp;#8217;s initialize method. In fact, you cannot even create an instance of &lt;tt&gt;Document&lt;/tt&gt; before the following error is raised:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
?&amp;gt; FancyReporter::Document.new
NoMethodError: undefined method `new' for FancyReporter::String:Module
	from (irb):35:in `initialize'
	from (irb):53:in `new'
	from (irb):53
&lt;/pre&gt;
&lt;p&gt;There are a number of ways this problem can be avoided. Often times, it&amp;#8217;s possible to come up with alternative names that do not clash with core objects, and when that&amp;#8217;s the case, it&amp;#8217;s preferable. In this particular case, &lt;tt&gt;String.new&lt;/tt&gt; can also be replaced with &lt;tt&gt;&amp;quot;&amp;quot;&lt;/tt&gt;, as nothing can change what objects are created via Ruby&amp;#8217;s string literal syntax. But there is also an approach that works independent of context, and that is to use explicit constant lookups from the global namespace. You can see an example of how explicit lookups look in the code below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
module FancyReporter
  class Document
    def initialize
       @output = ::String.new
    end

    attr_reader :output
  end
end
&lt;/pre&gt;
&lt;p&gt;Prepending any constant with &lt;tt&gt;::&lt;/tt&gt; will force Ruby to skip the nested namespaces and bubble all the way up to the root. In this sense, the difference between &lt;tt&gt;A::B&lt;/tt&gt; and &lt;tt&gt;::A::B&lt;/tt&gt; is that the former is a sort of relative lookup whereas the latter is absolute from the root namespace.&lt;/p&gt;
&lt;p&gt;In general, having to use absolute lookups may be a sign that there is an unnecessary name conflict within your application. But if upon investigation you find names that inheritently collide with one another, you can use this tool to avoid any ambiguity in your code.&lt;/p&gt;
&lt;p&gt;While we&amp;#8217;ve mostly covered the mechanics of namespacing, all this talk about &lt;tt&gt;::&lt;/tt&gt; compells me to share a cautionary tale of mass cargoculting before we wrap up for today. Please bear with me as I stroke my beard for a moment.&lt;/p&gt;
&lt;h3&gt;Abusing the Constant Lookup Operator (&lt;tt&gt;::&lt;/tt&gt;)&lt;/h3&gt;
&lt;p&gt;In some older documentation, and some relatively recent code written by folks who learned from old documentation, you may see class methods being called in the manner shown below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
YAML::load(File::read("foo.yaml"))
&lt;/pre&gt;
&lt;p&gt;While the above code runs fine, it&amp;#8217;s only a historical accident that it does. In fact, &lt;tt&gt;::&lt;/tt&gt; was never meant for method invocation, class methods or otherwise. You can easily demonstrate that &lt;tt&gt;::&lt;/tt&gt; can be used to execute instance methods as well, which eliminates any notion that &lt;tt&gt;::&lt;/tt&gt; has some special &amp;#8216;class methods only&amp;#8217; distinction to it.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;  
"foo"::reverse #=&amp;gt; "oof"
&lt;/pre&gt;
&lt;p&gt;As far as I can tell, this style of method invocation actually came about as a documentation convention. In both formal documentation and in mailing list discussions, it can sometimes be difficult to discern whether someone is talking about a class method or instance method, since both can be called just as well with the dot operator. So, a convention was invented so that for a class &lt;tt&gt;Foo&lt;/tt&gt;, the instance method &lt;tt&gt;bar&lt;/tt&gt; would be referred to as &lt;tt&gt;Foo#bar&lt;/tt&gt;, and the class method &lt;tt&gt;bar&lt;/tt&gt; would be referred to as &lt;tt&gt;Foo::bar&lt;/tt&gt;. This did away with the dot entirely, leaving no room for ambiguity.&lt;/p&gt;
&lt;p&gt;Unfortunately, this lead to a confusing situation. Beginners would often type &lt;tt&gt;Foo#bar&lt;/tt&gt; to try to call instance methods, but were at least promptly punished for doing so because such code will not run at all. However, typing &lt;tt&gt;Foo::bar&lt;/tt&gt; does work! Thus, an entire generation of Ruby developers were born thinking that &lt;tt&gt;::&lt;/tt&gt; is some sort of special operator for calling class methods, and to an extent, others followed suit as a new convention emerged.&lt;/p&gt;
&lt;p&gt;The fact that &lt;tt&gt;::&lt;/tt&gt; will happily call methods for you has to do with internal implementation details of &lt;span class="caps"&gt;MRI&lt;/span&gt;, and so it&amp;#8217;s actually an undefined behavior, subject to change. As far as I know, there is no guarantee it will actually work as expected, and so it shouldn&amp;#8217;t be relied upon.&lt;/p&gt;
&lt;p&gt;In your code, you should feel free to replace any method calls that use this style with ordinary &lt;tt&gt;Foo.bar&lt;/tt&gt; calls. This actually reflects more of the true nature of Ruby, in that it doesn&amp;#8217;t emphasize the difference between class level calls and instance level calls, since that distinction isn&amp;#8217;t especially important. In documentation, things are a little trickier, but it is now generally accepted that &lt;tt&gt;Foo.bar&lt;/tt&gt; refers to a class method and &lt;tt&gt;Foo#bar&lt;/tt&gt; refers to an instance method. In cases where that distinction alone might be confusing, you could always be explicit, as in the example below.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
obj.bar # obj is an instance of Foo
&lt;/pre&gt;
&lt;p&gt;If this argument wasn&amp;#8217;t convincing enough on it&amp;#8217;s own, you should know that every time you replace a &lt;tt&gt;Foo::bar&lt;/tt&gt; call with &lt;tt&gt;Foo.bar&lt;/tt&gt;, a brand new baby unicorn is born beneath a magnificent double rainbow. That should be reason enough to reverse this outdated practice, right?&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;This article probably gave you more details than you ever cared to know about namespacing. But future articles will be sure to blow your mind with what else modules can do. However, if you have any questions or thoughts about what we&amp;#8217;ve discussed so far, feel free to leave them in the comments section below. When Practicing Ruby originally ran, a key feature were the great discussions we had on the mailing list. I&amp;#8217;d love to see the same thing happen here on this blog.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 13 Apr 2011 18:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/037-issue-8-uses-for-modules.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/037-issue-8-uses-for-modules.html</guid></item><item><title>Issue #7: Meditations on Bad and Good Code (2 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 3, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In Issue #6, you got to see my intentionally bad implementation of Tic Tac Toe. For today, I have promised to show you some better code and the steps I took to get there. But before we move forward, let&amp;#8217;s take a quick look back at where we started.&lt;/p&gt;
&lt;p&gt;To start this exercise, I had challenged myself to implement this simple game without using any user defined classes or methods. Given that I wanted to make sure I produced &lt;strong&gt;bad&lt;/strong&gt; code to start with, I got a little nervous when my back-of-the-napkin proof of concept didn&amp;#8217;t come out looking that bad. Here it is again below, for those who forgot about it.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
board = [[nil,nil,nil],
         [nil,nil,nil],
         [nil,nil,nil]]

players = [:X, :O].cycle

loop do
  current_player = players.next
  puts board.map { |row| row.map { |e| e || " " }.join("|") }.join("\n")
  print "\n&amp;gt;&amp;gt; "
  row, col = gets.split.map { |e| e.to_i }
  puts
  board[row][col] = current_player
end
&lt;/pre&gt;
&lt;p&gt;The above code is good demo-ware, as long as you type really carefully and conveniently forget to finish a game before hitting ctrl+c. But to make a real, playable implementation, some end game conditions and basic validations are necessary. To my great joy, adding those new features caused this tight little script to explode into a hot mess of intertwined logic and nasty little hacks. Check out &lt;a href="https://github.com/sandal/tictactoe/tree/7fd72a33aec33f75909d8c9d59a43423b0f66b24"&gt;the source tree&lt;/a&gt; that we ended up with at the end of Issue #6 to see how things turned.&lt;/p&gt;
&lt;p&gt;While concise at less than 60 lines of code, it&amp;#8217;s pretty easy to see that this isn&amp;#8217;t the kind of software we should aspire to be writing. So the challenge was to start here and end up somewhere better.&lt;/p&gt;
&lt;p&gt;Whenever I do this exercise with my students, there is a roadmap I follow that tends to lead to some decent insights. It roughly goes like this:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Get some basic file structures and namespaces in place so that you get yourself out of the global namespace and open the doors for scripting some examples or running things in irb without firing off a procedure automatically.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Break down the procedure into some separable chunks so that you can think about smaller parts of the problems, and more easily see the dependencies between the steps in the procedure.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Re-think the design by identifying areas where objects can put an abstraction barrier between different layers of data and logic. Strive to have each bit of code do one thing and one thing well.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Identify the leaky abstractions and dangly bits that didn&amp;#8217;t get ironed out by the last step. Aim for beautiful solutions, but be skeptical of over-engineering at this point. No problem can be modeled perfectly&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Reflect on the exercise, and ask yourself whether you&amp;#8217;ve gone far enough with your cleanup. If you feel like so, then be sure to think about whether you&amp;#8217;ve gone &lt;strong&gt;too&lt;/strong&gt; far!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the approach I took as I worked on this problem myself, and you&amp;#8217;ll be able to see it step by step in the git logs. I tried to write good log messages, so I will link to them rather than repeat what was said, but I&amp;#8217;ll also share some more big-picture oriented thoughts as I walk you through my work.&lt;/p&gt;
&lt;h3&gt;Basic organization first&lt;/h3&gt;
&lt;p&gt;Here is my first commit of the evening:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/sandal/tictactoe/commit/5af96941d74f8014a3276b77fe67c17e0ed5e2df"&gt;https://github.com/sandal/tictactoe/commit/5af96941d74f8014a3276b77fe67c17e0ed5e2df&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can see the changes and message at the above link, but to view the source tree, you&amp;#8217;ll want the link below:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/sandal/tictactoe/tree/5af96941d74f8014a3276b77fe67c17e0ed5e2df"&gt;https://github.com/sandal/tictactoe/tree/5af96941d74f8014a3276b77fe67c17e0ed5e2df&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Tiny changes really, but it&amp;#8217;s the first thing I do as soon as I&amp;#8217;ve exited &amp;#8216;spike mode&amp;#8217; on any project, no matter how small. The structure I&amp;#8217;ve used is fairly standard, and it does two things for me.&lt;/p&gt;
&lt;p&gt;1. Allows me to load my whole library with a single require. (See app.rb for example and note how it doesn&amp;#8217;t change throughout this walkthrough)&lt;/p&gt;
&lt;p&gt;2. Places 100% of what I build under a single constant&amp;#8217;s namespace (in this case, &lt;tt&gt;TicTacToe&lt;/tt&gt;)&lt;/p&gt;
&lt;p&gt;These two points pretty much guarantee me that I won&amp;#8217;t have any naming clashes or unexpected collisions with other people&amp;#8217;s code unless I plan on loading a library that might clobber the name &lt;tt&gt;TicTacToe&lt;/tt&gt; or the &lt;tt&gt;require&lt;/tt&gt; path of &lt;i&gt;&amp;#8220;tictactoe/*&amp;#8221;&lt;/i&gt;. It also makes it easy for me to start interacting with my code from scripts I write, from irb, and from unit tests. For so little work, we get a ton of benefit, and this is a great place to start when doing any sort of cleanup.&lt;/p&gt;
&lt;h3&gt;Basic Slicing and Dicing&lt;/h3&gt;
&lt;p&gt;My next goal is to start breaking my monolithic procedure into some smaller chunks so I can get a sense of what parts go well together and how they need to interact with each other.&lt;/p&gt;
&lt;p&gt;I start by realizing that using a singleton pattern for &lt;tt&gt;Game&lt;/tt&gt;, while possible, isn&amp;#8217;t a great idea. A function bag approach in which we pass board and player information around like crazy also wouldn&amp;#8217;t be great, so I decide to make &lt;tt&gt;Game&lt;/tt&gt; an ordinary class in this commit:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/sandal/tictactoe/commit/2579626bd73fc7ad9e7d0a87419d5ecab2aacdda"&gt;https://github.com/sandal/tictactoe/commit/2579626bd73fc7ad9e7d0a87419d5ecab2aacdda&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Read the message, and then if you&amp;#8217;d like, have a look at the updated source tree at:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/sandal/tictactoe/tree/2579626bd73fc7ad9e7d0a87419d5ecab2aacdda"&gt;https://github.com/sandal/tictactoe/tree/2579626bd73fc7ad9e7d0a87419d5ecab2aacdda&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I immediately make use this refactoring by breaking down the original game procedure into several smaller, simpler methods.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;COMMIT&lt;/span&gt; &lt;span class="caps"&gt;MESSAGE&lt;/span&gt;:&lt;br /&gt;
&lt;a href="https://github.com/sandal/tictactoe/commit/286724de5328fda779caa500ccc76a0ad5de2bd7"&gt;https://github.com/sandal/tictactoe/commit/286724de5328fda779caa500ccc76a0ad5de2bd7&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;SOURCE&lt;/span&gt; &lt;span class="caps"&gt;TREE&lt;/span&gt;:&lt;br /&gt;
&lt;a href="https://github.com/sandal/tictactoe/tree/286724de5328fda779caa500ccc76a0ad5de2bd7"&gt;https://github.com/sandal/tictactoe/tree/286724de5328fda779caa500ccc76a0ad5de2bd7&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;At this point, it&amp;#8217;s not uncommon for folks to think they&amp;#8217;re done refactoring. By giving things nicer names and distributing the pain points so that they&amp;#8217;re not all crammed together in one place, the code feels cleaner. But upon further investigation of this code, while perhaps understandability and organization have improved, flexibility and abstraction have not. This is what I like to call &amp;#8216;procedural programming with objects&amp;#8217;, and we can do better than this.&lt;/p&gt;
&lt;p&gt;The good news is, with the code cleaned up a bit, we see where some of the pain points are. When it seems like a large amount of your code is dedicated to handling a particular concept, that means you have an object begging to be born. Our handling of the game board logic in this code is a prime example.&lt;/p&gt;
&lt;h3&gt;Sneaking in Domain Models&lt;/h3&gt;
&lt;p&gt;A key principle of object oriented design is to do one thing and do it well. But what does that mean? Hopefully, this refactored &lt;tt&gt;Board&lt;/tt&gt; class gives you an idea!&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;COMMIT&lt;/span&gt; &lt;span class="caps"&gt;MESSAGE&lt;/span&gt;:&lt;br /&gt;
&lt;a href="https://github.com/sandal/tictactoe/commit/efcbf51bcc1f7d4d094c671b60761229aec3dded"&gt;https://github.com/sandal/tictactoe/commit/efcbf51bcc1f7d4d094c671b60761229aec3dded&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;SOURCE&lt;/span&gt; &lt;span class="caps"&gt;TREE&lt;/span&gt;:&lt;br /&gt;
&lt;a href="https://github.com/sandal/tictactoe/tree/efcbf51bcc1f7d4d094c671b60761229aec3dded"&gt;https://github.com/sandal/tictactoe/tree/efcbf51bcc1f7d4d094c671b60761229aec3dded&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you look at the &lt;tt&gt;Board&lt;/tt&gt; class, you&amp;#8217;ll see that it takes the concept of a Tic Tac Toe board and solidifies it so that when &lt;tt&gt;Game&lt;/tt&gt; works with it, &lt;tt&gt;Board&lt;/tt&gt; does the heavy lifting and &lt;tt&gt;Game&lt;/tt&gt; mostly just calls the methods it needs to get its job done. This lets &lt;tt&gt;Game&lt;/tt&gt; forget about some of the finer points like what the individual kinds of illegal moves are, or how to compute the intersecting lines that cross through a given point. This sort of black box effect gives us some real abstraction, which is exactly why object oriented programming is as good as they say it is.&lt;/p&gt;
&lt;p&gt;With this complex board logic out of the way and some updates to the way flow is handled in game, it&amp;#8217;s obvious that &lt;tt&gt;Game&lt;/tt&gt; is now something like a controller, and &lt;tt&gt;Board&lt;/tt&gt; is a model. But there are still some loose ends in &lt;tt&gt;Game&lt;/tt&gt;, things that actually look like logic rather than just flow control and dispatch. The majority of the code you see in this class has to do with implementing a user interface and basic event loop. So, methods like &lt;tt&gt;check_move&lt;/tt&gt;, &lt;tt&gt;check_win&lt;/tt&gt;, and &lt;tt&gt;check_draw&lt;/tt&gt; feel a little bit out of place, since they implement actual logic about the rules of the game rather than just how players interact with it.&lt;/p&gt;
&lt;p&gt;Sometimes, little leaks like this aren&amp;#8217;t a big deal. In fact, the code looks reasonable to me at this point and if I were doing this for my day job and wasn&amp;#8217;t trying to get in the record books for &amp;#8217;World&amp;#8217;s Best Tic Tac Toe Implementation&amp;#8217;, I&amp;#8217;d probably stop here.&lt;/p&gt;
&lt;p&gt;But we&amp;#8217;re already cruising now, so why don&amp;#8217;t we try to shoot for the stars?&lt;/p&gt;
&lt;h3&gt;Grail Quests&lt;/h3&gt;
&lt;p&gt;I really wanted to find a way to rip that last bit of domain logic out of &lt;tt&gt;Game&lt;/tt&gt;, and after wrestling a little bit, I came up with something.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;COMMIT&lt;/span&gt; &lt;span class="caps"&gt;MESSAGE&lt;/span&gt;:&lt;br /&gt;
&lt;a href="https://github.com/sandal/tictactoe/commit/0fef18d320af2bd1a08f5115a2b94e552205f218"&gt;https://github.com/sandal/tictactoe/commit/0fef18d320af2bd1a08f5115a2b94e552205f218&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;SOURCE&lt;/span&gt; &lt;span class="caps"&gt;TREE&lt;/span&gt;:&lt;br /&gt;
&lt;a href="https://github.com/sandal/tictactoe/tree/0fef18d320af2bd1a08f5115a2b94e552205f218"&gt;https://github.com/sandal/tictactoe/tree/0fef18d320af2bd1a08f5115a2b94e552205f218&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The thing I kept wrestling with was how to manage the screen output stuff. I wrestled with a bunch of ideas, including defining a simply &lt;tt&gt;display()&lt;/tt&gt; method on &lt;tt&gt;Game&lt;/tt&gt; like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def display(message)
  puts message
end
&lt;/pre&gt;
&lt;p&gt;The reason why I wanted this is so my Rules mixin could rely on a method that &lt;tt&gt;Game&lt;/tt&gt; provided for display rather than directly assuming console output. But I think that what I ended up with is better.&lt;/p&gt;
&lt;p&gt;Imagine that my &lt;tt&gt;check_draw&lt;/tt&gt; method in Rules was written like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def check_draw
  if @board.covered?
     display "It's a draw"
     game_over
  end
end
&lt;/pre&gt;
&lt;p&gt;It&amp;#8217;s almost a trivial difference &lt;strong&gt;except&lt;/strong&gt; that now we have a leak on the Rules side. If &lt;tt&gt;TicTacToe::Game&lt;/tt&gt; is now meant to exclusively be a UI event loop, having the messages that are displayed to the user caught up in some module seems a bit ugly.&lt;/p&gt;
&lt;p&gt;But instead, I chose to let &lt;tt&gt;Game&lt;/tt&gt; fill in the blanks with an implementation like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
def check_draw
  if @board.covered?
     yield
     game_over
  end
end
&lt;/pre&gt;
&lt;p&gt;This now allows the logic for a draw to live in Rules, but the calling code in &lt;tt&gt;Game&lt;/tt&gt; to look like this:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
check_draw { puts "It's a draw" }
&lt;/pre&gt;
&lt;p&gt;A place for everything and everything in its place! Time to go hang some banners on aircraft carriers, because well, Mission Accomplished.&lt;/p&gt;
&lt;h3&gt;Fear, Uncertainty, and Doubt&lt;/h3&gt;
&lt;p&gt;Is this final implementation an example of good Ruby code? Yeah, probably. Is it excellent? I really have no idea. At the very least, it&amp;#8217;s almost certainly not &amp;#8216;The Best Tic Tac Toe Implementation Ever&amp;#8217;.&lt;/p&gt;
&lt;p&gt;But really, the kind of perfection I was trying to seek in this exercise is not really what we should be looking for in our day to day work. Right now I have the amps cranked up to 11, when 7 or 8 would really do fine. But as I said before, this is one of my favorite exercises for learning and teaching. Here&amp;#8217;s why: It really gets me thinking.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m still trying to decide on whether extracting out the &lt;tt&gt;Rules&lt;/tt&gt; module was really necessary, and I also have some areas about this I still don&amp;#8217;t like. For example, I&amp;#8217;m not sure whether &lt;tt&gt;Board&lt;/tt&gt; should know more about the rules of the game, or even less. I don&amp;#8217;t like the hard coding I did of all the parameters of the game in there, but I can&amp;#8217;t put my finger on why. After all, it&amp;#8217;s very unlikely that Tic Tac Toe is suddenly going to become Chess and need to expand to an NxN board. Even if it did, wouldn&amp;#8217;t it need to change a whole lot to accommodate it?&lt;/p&gt;
&lt;p&gt;Still, I don&amp;#8217;t like things like these constants:&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
LEFT_DIAGONAL_POSITIONS  = [[0,0],[1,1],[2,2]]
RIGHT_DIAGONAL_POSITIONS = [[2,0],[1,1],[0,2]]
SPAN                     = (0..2)
CELL_COUNT               = 9
&lt;/pre&gt;
&lt;p&gt;There is a natural connascence between all four of these values, but the code to generalize their creation would be longer and much uglier to read than the above. So maybe it&amp;#8217;s a good choice to do it this way, but it makes the mathematician in me uneasy.&lt;/p&gt;
&lt;p&gt;Another thing I don&amp;#8217;t like about my design is &lt;tt&gt;Board#to_s&lt;/tt&gt;, because putting presentation logic on domain logic is nasty. But to make a view object or otherwise promote one line of code to something more complex seems to be a cure that is worse than the disease.&lt;/p&gt;
&lt;p&gt;But on the bright side of things, I really like the callback scheme for doing the bits of game logic like &lt;tt&gt;check_win&lt;/tt&gt; and &lt;tt&gt;check_draw&lt;/tt&gt; and passing in a block with the rendering code. This is actually a formal design pattern just hiding in a line of code, and things like that remind me of why Ruby is so beautiful.&lt;/p&gt;
&lt;p&gt;Also, I&amp;#8217;ve never used &lt;tt&gt;throw&lt;/tt&gt; / &lt;tt&gt;catch&lt;/tt&gt; before in real code. Never really saw why I&amp;#8217;d need it. But at a glance, my use of it here actually seems pretty expressive and appropriate given the situation. But because I&amp;#8217;ve never used it before, I&amp;#8217;m still glancing at it sideways with considerable doubt. I even had to wrap it in a method called &lt;tt&gt;game_over&lt;/tt&gt; to hide the throw keyword to get over my fear of its relative novelty. But now, my &lt;tt&gt;game_over&lt;/tt&gt; method is like some sort of crazy goto call&amp;#8230; and that makes me not so sure that this was a good idea afterall.&lt;/p&gt;
&lt;p&gt;Oh yeah, and I also didn&amp;#8217;t write any tests while working on this code. I thought about writing them, but I felt that it&amp;#8217;d cause me to think about the tests themselves more than the coding practices I was experimenting with. But then again, maybe if I wrote tests, I wouldn&amp;#8217;t be pondering the relative merits of my fancy &lt;tt&gt;game_over()&lt;/tt&gt; goto.&lt;/p&gt;
&lt;p&gt;And this is how this exercise always ends. It doesn&amp;#8217;t come together in a beautiful blossom of Ruby awesomeness, it just kind of falls off a cliff. But really, that&amp;#8217;s okay! Not every question needs to be answered, and as I said before, if this were something I was working on just to get a job done, I would happily make concessions where needed to avoid letting perfect become the enemy of the good.&lt;/p&gt;
&lt;p&gt;Still, this sort of practice gnaws on your subconscious, and I&amp;#8217;ve seen it lead to great progress in my own studies and in my students as well. Hopefully you&amp;#8217;ve enjoyed seeing this process in action, and will give it a try soon if you weren&amp;#8217;t able to try it out this week.&lt;/p&gt;
&lt;h3&gt;Submissions from our readers&lt;/h3&gt;
&lt;p&gt;I haven&amp;#8217;t had a chance to review them in depth, but a few of our readers did submit their own explorations to share with others. Check out the &lt;a href="https://github.com/sandal/tictactoe/network"&gt;github network graph&lt;/a&gt; to see what others have done.&lt;/p&gt;
&lt;p&gt;Looking forward to hearing your thoughts on this exercise, and whether it seems like something you could make good use of. Until next time, happy hacking!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 06 Apr 2011 04:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/036-issue-7-good-and-bad-code.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/036-issue-7-good-and-bad-code.html</guid></item><item><title>Issue #6: Meditations on Bad and Good Code (1 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on December 1, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In this issue and the next one, I&amp;#8217;d like to demonstrate one of my favorite learning exercises while inviting you to follow along at home. It&amp;#8217;s something I usually do while teaching in a live, one-on-one setting, but I think we can adapt it for a broader audience and still get a lot out of it.&lt;/p&gt;
&lt;p&gt;In this exercise, the goal is to first produce some bad code, and then later steadily improve it while explaining why each change is an improvement. I usually like to start with a very simple problem but then just add some twists about how to implement it to make sure it comes out pretty bad.&lt;/p&gt;
&lt;p&gt;One surefire way of writing bad code without resorting to intentionally writing things worse than they should be is to eliminate a few of Ruby&amp;#8217;s key organizational tools. In particular, if you want to write ugly code without it seeming fake, it is easy to do so if you never write any user defined functions, classes, or modules. So we&amp;#8217;ll do exactly that!&lt;/p&gt;
&lt;h3&gt;Implementing Tic-Tac-Toe as a single procedure.&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;ve chosen the game &lt;a href="http://en.wikipedia.org/wiki/Tic-Tac-Toe"&gt;Tic-Tac-Toe&lt;/a&gt; as the problem to focus on, because it only involves a few simple rules and can be implemented by anyone who has basic programming skills.&lt;/p&gt;
&lt;p&gt;In fact, if you ignore end game conditions and error handling, you can get a simple prompt for a two player game with just a few lines of Ruby.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
board = [[nil,nil,nil],
         [nil,nil,nil],
         [nil,nil,nil]]

players = [:X, :O].cycle

loop do
  current_player = players.next
  puts board.map { |row| row.map { |e| e || " " }.join("|") }.join("\n")
  print "\n&amp;gt;&amp;gt; "
  row, col = gets.split.map { |e| e.to_i }
  puts
  board[row][col] = current_player
end
&lt;/pre&gt;
&lt;p&gt;But of course, the devil is in the details. To get a fully playable game, you need some basic error checking to ensure that you can&amp;#8217;t play out of bounds or on top of another player&amp;#8217;s marker. You also need to figure out when a player has won, and when the game has ended in a draw. While this doesn&amp;#8217;t sound like a lot of work, you&amp;#8217;ll see in the code below how much complexity these simple changes add.&lt;/p&gt;
&lt;pre name = "code" class = "ruby"&gt;
board   = [[nil,nil,nil],
           [nil,nil,nil],
           [nil,nil,nil]]

left_diagonal  = [[0,0],[1,1],[2,2]]
right_diagonal = [[2,0],[1,1],[0,2]]

players = [:X, :O].cycle

current_player = players.next

loop do
  puts board.map { |row| row.map { |e| e || " " }.join("|") }.join("\n")
  print "\n&amp;gt;&amp;gt; "
  row, col = gets.split.map { |e| e.to_i }
  puts

  begin
    cell_contents = board.fetch(row).fetch(col)
  rescue IndexError
    puts "Out of bounds, try another position"
    next
  end

  if cell_contents
    puts "Cell occupied, try another position"
    next
  end

  board[row][col] = current_player

  lines = []

  [left_diagonal, right_diagonal].each do |line|
    lines &amp;lt;&amp;lt; line if line.include?([row,col])
  end

  lines &amp;lt;&amp;lt; (0..2).map { |c1| [row, c1] }
  lines &amp;lt;&amp;lt; (0..2).map { |r1| [r1, col] }

  win = lines.any? do |line|
    line.all? { |row,col| board[row][col] == current_player }
  end

  if win
    puts "#{current_player} wins!"
    exit
  end

  if board.flatten.compact.length == 9
    puts "It's a draw!"
    exit
  end

  current_player = players.next
end
&lt;/pre&gt;
&lt;p&gt;While relatively short, you need to read through the whole script to really understand how any part of it operates. Of course, this script did not spring together fully formed, there was a thought process associated with it that drove it to this final implementation. For those curious, you can &lt;a href="https://gist.github.com/24ef3c8209877c1946bb"&gt;follow my stream of consciousness notes&lt;/a&gt; about what I was building and why in a step by step fashion.&lt;/p&gt;
&lt;p&gt;Seeing these notes will hopefully give you a bit of a sense of how this process might have gone if we were pair programming on this project, working in tiny iterations to push forward just a little bit farther each time. If so, you might already be catching a glimpse of what this exercise is all about. Otherwise, there is still more for us to do!&lt;/p&gt;
&lt;h3&gt;What Happens Next?&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;ve placed my bad tictactoe example in a &lt;a href="https://github.com/sandal/tictactoe/tree/7fd72a33aec33f75909d8c9d59a43423b0f66b24"&gt;repository on github&lt;/a&gt;. If you&amp;#8217;d like to participate, please fork this repository and make one change to the code at a time, leaving detailed reasoning in each commit message as to why you&amp;#8217;re making the change. Once you&amp;#8217;re happy with what you&amp;#8217;ve got, post a link in the comments section on this post so others can check out what you have done.&lt;/p&gt;
&lt;p&gt;In the next issue, I will post my own iterative set of improvements, as well as links to some of my favorite reader submissions. I will also summarize some of the lessons that can be learned from using this technique, and provide a few suggestions for other problems to attempt in this fashion.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;As always, please leave any questions, thoughts, or suggestions in the comments section below. These articles are much better when they&amp;#8217;re treated as discussions rather than monologues.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 24 Mar 2011 12:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/035-issue-6-good-and-bad-code.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/035-issue-6-good-and-bad-code.html</guid></item><item><title>Issue #5: Testing Anti-patterns; Testing Private Methods</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on November 25, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;. You should &lt;a href="http://twitter.com/seacreature"&gt;follow @seacreature on twitter&lt;/a&gt; if you want to keep up with my more recent projects.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Ruby has been described as having a test-obsessed culture. In many ways, this is a key strength of our community, as it often leads to producing better, more reliable software. Unfortunately, Ruby developers often focus more on the quantity of tests through metrics such as code coverage than they do on the quality of the tests they&amp;#8217;re writing.&lt;/p&gt;
&lt;p&gt;Crafting a well written test suite isn&amp;#8217;t easy, even for fairly experienced developers. After doing some initial sketches about what I could cover about improper ways to design and write tests, I realized I could probably dedicate an entire book to the topic. Rather than covering many anti-patterns in a shallow fashion, I decided to cover a single example in depth today. I&amp;#8217;ll revisit the topic from time to time in future issues, exposing a new ugly little corner of the Ruby testing world.&lt;/p&gt;
&lt;h3&gt;Testing Anti-Pattern: Testing private methods&lt;/h3&gt;
&lt;p&gt;If you are using &lt;tt&gt;send&lt;/tt&gt; to test private methods in your tests, you are almost certainly doing it wrong. Most private methods tend to fall into one of the following categories, none of which require &lt;tt&gt;send&lt;/tt&gt; to test:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;A method that does not have behavior of its own (a helper function)&lt;/li&gt;
	&lt;li&gt;A method that actually deserves to be public on the current object&lt;/li&gt;
	&lt;li&gt;A method that is only private to hide a design flaw&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Take a look at the three objects below and try to match them up with each pattern listed above.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Book
  def initialize(name)
    @name = name
  end

  def available_for_purchase?
    copies_remaining &amp;gt; 0     
  end

  private

  def copies_remaining
    Inventory.count(:book, @name)
  end
end

module Inventory
  extend self

  def count(item_type, name)
    item_class(item_type).find_by_name(name).quantity
  end

  def receive(item_type, name, quantity)
    item_class(item_type).create(name, quantity)
  end

  private

  def item_class(item_type)
    case item_type
    when :book
      InStockBook
    when :video
      InStockVideo
    end
  end
end

class InStockBook
  def self.titles
    @titles ||= {}
  end
  
  def self.find_by_name(name)
    titles[name]
  end

  def self.create(name, quantity)
    titles[name] = new(name, quantity)
  end

  def initialize(name, quantity)
    @title     = name
    @quantity  = quantity
  end

  attr_reader :title, :quantity

  def isbn
    @isbn ||= isbn_from_service
  end

  private

  def isbn_from_service
    isbn_service_connect

    isbn = @isbn_service.find_isbn_for(@title)

    isbn_service_disconnect

    return isbn
  end

  def isbn_service_connect
    @isbn_service = IsbnService.new
    @isbn_service.connect
  end

  def isbn_service_disconnect
    @isbn_service.disconnect
  end
end
&lt;/pre&gt;
&lt;p&gt;If you guessed that &lt;tt&gt;Inventory&lt;/tt&gt; was the object which demonstrated a private method that doesn&amp;#8217;t implement an external behavior, you guessed right. The sole purpose of &lt;tt&gt;Inventory#item_class&lt;/tt&gt; is just to make the code in &lt;tt&gt;Inventory#count&lt;/tt&gt; and &lt;tt&gt;Inventory#receive&lt;/tt&gt; a bit cleaner to read. Therefore, it&amp;#8217;d be wasteful to write an explicit test such as the one below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def test_item_class
  assert_equal InStockBook, Inventory.send(:item_class, :book)
end
&lt;/pre&gt;
&lt;p&gt;The following tests implicitly cover the functionality of &lt;tt&gt;Inventory#item_class&lt;/tt&gt; while focusing on actual interactions through the public interface.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def test_stocking_a_book
  Inventory.receive(:book, "Ruby Best Practices", 100)
  assert_equal 100, Inventory.count(:book, "Ruby Best Practices")
end
&lt;/pre&gt;
&lt;p&gt;Because indirectly testing a private method will result in the same code coverage results as testing the method directly, you won&amp;#8217;t silently miss out on a failure if &lt;tt&gt;Inventory#item_class&lt;/tt&gt; does not work as expected. However, by writing your tests this way, you focus primarily on what can be done to the object via its external interface. This leads to clearer, more maintainable tests. If a user is expected to add books through &lt;tt&gt;Inventory#receive&lt;/tt&gt;, they should not need to know about &lt;tt&gt;InStockBook&lt;/tt&gt;, so it can be regarded as an implementation detail. Changing the definition of &lt;tt&gt;Inventory#item_class&lt;/tt&gt; or even removing it entirely will not require a change to these tests as long as you maintain the signature of the objects public &lt;span class="caps"&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Now that we&amp;#8217;ve identified the approach for testing &lt;tt&gt;Inventory&lt;/tt&gt;, we are left with &lt;tt&gt;Book&lt;/tt&gt; and &lt;tt&gt;InStockBook&lt;/tt&gt; to discuss. Of the two, the problem with &lt;tt&gt;Book&lt;/tt&gt; is a little more obvious, so we&amp;#8217;ll tackle it first.&lt;/p&gt;
&lt;p&gt;Book implements a method called &lt;tt&gt;available_for_purchase?&lt;/tt&gt;, which relies on a private method called &lt;tt&gt;copies_remaining&lt;/tt&gt; to operate. The following code demonstrates a poorly implemented test.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def test_copies_remaining
  book = Book.new("Ruby Best Practices")
  Inventory.receive(book.name, 10)
 
  assert_equal book.send(:copies_remaining), 10 
end
&lt;/pre&gt;
&lt;p&gt;The reason why this is poor is because once again, we are relying on &lt;tt&gt;send&lt;/tt&gt; to call a private method in our tests. Our theory from the previous example is that private methods do not need to be tested because they don&amp;#8217;t actually implement behavior. However, &lt;tt&gt;Book#copies_remaining&lt;/tt&gt; seems like something you might want to actually make use of. If you imagine a web front-end for an e-commerce site, it&amp;#8217;s easy to visualize both an indicator of whether an item is in stock, as well as how many of that item are still available.&lt;/p&gt;
&lt;p&gt;The rule of thumb here is that if a method provides a sensible behavior that fits the context of your object, it&amp;#8217;s better off to just make it public. The following test seems very natural to me.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def test_copies_remaining
  book = Book.new("Ruby Best Practices")
  Inventory.receive(book.name, 10)
  
  assert_equal book.copies_remaining, 10 
end
&lt;/pre&gt;
&lt;p&gt;So far we&amp;#8217;ve seen two extremes: Private methods that are rightfully private and do not need to be tested explicitly, and private methods that really ought to be public so that they can be tested explicitly. We will now examine the space between these two opposite ends of the spectrum.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s think a bit about how we could test the &lt;tt&gt;InStockBook#isbn&lt;/tt&gt; shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class InStockBook

  # .. other features omitted

  def isbn
    @isbn ||= isbn_from_service
  end

end
&lt;/pre&gt;
&lt;p&gt;One way to do it the would be to mock out the call to &lt;tt&gt;isbn_from_service&lt;/tt&gt; as we do in the following tests.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def test_retreive_isbn
  book = InStockBook.new("Ruby Best Practices", 10)
  book.expects(:isbn_from_service).once.returns("978-0-596-52300-8")

  # Verify caching by calling isbn twice but expecting only one service
  # call to be made
  2.times { assert_equal "978-0-596-52300-8", @book.isbn }
end
&lt;/pre&gt;
&lt;p&gt;The downside of this approach is that by mocking out the call to &lt;tt&gt;isbn_from_service&lt;/tt&gt;, we&amp;#8217;re bypassing all of the following code, leaving it untested.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def isbn_from_service
  isbn_service_connect

  isbn = @isbn_service.find_isbn_for(@title)

  isbn_service_disconnect

  return isbn
end

def isbn_service_connect
  @isbn_service = IsbnService.new
  @isbn_service.connect
end

def isbn_service_disconnect
  @isbn_service.disconnect
end
&lt;/pre&gt;
&lt;p&gt;Making these methods public on &lt;tt&gt;InStockBook&lt;/tt&gt; doesn&amp;#8217;t make much sense, but we also can&amp;#8217;t say that these are mere implementation details that can be ignored. In these situations, typically some redesign is necessary, and in this case, a simple shift of this functionality upstream to the &lt;tt&gt;IsbnService&lt;/tt&gt; class makes the most sense.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt; 
class IsbnService

  def self.find_isbn_for(title)
    service = new

    service.connect
    isbn = service.find_isbn_for(title) # delegate to instance
    service.disconnect

    return isbn
  end

  # .. other functionality

end
&lt;/pre&gt;
&lt;p&gt;This functionality can now easily be tested as a public behavior of the &lt;tt&gt;IsbnService&lt;/tt&gt; class, where it won&amp;#8217;t get jumbled up with &lt;tt&gt;InStockBook&lt;/tt&gt;&amp;#8216;s logic. All that&amp;#8217;s left to do is rewrite our &lt;tt&gt;InStockBook#isbn&lt;/tt&gt; method so that it delegates to this new class.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class InStockBook

  # .. other features omitted

  def isbn
    @isbn ||= IsbnService.find_isbn_for(@title)
  end

end
&lt;/pre&gt;
&lt;p&gt;Our updated &lt;tt&gt;isbn&lt;/tt&gt; tests only need to change slightly to accommodate this change, as shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def test_retreive_isbn
  book = InStockBook.new("Ruby Best Practices", 10)
  IsbnService.expects(:find_isbn_for).with(book.title).once.
              returns("978-0-596-52300-8")

  # Verify caching by calling isbn twice but expecting only one service
  # call to be made
  2.times { assert_equal "978-0-596-52300-8", @book.isbn }
end
&lt;/pre&gt;
&lt;p&gt;Now, when reading the tests for &lt;tt&gt;InStockBook&lt;/tt&gt;, the developer can safely gloss over &lt;tt&gt;IsbnService&lt;/tt&gt;&amp;#8216;s implementation until its contract changes. With this dilemma solved, we&amp;#8217;ve now comprehensively categorized the strategies that allow you to avoid testing private methods without sacrificing the clarity of your test suite or its overall coverage of your implementation code.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;ve now seen examples of how to deal with all of the following situations that might tempt us to use &lt;tt&gt;send&lt;/tt&gt; in our tests unnecessarily:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;A method that does not have behavior of its own (a helper function)&lt;/li&gt;
	&lt;li&gt;A method that actually deserves to be public on the current object&lt;/li&gt;
	&lt;li&gt;A method that is only private to hide a design flaw&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Can you think of a situation where none of these approaches seem to work? Please feel free to share them in the comments section below.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 22 Mar 2011 12:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/034-issue-5-testing-antipatterns.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/034-issue-5-testing-antipatterns.html</guid></item><item><title>Issue #4: Writing Configurable Applications (2 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on November 18, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In Issue #3, we discussed the numerous downsides of mixing configuration code with application code. We then went on to discuss how using &lt;span class="caps"&gt;YAML&lt;/span&gt; files can eliminate many of those problems. But since nothing is perfect, we&amp;#8217;re back again today to discuss some of the downsides of &lt;span class="caps"&gt;YAML&lt;/span&gt; based configurations as well as some alternative approaches that have their own ups and downs. We&amp;#8217;ll also touch on some practices that should be followed regardless of how you implement your configuration systems.&lt;/p&gt;
&lt;h3&gt;Dynamic Configuration&lt;/h3&gt;
&lt;p&gt;Practicing Ruby reader Franklin Webber responded to some of the questions posed in Issue #3. In particular, he shared an example of YAML&amp;#8217;s aliasing functionality which allows you to reduce duplication in your configuration files.&lt;/p&gt;
&lt;pre&gt;
default: &amp;amp;DEFAULT
  host:
    name: testsystem
    http_port: '8080'
    username: defaultuser
  database:
    host: db01/db01
    username:
    password:
  test:
    browser: FIREFOX

windows_default: &amp;amp;WIN_DEFAULT
  &amp;lt;&amp;lt;: *DEFAULT
  test:
    browser: IE
&lt;/pre&gt;
&lt;p&gt;In this example, the &lt;tt&gt;default&lt;/tt&gt; and &lt;tt&gt;windows_default&lt;/tt&gt; configurations share almost the same attributes, except that browsers differ in test mode. Franklin uses aliasing to essentially perform a hash merge on the fly in the code above, solving his duplication problem. This is a neat way to keep your &lt;span class="caps"&gt;YAML&lt;/span&gt; configurations well organized.&lt;/p&gt;
&lt;p&gt;But after sharing this trick, Franklin shows something that can&amp;#8217;t be done directly in &lt;span class="caps"&gt;YAML&lt;/span&gt; which illustrates where the data format breaks down. While it is possible to reference different points in the data structure, it&amp;#8217;s not possible to manipulate them, so the following concatenation example cannot work without modification:&lt;/p&gt;
&lt;pre&gt;
host:
  name: localhost
  port: 3000
web:
  login_url: #{name}:#{port}/login 
&lt;/pre&gt;
&lt;p&gt;Despite how simple this all seems, it is here that we cross the line from problems solved by a data format to those solved by programming languages. Franklin suggests that it would be possible to eval the strings to reach his goals, and Rails does almost the same thing by processing its &lt;span class="caps"&gt;YAML&lt;/span&gt; files through &lt;span class="caps"&gt;ERB&lt;/span&gt;. But once we start going down that road, we need to ask ourselves what it would take to just implement the entire configuration in pure Ruby. As you can see by the code below, the answer is &amp;#8216;not much&amp;#8217;.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module MyApp
  module Config
    HOST = { :name =&amp;gt; 'localhost', :port =&amp;gt; 3000 }
    WEB  = { :login_url =&amp;gt;  "#{HOST[:name]}:#{HOST[:port]}/login" }
  end
end
&lt;/pre&gt;
&lt;p&gt;If we drop this snippet directly into our application code, we run into all the problems that our first example had in Issue #3. But by simply breaking this module out into its own file and requiring that file, those issues are avoided.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require "config/my_app_config"
require "rest_client"

module MyApp
  module Client
    extend self

    def authenticate(user, password)
      RestClient.post(MyApp::Config::WEB[:login_url], 
        :user =&amp;gt; user, :password =&amp;gt; password)
    end
  end
end

MyApp::Client.authenticate('my_user', 'seekrit')
&lt;/pre&gt;
&lt;p&gt;Using ordinary Ruby constants is no more complicated than referring to data stored in a &lt;span class="caps"&gt;YAML&lt;/span&gt; file, but gives you the full power of Ruby in your configuration scripts. In more complex configurations, you may even end up writing a mini-&lt;span class="caps"&gt;DSL&lt;/span&gt;, as shown in the &lt;tt&gt;AccessControl&lt;/tt&gt; code below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
AccessControl.configure do
  role "basic", 
    :permissions =&amp;gt; [:read_answers, :answer_questions]
  
  role "premium", 
    :parent      =&amp;gt; "basic",
    :permissions =&amp;gt; [:hide_advertisements]

  role "manager", 
    :parent      =&amp;gt; "premium",
    :permissions =&amp;gt; [:create_quizzes, :edit_quizzes]

  role "owner",
    :parent      =&amp;gt; "manager",
    :permissions =&amp;gt; [:edit_users, :deactivate_users]
end
&lt;/pre&gt;
&lt;p&gt;While this looks like vanilla configuration code on the surface, we can see that what we&amp;#8217;re actually working with are full blown Ruby objects. Here are some examples of how this system is used:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
&amp;gt;&amp;gt; AccessControl.roles_with_permission(:create_quizzes)
=&amp;gt; ["manager", "owner"]
&amp;gt;&amp;gt; AccessControl["premium"].permissions
=&amp;gt; [:hide_advertisements, :read_answers, :answer_questions]
&amp;gt;&amp;gt; AccessControl["owner"].allows?(:edit_users)
=&amp;gt; true
&amp;gt;&amp;gt; AccessControl["basic"].allows?(:edit_users)
=&amp;gt; false
&lt;/pre&gt;
&lt;p&gt;This is an advanced configuration system that not only encapsulates some configuration data, but also makes it possible to query that data in useful ways. The following implementation code illustrates how little magic is involved in building such a system.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module AccessControl
  extend self
 
  def configure(&amp;amp;block)
    instance_eval(&amp;amp;block)
  end

  def definitions
    @definitions ||= Hash.new
  end

  def role(level, options={})  
    definitions[level] = Role.new(level, options)
  end

  def roles_with_permission(permission)
    definitions.select { |k,v| v.allows?(permission) }.map { |k,_| k }
  end

  def [](level)
    definitions[level]
  end

  class Role
    def initialize(name, options)
      @name        = name
      @permissions = options[:permissions]
      @parent      = options[:parent]
    end

    attr_reader :parent

    def permissions
      return @permissions unless parent
      
      @permissions + AccessControl[parent].permissions
    end

    def allows?(permission)
      permissions.include?(permission)
    end
    
    def to_s
      @name
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;Because doing configuration in pure Ruby is so easy, I often lean towards it rather than using &lt;span class="caps"&gt;YAML&lt;/span&gt; or some other external file format. I find configuration files written in Ruby to be just as readable as &lt;span class="caps"&gt;YAML&lt;/span&gt;, but far more flexible.&lt;/p&gt;
&lt;p&gt;There are some situations in which external data formats make more sense than Ruby based configurations. Using &lt;span class="caps"&gt;YAML&lt;/span&gt; might be a better idea than the approach shown above if any of the following apply to your application:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;You need to integrate with other programs that will either read or write your configuration files. It is easier for a program written in another language to produce and consume &lt;span class="caps"&gt;YAML&lt;/span&gt; than it is for it to work with arbitrary Ruby code&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;You don&amp;#8217;t want users to be able to execute arbitrary code in your application&amp;#8217;s runtime environment. This can either be for security reasons, or for protecting users from their own stupidity by restricting the range of possible mistakes they can make.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;You want configuration data that can easily be passed over a network and then executed remotely.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While these are all good reasons to avoid Ruby based configurations, frankly they are not common scenarios. The reason Ruby has had such a widespread adoption of &lt;span class="caps"&gt;YAML&lt;/span&gt; is almost certainly not because of it being the best tool for the job, but instead due to an early design decision made in Rails that people have emulated in their own projects without further thought. While either technique may get the job done, I&amp;#8217;d argue that Ruby based configurations are a better default choice due to their inherent flexibility.&lt;/p&gt;
&lt;p&gt;But sometimes, neither Ruby nor &lt;span class="caps"&gt;YAML&lt;/span&gt; does what we need them to do. In certain situations, configuration data isn&amp;#8217;t made available until the application is invoked. For those scenarios, we can take advantage of how well Ruby is integrated with the shell by making use of environment variables.&lt;/p&gt;
&lt;h3&gt;Using the Shell Environment for Configuration&lt;/h3&gt;
&lt;p&gt;Every Ruby application has a fairly primitive but useful configuration system built into it through direct access to shell environment variables. As you can see in the code below, Ruby provides a top level constant that turns the environment variable mappings into a plain old Hash object.&lt;/p&gt;
&lt;pre&gt;
$ TURBINE_API_KEY="saf3t33553" ruby -e "puts ENV['TURBINE_API_KEY']"
IqxPfasfasasfasfgqNm
&lt;/pre&gt;
&lt;p&gt;The fact that I mention &lt;span class="caps"&gt;API&lt;/span&gt; keys in the above contrived example is no coincidence. The area I first made use of environment variables in my own applications was in a command line application which acted as a client to a web service I needed to interact with. Each distinct user needed to use a different &lt;span class="caps"&gt;API&lt;/span&gt; key, but I didn&amp;#8217;t want to rely on fragile home directory lookup code to provide per-user configuration. By using environment variables, it was possible to write a line like the following in my &lt;i&gt;.bash_profile&lt;/i&gt; which would ensure that this information was available whenever my command line program ran.&lt;/p&gt;
&lt;pre&gt;
export TURBINE_API_KEY="IqxPfasfasasfasfgqNm"
&lt;/pre&gt;
&lt;p&gt;Since most modern shell implementations support environment variables, they&amp;#8217;re a good choice for this sort of semi-global configuration data. You&amp;#8217;ll also find environment variables used in places where you don&amp;#8217;t have much control over the system where your application is destined to run. The Ruby web application deployment service Heroku is a good example of that sort of environment.&lt;/p&gt;
&lt;p&gt;On Heroku, you aren&amp;#8217;t given direct shell access and aren&amp;#8217;t even given any guarantees about where on the filesystem your application is destined to run. On top of that, if you want to run an open source application on Heroku while actively mirroring your changes to Github or some other public git host, you can&amp;#8217;t simply check in configuration files which may contain sensitive information, whether written in Ruby, &lt;span class="caps"&gt;YAML&lt;/span&gt;, or anything else.&lt;/p&gt;
&lt;p&gt;The way Heroku solves these problems is with a configuration system based on, you guessed it, environment variables. The following example from the Heroku website shows how these set via the heroku command line app.&lt;/p&gt;
&lt;pre&gt;
$ cd myapp
$ heroku config:add S3_KEY=8N029N81 S3_SECRET=9s83109d3+583493190
Adding config vars:
  S3_KEY    =&amp;gt; 8N029N81
  S3_SECRET =&amp;gt; 9s83109d3+583493190
Restarting app...done.
&lt;/pre&gt;
&lt;p&gt;In the application code, we see the variables accessed in a similar fashion to our previous example.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
AWS::S3::Base.establish_connection!(
  :access_key_id     =&amp;gt; ENV['S3_KEY'],
  :secret_access_key =&amp;gt; ENV['S3_SECRET']
)
&lt;/pre&gt;
&lt;p&gt;While hardly the first tool you should reach for, environment variables make sense in situations in which you do not want to store sensitive information within your application. They also come in handy when you don&amp;#8217;t want to assume anything about your user&amp;#8217;s file system in order to locate user-wide configuration settings.&lt;/p&gt;
&lt;p&gt;Before we wrap up with some general tips that are relevant to all configurable applications, I&amp;#8217;d like to quickly visit one more trick that involves project-wide configurations.&lt;/p&gt;
&lt;h3&gt;Per-project configurations for command line apps&lt;/h3&gt;
&lt;p&gt;Some command line applications need to be context aware in order to do their jobs. Two such examples are rake and git. Both tools know how to locate their own configuration information so that they do the right thing when running their commands.&lt;/p&gt;
&lt;p&gt;For example, git knows which repository to interact with because it knows how to work backwards to find the &lt;i&gt;.git/&lt;/i&gt; configuration folder at the project root. Likewise, running &lt;tt&gt;rake test&lt;/tt&gt; from anywhere within your project causes rake to look backwards recursively until it finds the nearest &lt;i&gt;Rakefile&lt;/i&gt; to run. This general pattern can be seen in many other applications, and is worth knowing about in case you ever need to make use of it yourself.&lt;/p&gt;
&lt;p&gt;While I don&amp;#8217;t want to go into much detail about this topic, I will say that it seemed a bit magical to me until I needed to implement this sort of functionality in my own projects. The basic idea is no more complicated than working backwards from your current directory until you find the file or folder than you need to interact with, which is somthing Ruby&amp;#8217;s pathname library can make quick work of.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s an example pulled directly out of a project of mine which illustrates a reverse search from the current working directory back to the filesystem&amp;#8217;s root directory.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require 'pathname'

def config_dir(dir = Pathname.new("."))
  app_config_dir = dir + ".myappconfigfolder"
  if dir.children.include?(app_config_dir)
    app_config_dir.expand_path
  else
    return nil if dir.expand_path.root?
    config_dir(dir.parent)
  end
end
&lt;/pre&gt;
&lt;p&gt;A bit of code like this combined with ordinary &lt;tt&gt;require&lt;/tt&gt; calls for Ruby configurations or &lt;tt&gt;&lt;span class="caps"&gt;YAML&lt;/span&gt;.load_file&lt;/tt&gt; calls for &lt;span class="caps"&gt;YAML&lt;/span&gt; configurations can be used to implement exactly the sort of context sensitive behavior you find in rake and git. I&amp;#8217;ll leave the exact methods of doing that as something for you to explore on your own, but hopefully this bit of code will come in handy if you ever run into that sort of situation.&lt;/p&gt;
&lt;p&gt;This article turned out to be longer than I expected it to be, but hopefully was still quite useful to you. Before we part, let&amp;#8217;s review a few key points to keep in mind when building any sort of configuration system.&lt;/p&gt;
&lt;h3&gt;Configuration Best Practices&lt;/h3&gt;
&lt;ul&gt;
	&lt;li&gt;Convention often is better than configuration. Always provide sensible defaults where possible. For example, if you&amp;#8217;re interacting with a service that has a common default port, don&amp;#8217;t force the user to define a port to use unless they wish to deviate from the default.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Don&amp;#8217;t put your real configuration files into your application&amp;#8217;s code repository, since this can expose sensitive data and also makes it hard for others to submit patches without merge conflicts on configuration settings.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Include a sample configuration file filled with reasonable defaults with your application. For example, in Rails, people often check in a &lt;i&gt;config/database.yml.example&lt;/i&gt; for this purpose. The goal should be to make it as easy for your user to make a copy of the sample file and then customize it as needed to get their systems up and running&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Raise an appropriate error message when a config file is missing. You can do this by doing a &lt;tt&gt;File.exist?&lt;/tt&gt; check before loading your configuration file, or by rescuing the error a failed load causes and then re-raising a more specific error that instructs the user on where to set up their configuration file.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Make it as easy as possible for users to override defaults by merging their overrides rather than forcing them to replace whole configuration structures in order to make a small change.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;What do you think of what we&amp;#8217;ve covered here? Feel free to leave your questions, comments and suggestions in the comments section below.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 17 Mar 2011 19:33:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/033-issue-4-configurable.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/033-issue-4-configurable.html</guid></item><item><title>Issue #3: Writing Configurable Applications (1 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on November 16, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Ruby developers tend to prefer convention over configuration, but that doesn&amp;#8217;t mean our applications are configuration-free.  If you&amp;#8217;re doing serious software development, it&amp;#8217;s likely that at least some of your projects depend on some sort of configuration data. Whether you simply need to store database credentials, an &lt;span class="caps"&gt;API&lt;/span&gt; key, or something much more complicated, it&amp;#8217;s important to know how to do so in a way that is flexible without introducing too much administrative overhead.&lt;/p&gt;
&lt;p&gt;In this two part article series, we&amp;#8217;ll be talking about the many options Ruby provides us for working with configuration data, and what techniques work best in various common scenarios. We&amp;#8217;ll start by showing a single example of a problem and one way to solve it, and then go on to discuss various other options in Issue #4.&lt;/p&gt;
&lt;h3&gt;Configuration Done Wrong&lt;/h3&gt;
&lt;p&gt;The worst way to work with configuration data is to embed it directly within your application. The simple Sinatra application shown below is a nice example of what &lt;strong&gt;not&lt;/strong&gt; to do.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require "rubygems"
require "sinatra"
require "active_record"

class User &amp;lt; ActiveRecord::Base; end

configure do
  ActiveRecord::Base.establish_connection(
    :adapter  =&amp;gt; "mysql",
    :host     =&amp;gt; "myhost",
    :username =&amp;gt; "myuser",
    :password =&amp;gt; "mypass",
    :database =&amp;gt; "somedatabase"
  )
end

get "/users" do
  @users = User.all
  haml :user_index
end
&lt;/pre&gt;
&lt;p&gt;The code above establishes a connection to the database on application startup and then proceeds to implement a rather simple call to get a full user listing and then render a Haml template. With an application this simple, the configuration data seems a bit harmless. But with just a moment&amp;#8217;s thought, it is easy to see numerous flaws with this sort of design.&lt;/p&gt;
&lt;p&gt;The first and most obvious issue with this sort of code is security, everyone who looks at its source needs to be trusted, as the credentials for the database connection are embedded directly within it. Now, this may or may not be a concern depending on who is involved with the project, and what other systems are in place to restrict access to production systems, but it is important to think about nonetheless.&lt;/p&gt;
&lt;p&gt;In a field in which revision control is a key part of our practices, it&amp;#8217;s not as simple as removing this sensitive information when you decide you no longer want to share it with others. Rewriting the history of a repository is straightforward on its own, but mixing application and configuration code makes it tricky to do this without jumping through a bunch of hoops. This is where the security concerns overlap with maintenance issues.&lt;/p&gt;
&lt;p&gt;Suppose you want to share this trivial sinatra application with a friend, or even use it on another machine. The in-application configuration forces everyone to set up an identical database environment, even if the needs of the application may not really call for that. Any change to this configuration information would lead to merge conflicts when you try to pull in changes across machines, which could become annoying quite fast.&lt;/p&gt;
&lt;p&gt;Fortunately, Ruby makes writing proper configuration systems easy enough where the only valid reason for writing code this way is if you&amp;#8217;re doing a throwaway spike. Let&amp;#8217;s see how easily we can emulate the way Rails solves this problem in their own framework.&lt;/p&gt;
&lt;h3&gt;&lt;span class="caps"&gt;YAML&lt;/span&gt; Based Configurations&lt;/h3&gt;
&lt;p&gt;With slight modifications, we can move our configuration out of our application and into a &lt;span class="caps"&gt;YAML&lt;/span&gt; file. We&amp;#8217;d like to end up with a database.yml file looking quite similar to a standard Rails configuration file, such as the one below:&lt;/p&gt;
&lt;pre&gt;
development:
  adapter: mysql
  database: mydatabase
  username: myuser
  password: mypass
  host: myhost
&lt;/pre&gt;
&lt;p&gt;Through the standard &lt;span class="caps"&gt;YAML&lt;/span&gt; library, we can easily access this data by parsing it into a nested hash, as shown in the irb session below.&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; require "yaml"
=&amp;gt; true
&amp;gt;&amp;gt; YAML.load_file("config/database.yml")
=&amp;gt; {"development"=&amp;gt;{"username"=&amp;gt;"myuser", "adapter"=&amp;gt;"mysql", 
   "database"=&amp;gt;"mydatabase", "host"=&amp;gt;"myhost", "password"=&amp;gt;"mypass"}}
&lt;/pre&gt;
&lt;p&gt;If we compare this output to our original example of calling &lt;tt&gt;establish_connection()&lt;/tt&gt; directly with an explicit configuration hash, the following code should be very easy to follow.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require "rubygems"
require "yaml"
require "sinatra"
require "active_record"

class User &amp;lt; ActiveRecord::Base; end

configure do
  database_config = YAML.load_file("config/database.yml")
  ActiveRecord::Base.establish_connection(database_config)
end

get "/users" do
  @users = User.all
  haml :user_index
end
&lt;/pre&gt;
&lt;p&gt;By removing the configuration data from the application code, we have made it so that the application code no longer needs to be modified everywhere it runs, provided the configuration data is properly set up. We can now safely tell our revision control system to ignore the configuration file without it causing many problems.&lt;/p&gt;
&lt;p&gt;Now that we&amp;#8217;ve seen a simple problem and a reasonable fix for it, let&amp;#8217;s ponder a few questions so that we can hit some more subtle topics in Issue #4&lt;/p&gt;
&lt;h3&gt;Questions and Discussion Points&lt;/h3&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;span class="caps"&gt;YAML&lt;/span&gt; is a nice readable data format with good Ruby support, but it can only represent data, which does not allow you to make dynamic configuration systems with it. Rails runs its &lt;span class="caps"&gt;YAML&lt;/span&gt; files through &lt;span class="caps"&gt;ERB&lt;/span&gt; to address this issue, but what other ways could this problem be solved?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;How would you handle configuration for something like a command line application which may be run anywhere on your system? How might you build per-user and per-project configuration systems?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Suppose you have a project that is mirrored to both Github and Heroku, and that you want to run directly from your public sources while providing some configuration options in your production environment. How should you handle this?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;What are some important practices to follow when implementing configuration systems, regardless of the underlying context and what approach you choose?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Please feel free to include your answers to these questions in the comments section below, along with any other thoughts or questions you might wish to share. I promise to reply personally to anyone who leaves a comment!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 10 Mar 2011 19:33:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/032-issue-3-configurable.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/032-issue-3-configurable.html</guid></item><item><title>Issue #2: Ruby's method lookup path (2 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on November 11, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h3&gt;Ruby&amp;#8217;s method lookup rules at a glance&lt;/h3&gt;
&lt;p&gt;In &lt;a href="http://blog.rubybestpractices.com/posts/gregory/030-issue-1-method-lookup.html"&gt;Issue #1&lt;/a&gt; we discussed Ruby&amp;#8217;s lookup path and proved by example that class inheritance is just a small part of the overall picture. To recap, Ruby methods are looked up in the following order:&lt;/p&gt;
&lt;p&gt;1) Methods defined in the object&amp;#8217;s singleton class (i.e. the object itself)&lt;br /&gt;
2) Modules mixed into the singleton class in reverse order of inclusion&lt;br /&gt;
3) Methods defined by the object&amp;#8217;s class&lt;br /&gt;
4) Modules included into the object&amp;#8217;s class in reverse order of inclusion&lt;br /&gt;
5) Methods defined by the object&amp;#8217;s superclass, i.e. inherited methods&lt;/p&gt;
&lt;p&gt;The example we looked at in the previous issue just showed the mechanics of how the above process plays out, it didn&amp;#8217;t really hint at practical use cases. Today, we&amp;#8217;ll look at a scenario for each of these options and discuss some of the up and downs that come along with them. Rather than presenting the examples in method lookup order, I&amp;#8217;ll try to start with the most common ones and work my way out to the more special purpose ones.&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;i&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: In a comment on issue #1, &lt;a href="http://twitter.com/jeg2"&gt;@JEG2&lt;/a&gt; correctly pointed out that this roadmap doesn&amp;#8217;t account for what happens after the whole class hierarchy is walked. Once &lt;tt&gt;BasicObject&lt;/tt&gt; is reached, Ruby starts from the bottom again calling the &lt;tt&gt;method_missing&lt;/tt&gt; hook, which is essentially an implicit step 6. I left this detail out for the sake of simplicity, but it&amp;#8217;s very important to at least be aware of.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h3&gt;When to use ordinary class definitions&lt;/h3&gt;
&lt;p&gt;The following code implements a simple timer that can write out a timestamp to a file and read it back later to determine elapsed time. Study it and consider its design.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Timer
  MissingTimestampError = Class.new(StandardError)

  def initialize(dir=Turbine::Application.config_dir)
    @file = "#{dir}/timestamp"
  end

  def write_timestamp
    File.open(@file, "w") { |f| f &amp;lt;&amp;lt; Time.now.utc.to_s }
  end
  
  def timestamp
    raise MissingTimestampError unless running?
    Time.parse(File.read(@file)).localtime
  end
  
  def elapsed_time
    (Time.now.utc - timestamp.utc) / 60.0 / 60.0
  end

  def clear_timestamp
    FileUtils.rm_f(@file)
  end

  def running?
    File.exist?(@file)
  end
end 
&lt;/pre&gt;
&lt;p&gt;When deciding if just a plain old class definition will do, I often ask myself several questions.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Is it likely is that I&amp;#8217;ll need to customize this code later for another purpose?&lt;/li&gt;
	&lt;li&gt;Is this code meant to be interacted with and extended by third party code?&lt;/li&gt;
	&lt;li&gt;Are there any common behaviors in this code I&amp;#8217;d want to extract and use elsewhere?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Because this &lt;tt&gt;Timer&lt;/tt&gt; class definition comes from a real project of mine, I can tell you that the answer to all of the above questions in the context this code is intended to be used is a simple &amp;#8216;no&amp;#8217;. What this indicates to me is that while extension might be necessary at some point down the line, there is no immediate need to design for extensibility, and so we go with the most simple option that could possibly work.&lt;/p&gt;
&lt;p&gt;Another indicator that a plain class definition might be appropriate here is the fact that most of the functionality in this class is centered around manipulating a particular bit of state, the &lt;i&gt;timestamp&lt;/i&gt; file. The problem we are trying to solve is quite a narrow one, and a single-minded class definition reflects that.&lt;/p&gt;
&lt;p&gt;The downside to designing code this way is that it does make third-party modification harder. If for example, you wanted to add some behavior around the &lt;tt&gt;timestamp()&lt;/tt&gt; method, you have three options, none of them great:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;You can create a subclass of &lt;tt&gt;Timer&lt;/tt&gt;, but your new class won&amp;#8217;t be used by the application that defined &lt;tt&gt;Timer&lt;/tt&gt; without modification.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;You can create an instance of &lt;tt&gt;Timer&lt;/tt&gt; and then add per-object behavior, but this has the same problem as subclassing.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;You can use &lt;tt&gt;alias_method&lt;/tt&gt; to create a monkeypatch to &lt;tt&gt;Timer&lt;/tt&gt;, which will inject your code into the original application, but runs risks of naming clashes and other nasty things.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While it ultimately depends on how the calling code uses this &lt;tt&gt;Timer&lt;/tt&gt; class, and what features are provided for making extensions, it&amp;#8217;s not going to be trivial to modify systems built in this fashion. But because we already determined this was a narrow bit of functionality designed to be used internally within a larger application, it isn&amp;#8217;t necessarily a problem that it isn&amp;#8217;t super extendable.&lt;/p&gt;
&lt;p&gt;Many of the rules that apply to defining your own classes also apply to inheritance based designs, so let&amp;#8217;s investigate that now.&lt;/p&gt;
&lt;h3&gt;When Inheritance Makes Sense&lt;/h3&gt;
&lt;p&gt;For those working with Rails, you already encounter class inheritance on a daily basis, through the ActiveRecord &lt;span class="caps"&gt;ORM&lt;/span&gt;. Despite the terrible choice of name, &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; is a reasonable example of when class inheritance is a decent option.&lt;/p&gt;
&lt;p&gt;Consider the typical ActiveRecord model, and how many become no more complex than the following definition:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class User &amp;lt; ActiveRecord::Base 
    has_many :comments

    validates_presence_of :name, :email
  end
&lt;/pre&gt;
&lt;p&gt;While it&amp;#8217;s true that in most interesting applications, models do become more complex, implementing intricate business logic, the amount of functionality added by the user is dwarfed by what ActiveRecord provides.&lt;/p&gt;
&lt;p&gt;One key thing to notice about a subclass of &lt;tt&gt;ActiveRecord::Base&lt;/tt&gt; is that by design, there is really no incentive to manage your own state. All state manipulation is passed upwards to the parent class to handle, which typically involves using a pre-configured database connection also managed by the parent class to persist whatever state is required.&lt;/p&gt;
&lt;p&gt;Inheritance makes sense in situations where complex state manipulations are handled by the parent class. This is especially true if the parent class provides a boat-load of functionality which dwarfs the customization needs of the child class. Since both things are true about a typical ActiveRecord model, the design is certainly a reasonable choice.&lt;/p&gt;
&lt;p&gt;However, before you start modeling your own projects after this pattern, you should take a look at the great pains that go into designing an extensible parent class. It&amp;#8217;s outside the scope of this article, but I&amp;#8217;d recommend reading what &lt;a href="http://is.gd/gW558 "&gt;Yehuda Katz has to say about ActiveModel&lt;/a&gt;, which provides the bulk of ActiveRecord&amp;#8217;s functionality under the hood.&lt;/p&gt;
&lt;p&gt;Before we move on to other topics, I&amp;#8217;d like to offer another example outside of the Rails world, just to help further illuminate the pattern.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;PDF&lt;/span&gt; generation library &lt;a href="http://prawn.majesticseacreature.com"&gt;Prawn&lt;/a&gt; provides a class that&amp;#8217;s designed to be inherited from, &lt;tt&gt;Prawn::Document&lt;/tt&gt;. I made use of this functionality recently to build a small typesetting library for formatting technical articles. While I won&amp;#8217;t go into much detail here, you can check out the &lt;a href="http://github.com/madriska/jambalaya"&gt;implementation and example code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What you&amp;#8217;ll find in &lt;a href="https://github.com/madriska/jambalaya/blob/master/lib/jambalaya.rb"&gt;lib/jambalaya.rb&lt;/a&gt; is that except for a custom factory method for generating the document, Jambalaya introduces no new state, relying on calls to &lt;tt&gt;Prawn::Document&lt;/tt&gt; to do all the heavy lifting. You can also see that &lt;a href="https://github.com/madriska/jambalaya/blob/master/example/rbp_ch1.rb"&gt;examples/rbp_ch1.rb&lt;/a&gt; gives the illusion of a totally new special purpose &lt;span class="caps"&gt;DSL&lt;/span&gt;, but that in truth, almost all the work is being done by Prawn under the hood.&lt;/p&gt;
&lt;p&gt;Unfortunately, the disadvantages of class inheritance become clear the farther away you get from these scenarios in which the subclass truly is analogous to its parent class. You get only one parent class, and chaining to it is a commitment that you must be willing to respect all the way up the hierarchy. For the scenarios we&amp;#8217;ve shown, the benefits outweigh the drawbacks, but for many others, they do not.&lt;/p&gt;
&lt;p&gt;In Issue #1, I asked the question of which techniques are special cases, and which are meant to be used commonly. While not rare by any means, inheritance falls closer to being a special case than it does to being the first tool you should reach for. If this comes as a surprise to you, it&amp;#8217;s about time for us to talk about modules.&lt;/p&gt;
&lt;h3&gt;Mixing modules into a class&lt;/h3&gt;
&lt;p&gt;If you want to see the power of mixins, you need to look no farther than Ruby&amp;#8217;s &lt;tt&gt;Enumerable&lt;/tt&gt; module. Rather than relying on a common base class to provide iterators for collections, Ruby mixes in the &lt;tt&gt;Enumerable&lt;/tt&gt; module into the structures it provides, including &lt;tt&gt;Array&lt;/tt&gt; and &lt;tt&gt;Hash&lt;/tt&gt;. This is where a whole host of useful methods come from, including &lt;tt&gt;map&lt;/tt&gt;, &lt;tt&gt;select&lt;/tt&gt;, and &lt;tt&gt;inject&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;The beauty of this design is that it imposes a much lighter contract than the rigid is-a relationship enforced by class inheritance. Instead, mixins focuses on what you can do with an object rather than what that object is. It makes perfect sense to say both &lt;tt&gt;Hash&lt;/tt&gt; and &lt;tt&gt;Array&lt;/tt&gt; objects have elements that can be enumerated over. As far as Ruby is concerned, the same can be true about any object which defines an &lt;tt&gt;each()&lt;/tt&gt; method.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at a custom Ruby class which implements each and mixes in &lt;tt&gt;Enumerable&lt;/tt&gt;. It is a simple numerical queue which is file-backed, from the same project our &lt;tt&gt;Timer&lt;/tt&gt; came from.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Queue 
  include Enumerable

  def initialize(file)
    @file = file
  end

  def entries
    return [] if empty?

    File.read(@file).split("\n").map { |e| e.to_f }
  end

  def each
    entries.each { |e| yield(e) }
  end

  # additional unrelated methods omitted
end
&lt;/pre&gt;
&lt;p&gt;The data file this queue wraps looked something similar to the data shown below.&lt;/p&gt;
&lt;pre&gt;
125.75
100.25
300.50
700
&lt;/pre&gt;
&lt;p&gt;Given a properly formatted input file, it&amp;#8217;s possible to interact with the &lt;tt&gt;Queue&lt;/tt&gt; like any other &lt;tt&gt;Enumerable&lt;/tt&gt; object.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
queue = Queue.new("queue.txt")
p queue.map { |x| "Amount: #{x}" }
p queue.inject { |x,y| x + y }
&lt;/pre&gt;
&lt;p&gt;If you go ahead and try this yourself, you&amp;#8217;ll find that it will work identically if you simply replace the first line with an array, as shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
queue = [125.75, 100.25, 300.50, 700]
p queue.map { |x| "Amount: #{x}" }
p queue.inject { |x,y| x + y }
&lt;/pre&gt;
&lt;p&gt;This simple example hints at the real beauty of &lt;tt&gt;Enumerable&lt;/tt&gt; in particular, and the mixin technique in general. In reality, my &lt;tt&gt;Queue&lt;/tt&gt; object and Ruby&amp;#8217;s &lt;tt&gt;Array&lt;/tt&gt; class have very little in common. But in the context of how you can iterate over the two objects, they can share a matching interface for the things they do have in common.&lt;/p&gt;
&lt;p&gt;This is where modules shine. They allow some of the benefits of inheritance in that they allow implementation sharing, but without the requirement of organizing things into a rigid class hierarchy. Things get even more interesting when you remember to tie your understanding of how modules work back to the way Ruby looks up methods.&lt;/p&gt;
&lt;h3&gt;Exploiting the lookup order of mixins&lt;/h3&gt;
&lt;p&gt;Methods are looked up in mixins in reverse order of their inclusion, giving the last module you mixed in a priority spot in the lookup path. A pleasant effect that arises naturally from this rule is that it provides an elegant technique for monkey patching that does not rely on method aliasing. Let&amp;#8217;s look at an example of a typical patch that uses method aliasing, and how it could be written differently.&lt;/p&gt;
&lt;p&gt;Below is the code that Rubygems uses to patch &lt;tt&gt;require&lt;/tt&gt; to add in gem loading functionality. Since &lt;tt&gt;require&lt;/tt&gt; is just a method in Ruby, and not a keyword, the patch is relatively straightforward in pure Ruby.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module Kernel
  alias gem_original_require require

  def require(path) # :doc:
    gem_original_require path
  rescue LoadError =&amp;gt; load_error
    if load_error.message.end_with?(path)
      if Gem.try_activate(path)
        return gem_original_require(path)
      end
    end

    raise load_error
  end
end 
&lt;/pre&gt;
&lt;p&gt;At the time this code was written, using method aliasing was the standard way of changing the behavior of an existing method. Aliases are used to make a copy of an existing method before modifying it, which allows customized code to delegate to the original method. This permits re-using the parts of the original method that are needed while (hopefully) preventing issues with backwards compatibility. The general approach works well, but it increases the chances that the copied methods will clash with each other as the chain gets longer, and also adds a number of superfluous methods to objects that are really just implementation details.&lt;/p&gt;
&lt;p&gt;Taking advantage of Ruby&amp;#8217;s method lookup order in modules, we can get around the issues with aliasing by writing a patch similar to the one shown below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module GemCustomRequire
  def require(path) # :doc:
    super
  rescue LoadError =&amp;gt; load_error
    if load_error.message.end_with?(path)
      if Gem.try_activate(path)
        return super
      end
    end

    raise load_error
  end   
end 

class Object
  include GemCustomRequire
end
&lt;/pre&gt;
&lt;p&gt;Because the original &lt;tt&gt;require()&lt;/tt&gt; method is defined within the &lt;tt&gt;Kernel&lt;/tt&gt; module and not on &lt;tt&gt;Object&lt;/tt&gt; itself, we can include our &lt;tt&gt;GemCustomRequire&lt;/tt&gt; module and then use &lt;tt&gt;super&lt;/tt&gt; to call the original require. The result is code that looks more natural and ordinary, reducing the amount of magic you need to know in order to understand it. It also completely avoids the possible issue of copied methods clashing with one another.&lt;/p&gt;
&lt;p&gt;This ability to do safe monkeypatching that modules affords us has been picking up steam within popular Ruby projects. Rails 3 was in a large extent designed to afford this sort of modularity, for the express purpose of making it easier for third party plugins to hook into the system in a more graceful way than method aliasing. Other projects that require a high degree of extensibility are quickly following in its footsteps, which is a good thing.&lt;/p&gt;
&lt;p&gt;While you&amp;#8217;re less likely to run into this question in application code than you are in library or framework code, knowing what mixins can gain you in terms of extensibility can really come in handy. There are tons of other good things to say about modules, but we&amp;#8217;ll need to save those for another day.&lt;/p&gt;
&lt;h3&gt;Per Object Behavior&lt;/h3&gt;
&lt;p&gt;I was originally going to go into detail about mixing modules into individual objects as well as defining singleton methods. However, I think that can be a topic all of it&amp;#8217;s own, and I want to give it a proper treatment rather than tacking it on to the end of an already lengthy newsletter.&lt;/p&gt;
&lt;p&gt;I promise we&amp;#8217;ll revisit it soon, but for those who absolutely want to explore potential uses of these techniques right away, I offer two small challenges.&lt;/p&gt;
&lt;p&gt;1) Rather than using a framework for testing stubs, experiment with something like the code below next time you&amp;#8217;re writing tests.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  obj = Object.new

  class &amp;lt;&amp;lt; obj
    def my_stubbed_method

    end
  end
&lt;/pre&gt;
&lt;p&gt;2) Rather than re-opening a class to add some extra behavior, experiment with mixing modules into individual objects to get the extra features you need.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  module MathHelpers
    def sum
      inject { |x,y| x + y }
    end

    def average
      sum.to_f / length
    end
  end

  array = [1,2,3]
  array.extend(MathHelpers)
  p array.average
&lt;/pre&gt;
&lt;p&gt;If you try these ideas out, chances are you&amp;#8217;ll find other good uses for per-object behavior as well.&lt;/p&gt;
&lt;h3&gt;Reflections&lt;/h3&gt;
&lt;p&gt;Hopefully you&amp;#8217;ve learned something new about Ruby&amp;#8217;s method lookup rules, or at least been given some new things to think about and explore. If you&amp;#8217;ve come from a background in which inheritance has been your only tool, you will likely have to retrain yourself a bit to make full use of what Ruby has to offer.&lt;/p&gt;
&lt;p&gt;Whenever you compare one of these options to the other, consider your context and how much the advantages and disadvantages of each technique affect your particular situation. The correct approach always depends on that context, and if in doubt, experiment and see what seems to fit best.&lt;/p&gt;
&lt;p&gt;More discussion on this topic is welcome in the comments section below. While I wrote this article a while ago, I am happy to jump back into the topic as long as folks have interesting ideas and questions to share.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 08 Mar 2011 17:33:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/031-issue-2-method-lookup.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/031-issue-2-method-lookup.html</guid></item><item><title>Issue #1: Ruby's method lookup path (1 of 2)</title><description>&lt;p&gt;&lt;small&gt;&lt;i&gt;Originally published as part of the Practicing Ruby newsletter on November 9, 2010. Most of these issues draw inspiration from discussions and teaching sessions at my free online school, &lt;a href="http://university.rubymendicant.com"&gt;Ruby Mendicant University&lt;/a&gt;.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;I decided to start off this newsletter with one of the most basic but essential pieces of knowledge you can have about Ruby&amp;#8217;s object model: the way it looks up methods. Let&amp;#8217;s do a little exploration by working through a few examples.&lt;/p&gt;
&lt;p&gt;Below we have a simple report class who&amp;#8217;s job is to perform some basic data manipulations and then produce some text output.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Report
  def initialize(ledger)
    @balance          = ledger.inject(0) { |sum, (k,v)| sum + v }
    @credits, @debits = ledger.partition { |k,v| v &amp;gt; 0 }
  end

  attr_reader :credits, :debits, :balance

  def formatted_output
    "Current Balance: #{balance}\n\n" +
    "Credits:\n\n#{formatted_line_items(credits)}\n\n" +
    "Debits:\n\n#{formatted_line_items(debits)}"
  end

  def formatted_line_items(items)
    items.map { |k, v| "#{k}: #{'%.2f' % v.abs}" }.join("\n")
  end
end
&lt;/pre&gt;
&lt;p&gt;The following example demonstrates how we&amp;#8217;d make use of this class.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
ledger = [ ["Deposit Check #123", 500.15],
           ["Fancy Shoes",       -200.25],
           ["Fancy Hat",          -54.40],
           ["ATM Deposit",       1200.00],
           ["Kitteh Litteh",       -5.00] ]

report = Report.new(ledger)
puts report.formatted_output
&lt;/pre&gt;
&lt;p&gt;And for those who don&amp;#8217;t want to take the time to copy and paste this code and run it locally, the actual output is shown below.&lt;/p&gt;
&lt;pre&gt;
Current Balance: 1440.5

Credits:

Deposit Check #123: 500.15
ATM Deposit: 1200.00

Debits:

Fancy Shoes: 200.25
Fancy Hat: 54.40
Kitteh Litteh: 5.00
&lt;/pre&gt;
&lt;p&gt;While not particularly pretty, this report is mostly what we&amp;#8217;d expect to see. You can probably imagine how this information might be embedded within another reports, such as an email based form letter with some header and footer information. One possible way to do this would be through class inheritance, as in the example below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require "date"

class EmailReport &amp;lt; Report
  def header
    "Dear Valued Customer,\n\n"+
    "This report shows your account activity as of #{Date.today}\n"
  end

  def banner
    "\n............................................................\n"
  end

  def formatted_output
    header + banner + super + banner + footer
  end

  def footer
    "\nWith Much Love,\nYour Faceless Banking Institution"
  end
end
&lt;/pre&gt;
&lt;p&gt;We only need to make a minor change to our calling code to make use of this new class.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
ledger = [ ["Deposit Check #123", 500.15],
           ["Fancy Shoes",       -200.25],
           ["Fancy Hat",          -54.40],
           ["ATM Deposit",       1200.00],
           ["Kitteh Litteh",       -5.00] ]

report = EmailReport.new(ledger)
puts report.formatted_output
&lt;/pre&gt;
&lt;p&gt;Below you can see what the new output ends up looking like.&lt;/p&gt;
&lt;pre&gt;
Dear Valued Customer,

The following report shows your account activity as of 2010-11-09

............................................................
Current Balance: 1440.5

Credits:

Deposit Check #123: 500.15
ATM Deposit: 1200.00

Debits:

Fancy Shoes: 200.25
Fancy Hat: 54.40
Kitteh Litteh: 5.00
............................................................

With Much Love,
Your Faceless Banking Institution
&lt;/pre&gt;
&lt;p&gt;Looking back at the &lt;tt&gt;EmailReport&lt;/tt&gt; code, it&amp;#8217;s easy to see what we&amp;#8217;ve done to produce this new output. We&amp;#8217;ve defined a new &lt;tt&gt;formatted_output&lt;/tt&gt; method which adds the headers and footers, and combined this new behavior with the original behavior of our &lt;tt&gt;Report&lt;/tt&gt; class by calling &lt;tt&gt;super&lt;/tt&gt;. This is the same extension by inheritance pattern that you&amp;#8217;ll learn in any basic computer science course or encounter in any of the reasonably traditional object oriented languages out there.&lt;/p&gt;
&lt;p&gt;But before you go asking for a refund and start telling your friends that this newsletter is painfully dull, consider this: While many languages have a method lookup path which is based on inheritance alone, that isn&amp;#8217;t even close to being true about Ruby.&lt;/p&gt;
&lt;p&gt;Because Ruby allows for module mixins and per-object behavior, the &lt;tt&gt;super&lt;/tt&gt; keyword takes on a whole new life in which an object&amp;#8217;s superclass is the last stop on a five part journey through Ruby&amp;#8217;s object model. The following example proves the point by composing a simple string which demonstrates the order in which methods are resolved in Ruby.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module W
  def foo
    "- Mixed in method defined by W\n" + super
  end
end

module X
  def foo
    "- Mixed in method defined by X\n" + super
  end
end

module Y
  def foo
    "- Mixed in method defined by Y\n" + super
  end
end

module Z
  def foo
    "- Mixed in method defined by Z\n" + super
  end
end

class A
  def foo
    "- Instance method defined by A\n"
  end
end

class B &amp;lt; A
  include W
  include X

  def foo
    "- Instance method defined by B\n" + super
  end
end

object = B.new
object.extend(Y)
object.extend(Z)

def object.foo
  "- Method defined directly on an instance of B\n" + super
end

puts object.foo
&lt;/pre&gt;
&lt;p&gt;When we run this code, we see the following output, which traces the &lt;tt&gt;super&lt;/tt&gt; calls all the way up from the method defined directly on our object to its superclass.&lt;/p&gt;
&lt;pre&gt;
- Method defined directly on an instance of B
- Mixed in method defined by Z
- Mixed in method defined by Y
- Instance method defined by B
- Mixed in method defined by X
- Mixed in method defined by W
- Instance method defined by A
&lt;/pre&gt;
&lt;p&gt;As promised, it&amp;#8217;s a five step journey. Particularly, the above is a demonstration that Ruby methods are looked up in the following order:&lt;/p&gt;
&lt;p&gt;1) Methods defined in the object&amp;#8217;s singleton class (i.e. the object itself)&lt;br /&gt;
2) Modules mixed into the singleton class in reverse order of inclusion&lt;br /&gt;
3) Methods defined by the object&amp;#8217;s class&lt;br /&gt;
4) Modules included into the object&amp;#8217;s class in reverse order of inclusion&lt;br /&gt;
5) Methods defined by the object&amp;#8217;s superclass.&lt;/p&gt;
&lt;p&gt;This process is then repeated all the way up the inheritance chain until &lt;tt&gt;BasicObject&lt;/tt&gt; is reached. Now that we know the basic order, we should stop and consider a few questions about what we&amp;#8217;ve discussed so far.&lt;/p&gt;
&lt;h3&gt;Open Questions / Things To Explore&lt;/h3&gt;
&lt;ul&gt;
	&lt;li&gt;Why would we want or need five distinct places to define methods? Do these other options really gain us anything over ordinary inheritance?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Does this change the way that classic object oriented design principles apply to Ruby? For example, how well do you think direct translations of design patterns map to Ruby?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Think of each place you can define a method in Ruby, and consider which ones are important for every day use, and which ones are edge cases. Is per-object behavior really that useful?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;It is rare to use all of these options at once, and the only reason it was done in this exercise was for demonstration purposes. But taken individually, can you think of a practical use for each way of defining Ruby methods?&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;What are some disadvantages for each technique shown here?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I will address these points and also go over some practical applications in the next issue, but please share your own thoughts in the comments section below.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 03 Mar 2011 13:52:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/030-issue-1-method-lookup.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/030-issue-1-method-lookup.html</guid></item><item><title>The Gradual Practicing Ruby Giveaway, Phase II</title><description>&lt;p&gt;It&amp;#8217;s no secret that while the Ruby Best Practices blog rocked the house in 2009, we&amp;#8217;ve pretty much been Pinin&amp;#8217; For The Fjords in recent months. As is the case with most hackers, we all simply got too busy to continue working on the &lt;span class="caps"&gt;RBP&lt;/span&gt; blog. But every cloud has a silver lining, and because one of the projects keeping me busy was a massive writing project done in private, I now have lots of materials to share with the former &lt;span class="caps"&gt;RBP&lt;/span&gt; readers!&lt;/p&gt;
&lt;p&gt;Starting today, I&amp;#8217;m going to be releasing back issues from the Practicing Ruby newsletter twice a week, on Tuesdays and Thursdays. I&amp;#8217;m hoping that this gradual posting schedule will give adequate time to have discussions about each topic, and possibly inspire wholly new content to be posted on occasion. Because that means a total of 30 posts in the next few months, we&amp;#8217;ve decided to go ahead and rename this blog the &lt;em&gt;Practicing Ruby&lt;/em&gt; blog.&lt;/p&gt;
&lt;p&gt;I look forward to interacting with the readers of this blog once again. That was always one of the best parts of working on this project, and that alone makes it worthwhile for me to share the archives of my previously commercial newsletter with you!&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;i&gt;&lt;span class="caps"&gt;LICENSE&lt;/span&gt; &lt;span class="caps"&gt;UPDATE&lt;/span&gt;: Please note that in the process of transitioning the &lt;span class="caps"&gt;RBP&lt;/span&gt; Blog to become the Practicing Ruby blog, we have changed our licensing on the content produced here. While we previously used the Creative Commons Attribution-NonCommercial-ShareAlike license, we have dropped the NC clause and are now using the Attribution-ShareAlike license. This change makes all original content on this blog as well as the Practicing Ruby issues I&amp;#8217;m releasing qualify as free cultural works. Yay!&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 03 Mar 2011 13:08:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/029-the-gradual-practicing-ruby-giveaway-2.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/029-the-gradual-practicing-ruby-giveaway-2.html</guid></item><item><title>Practicing Ruby, The Newsletter</title><description>&lt;p&gt;&lt;i&gt;Welcome, Hacker News folks.  If you find the following project worthwhile, I&amp;#8217;d really appreciate an &lt;a href="http://news.ycombinator.com/item?id=1883108"&gt;up vote&lt;/a&gt; so that I can reach a broader audience.  Please read a bit about &lt;a href="http://blog.rubybestpractices.com/posts/gregory/025-ruby-mendicant-university.html"&gt;Ruby Mendicant University&lt;/a&gt; to see what cause this goes to support.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Since the first Ruby Mendicant University session began in August, I&amp;#8217;ve interacted with and mentored roughly 50 students.  The fascinating thing is that despite the wide gradient in skill levels we see at &lt;span class="caps"&gt;RMU&lt;/span&gt;, there are many common obstacles that trip up the majority of students attending our sessions.&lt;/p&gt;
&lt;p&gt;As it turns out, there is a surprisingly wide gap between a foundational book such as David A. Black&amp;#8217;s &lt;a href="http://www.manning.com/black2/"&gt;Well Grounded Rubyist&lt;/a&gt; which is suitable for beginners and my own &lt;a href="http://rubybestpractices.com/"&gt;Ruby Best Practices&lt;/a&gt; book which is designed to help intermediate developers.  At least as far as I&amp;#8217;ve seen, this isn&amp;#8217;t a hole that has been filled in by other resources that aren&amp;#8217;t some kind of interactive training program.  My new project, the &lt;a href="http://letter.ly/practicing-ruby"&gt;Practicing Ruby newsletter&lt;/a&gt; is designed to fill in that gap.&lt;/p&gt;
&lt;p&gt;Each week, I&amp;#8217;ll choose a topic based on things I&amp;#8217;ve noticed tend to trip up intermediate Ruby developers.  Every Tuesday, some background information on the topic along with some discussion points and open questions will be sent out to the readers.  Some time will be given for open discussion on a mailing list made available to subscribers, and then a follow up article will delivered on Thursday which provides some answers to the given questions as well as some practical examples of whatever topic we are studying that week.&lt;/p&gt;
&lt;p&gt;My hope is that this will be a great use of one of the byproducts of running &lt;span class="caps"&gt;RMU&lt;/span&gt;.  Not everyone has the available free time and motivation to spend several weeks in the intense training programs we offer at &lt;span class="caps"&gt;RMU&lt;/span&gt;, and space in our sessions is severely limited.  But a wide range of Ruby developers can benefit by exploring the topics we&amp;#8217;re running into at &lt;span class="caps"&gt;RMU&lt;/span&gt;, especially if we can break them down into something that takes an hour or less a week to study.  If you&amp;#8217;re an intermediate Ruby developer who would rather do your learning in bite-size chunks, this newsletter is for you.&lt;/p&gt;
&lt;p&gt;If this sounds interesting to you, please &lt;a href="http://letter.ly/practicing-ruby"&gt;sign up for Practicing Ruby&lt;/a&gt; now.  The cost is $5 a month, but you can feel good in knowing that the better off this newsletter does, the easier it will be for me to continue dedicating time to the completely free Ruby Mendicant University.  I currently spend more than 30 hours a week on &lt;span class="caps"&gt;RMU&lt;/span&gt;, with about half of that going to student interaction, and would happily dedicate all my time to it if it were possible.  This newsletter might be a way for me to get a little extra revenue to make up for my lost consulting hours, at least until we have a proper non-profit organization in place for supporting &lt;span class="caps"&gt;RMU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;While I do not have any sample newsletters ready just yet, you can download the whole &lt;a href="http://rubybestpractices.com"&gt;&lt;span class="caps"&gt;RBP&lt;/span&gt; book&lt;/a&gt; for free to get a sense of my writing style.  I can also tell you that the first topic we&amp;#8217;ll be covering will be an exploration of Ruby&amp;#8217;s method lookup path followed up by some practical examples of how to put those ideas into action.  This will help answer some questions about when to use inheritance, mixins, and even per-object behavior.&lt;/p&gt;
&lt;p&gt;Feel free to post any questions or suggestions in the comments section below.  I&amp;#8217;m really excited about this project and would be happy to hear your thoughts!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Mon, 08 Nov 2010 16:43:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/028-practicing-ruby.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/028-practicing-ruby.html</guid></item><item><title>The Ruby Mendicant University Core Skills Course</title><description>&lt;p&gt;&lt;strong&gt;For the tl;dr crowd, there are &lt;a href="#student-projects"&gt;sixteen student projects&lt;/a&gt; and &lt;a href="#exercises"&gt;four sample exercises&lt;/a&gt; from the course towards the end of this document.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;The Original Idea&lt;/h3&gt;
&lt;p&gt;Back when I wrote about the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/025-ruby-mendicant-university.html"&gt;Ruby Mendicant University idea&lt;/a&gt; in June, I didn&amp;#8217;t have a particular plan for it.  I knew I wanted to teach a free online course of some kind, drawing from my life experiences as much as possible to form the learning materials.  Being mentored by &lt;a href="https://twitter.com/#!/jeg2"&gt;James Gray&lt;/a&gt; for the first several years of my career as a software developer really taught me the importance of having a great teacher to guide me while I worked, so I knew that mentorship would be a key part of the program.  The idea behind &lt;span class="caps"&gt;RMU&lt;/span&gt; was mainly to scale that experience from a 1-1 relationship to one that could reach many students.&lt;/p&gt;
&lt;p&gt;While it has been a wild ride so far, the results have been simply amazing.  I&amp;#8217;ll spend the next few days trying to give the Ruby community a sense of what&amp;#8217;s going on within &lt;span class="caps"&gt;RMU&lt;/span&gt;, so they can see the awesomeness of the program for themselves.  I&amp;#8217;ve decided to start by describing the core skills course, which is something every student attending &lt;span class="caps"&gt;RMU&lt;/span&gt; needs to tackle if they wish to continue on in the program.&lt;/p&gt;
&lt;h3&gt;The &lt;span class="caps"&gt;RMU&lt;/span&gt; Core Skills Course&lt;/h3&gt;
&lt;p&gt;In order to become a permanent member of the &lt;span class="caps"&gt;RMU&lt;/span&gt; community and move on to take additional courses, each student must pass an entrance exam and then go on to successfully complete their core skills course.  The entrance exam is mainly used for ensuring that the people joining the program are diligent workers who have sufficient background knowledge to do well in our core skills course.  The core skills course itself though, is something I think is something genuinely unique that you can&amp;#8217;t find anywhere outside of &lt;span class="caps"&gt;RMU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Each core course starts with roughly 15 students submitting a proposal for a personal project to work on during their session.  This can be anything Ruby related, but typically involves the student building an open source application or library that scratches a particular itch of theirs.  This is the first learning opportunity of the course, as it filters out those who can&amp;#8217;t think of anything to work on.  There isn&amp;#8217;t a list of suggested projects to pick from, students need to come up with an idea themselves.  As each student decides what they want to work on, I work with them to figure out what they can reasonably produce in three weeks which would be useful and show measurable progress.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve found that without some tangible goal to focus on, improving your skills is an abstract, disconnected experience that is only enjoyable until the going gets tough and the novelty wears off.  No &lt;span class="caps"&gt;RMU&lt;/span&gt; student can even begin their session with this mindset, which I think is a key part of why the course can inspire such rapid growth.  But many folks are only likely to volunteer to work on problems which they find sexy on the surface, which isn&amp;#8217;t a good way to master a craft.  That&amp;#8217;s where assigned work comes in.&lt;/p&gt;
&lt;p&gt;While the exercises used in each session are different, I try to include one for each of the following topics:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Integrating with other systems (typically web services)&lt;/li&gt;
	&lt;li&gt;Modeling non-trivial business logic (often a game of some sort)&lt;/li&gt;
	&lt;li&gt;Community Service (contributions to &lt;span class="caps"&gt;RMU&lt;/span&gt; and the community at large)&lt;/li&gt;
	&lt;li&gt;Classical Computer Science / Engineering Knowledge (Patterns, Algorithms, etc.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;No matter what the topic is, the end result of the exercises is some sort of real application.  Even when we&amp;#8217;re studying higher level concepts, we don&amp;#8217;t stray too far from things that you could actually use in day to day life.  The varied nature of the assignments insures that all students are challenged in one way or another, regardless of their relative level of experience entering the course.&lt;/p&gt;
&lt;p&gt;While earlier core skills sessions we ran involved some exercises that needed to be done in isolation, the current course is set up in such a way that the exercises can be actively discussed between students without it spoiling the learning experience.  This in itself is invaluable, because most of the lessons learned in &lt;span class="caps"&gt;RMU&lt;/span&gt; are not actually pre-determined by the course materials, but instead, emerge through conversations about the problems.&lt;/p&gt;
&lt;p&gt;Rather than doing lectures, I maintain extensive office hours on &lt;span class="caps"&gt;IRC&lt;/span&gt; and the session mailing list, spending 15-20 hours per week answering student questions and discusses topics as they arise through the session.  This has been extremely time consuming, so I&amp;#8217;ve started to enlist the help of some of our alumni in mentoring session students.  But this is also the area of &lt;span class="caps"&gt;RMU&lt;/span&gt; that I find most special and unique.  I spend three days a week in constant conversation with my students, which means that I learn an incredible amount about each of them and can tailor the program to meet their individual needs and interests.&lt;/p&gt;
&lt;p&gt;Students can submit their assigned work or individual projects for review at any point in time during the three week program.  I approve submissions which don&amp;#8217;t show any obvious gaps in understanding the key points, the others, I provide feedback on and ask them to resubmit when they&amp;#8217;ve made revisions.  Students who make good progress at the end of the course by getting at least three of four assigned projects approved, and by doing good work on their individual project, gain alumni status and are invited to stay involved with &lt;span class="caps"&gt;RMU&lt;/span&gt; for the long haul.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll have to save the alumni responsibilities and benefits for another post.  But it&amp;#8217;s worth noting that so far, 16 of 25 students who have completed a core skills course gained alumni status.  While I&amp;#8217;d like to see that number higher, several of the students who didn&amp;#8217;t successfully complete their session are working with me to find a way to get their status later.  But the great thing is, 100% of our alumni intend to take more courses at &lt;span class="caps"&gt;RMU&lt;/span&gt; in the near future!&lt;/p&gt;
&lt;h3&gt;&lt;a name="student-projects"&gt;Student Projects from August / September&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Below are the sixteen individual projects of our current &lt;span class="caps"&gt;RMU&lt;/span&gt; alumni from our August and September sessions. While many of them are still in their early stages of development, several are quite useful already.  Keep in mind, most of these projects were built in just three weeks!&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://github.com/RubyMoney/google_currency"&gt;google_currency&lt;/a&gt; (Shane Emmons): Ruby Money::Bank interface for the Google Currency exchange data&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/fwoeck/cashmate"&gt;cashmate&lt;/a&gt; (Frank Wöckener): Business proposal management system&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/chipiga/forum_engine"&gt;forum_engine&lt;/a&gt; (Pavel Chipiga): Forum engine built on Rails 3&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/justinramel/groupy"&gt;groupy&lt;/a&gt; (Justin Ramel) ActiveDirectory Group Management Utility&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/soulim/prism"&gt;prism&lt;/a&gt; (Alex Soulim): WebSocket Server for chat&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/ruanwz/gstore"&gt;gstore&lt;/a&gt; (David Ruan): Ruby client library for the Google Storage &lt;span class="caps"&gt;API&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://groups.google.com/group/prawn-ruby/browse_thread/thread/e2c3ac97065db4ca/2fd7a9b4ba60be6d?lnk=gst&amp;amp;q=examples#2fd7a9b4ba60be6d"&gt;Prawn Reference Guide / Examples Refactoring&lt;/a&gt; (Felipe Doria) Soon to be merged upstream&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/lucasefe/themes_for_rails"&gt;themes_for_rails&lt;/a&gt; (Lucas Florio): Theme support for Rails 3&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/morganp/unwatched"&gt;unwatched&lt;/a&gt; (Morgan Prior): Utility for tracking watched media files&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/wpiekutowski/montgomery"&gt;montogomery&lt;/a&gt; (Wojciech Piekutowski): A lightweight object mapper for MongoDB&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/madebydna/vegtastic"&gt;vegtastic&lt;/a&gt; (Andrea Singh): Collaborative vegan/vegetarian recipe collection app&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/jazzu/resourceful"&gt;resourceful&lt;/a&gt; (Jaakko Vallo): Automated work schedule management.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/ignacy/qinfo"&gt;qinfo&lt;/a&gt; (Ignacy Moryc): &lt;span class="caps"&gt;SQL&lt;/span&gt; Query optimization tool&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/tehviking/minivid"&gt;minivid&lt;/a&gt; (Brandon Hays): 60 second video uploading and sharing service&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/afcapel/rallot"&gt;rallot&lt;/a&gt; (Alberto Fernández-Capel): Secure electronic voting system backend&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/ericgj/rmu-alarmclock"&gt;rmu-alarmclock&lt;/a&gt; (Eric Gjertsen): Client/server based reminder system&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While I gave students guidance on their projects throughout the course, my focus was on education, and not on quality assurance, so please do not consider this listing of projects a personal endorsement that they&amp;#8217;ll be suitable for a particular purpose.&lt;/p&gt;
&lt;h3&gt;&lt;a name="exercises"&gt;Released problems from August / September sessions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;If you&amp;#8217;re curious about the sorts of problems students worked on during their sessions, you can check out the few that have been released so far.  Eventually all of our materials will be released, and collected nicely in an easy to find location.  For now, feel free to re-distribute these under the terms of the &lt;span class="caps"&gt;GNU&lt;/span&gt; Free Documentation License.&lt;/p&gt;
&lt;p&gt;You can try these on your own, or look through the network graphs to see some student submissions that have been made public.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://github.com/rmu/s0-e1"&gt;s0-e1&lt;/a&gt; : Prototyping the board game Dominion&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/rmu/s0-e2"&gt;s0-e2&lt;/a&gt; : &lt;span class="caps"&gt;IRC&lt;/span&gt; bot / Web Service mashup&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/rmu/s1-e1"&gt;s1-e1&lt;/a&gt; : Twitter bot / Web Service mashup&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/rmu/s1-e2"&gt;s1-e2&lt;/a&gt; : Prototyping a Github themed achievement system&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lots of other exercises will be released soon, so stay tuned.&lt;/p&gt;
&lt;h3&gt;Please help us with funding!&lt;/h3&gt;
&lt;p&gt;&lt;a href='http://www.pledgie.com/campaigns/13580'&gt;&lt;img alt='Click here to lend your support to: Help Ruby Mendicant University "Grow up" and make a donation at www.pledgie.com !' src='http://www.pledgie.com/campaigns/13580.png?skin_name=chrome' border='0' /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you might imagine based on this article, running &lt;span class="caps"&gt;RMU&lt;/span&gt; is an incredibly labor intensive task.  What started as something I thought I&amp;#8217;d do as a side project after the initial setup work has gone in the opposite direction.  I now spend 30-35 hours per week on &lt;span class="caps"&gt;RMU&lt;/span&gt; related responsibilities, about half of that time is spent interacting with students.  My coworker Jordan Byron spends at least two days a week working on &lt;span class="caps"&gt;RMU&lt;/span&gt;, building out our web application for course management, and my wife Jia spends several hours each week dealing with administrative tasks.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll write more about what goes into running &lt;span class="caps"&gt;RMU&lt;/span&gt; soon, but for now, there is a pledgie open that is meant to bridge us over until we get a proper non-profit organization with a decent revenue model set up.  Your contributions would be greatly appreciated, so &lt;a href="http://pledgie.com/campaigns/13580"&gt;please donate now&lt;/a&gt;, and tell your friends as well.&lt;/p&gt;
&lt;h3&gt;Questions / Thoughts About &lt;span class="caps"&gt;RMU&lt;/span&gt;?&lt;/h3&gt;
&lt;p&gt;We are not accepting new students now, but you are still welcome to discuss the program with us either via irc.freenode.net in the #rmu-public channel, or via the &lt;a href="http://groups.google.com/group/ruby-mendicant-university----public"&gt;&lt;span class="caps"&gt;RMU&lt;/span&gt; Public&lt;/a&gt; google group. We are happy to hear your thoughts and questions!  Also, feel free to leave questions or comments here on this blog post, I&amp;#8217;ll be sure to read them and respond quickly if there is anything on your mind.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 21 Oct 2010 17:35:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/027-ruby-mendicant-university-update.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/027-ruby-mendicant-university-update.html</guid></item><item><title>Ruby Mendicant University is a Go!</title><description>&lt;blockquote&gt;
&lt;p&gt;Born less than 24 hours ago, my &lt;a href="http://blog.rubybestpractices.com/posts/gregory/025-ruby-mendicant-university.html"&gt;Ruby Mendicant University idea&lt;/a&gt; is already more than a third of the way to having its &lt;a href="http://pledgie.com/campaigns/11063"&gt;initial costs  covered.&lt;/a&gt; In less than 12 hours, we&amp;#8217;ve had 35 people &lt;a href="http://spreadsheets.google.com/viewform?formkey=dHlLM2E1ZllqN2hSWWhIWFBXRmthN2c6MQ"&gt;request a copy of the entrance exam.&lt;/a&gt; So now that the idea is validated, it&amp;#8217;s time to begin on the execution.&lt;/blockquote&gt;&lt;/p&gt;
&lt;p&gt;You can keep up on the latest news by following the &lt;a href="http://seacreature.posterous.com/tag/rubymendicant"&gt;rubymendicant&lt;/a&gt; tag at &lt;a href="http://seacreature.posterous.com"&gt;seacreature.posterous.com&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;i&gt;PS: This morning, I decided that &lt;a href="http://seacreature.posterous.com/19694115"&gt;I want to consolidate my blogging and tweeting,&lt;/a&gt; so most of my content will be posted to my new posterous blog rather than here at &lt;span class="caps"&gt;RBP&lt;/span&gt; blog.   Also, I am moving my twitter account over to &lt;a href="http://twitter.seacreature"&gt;@seacreature&lt;/a&gt; exclusively, rather than using &lt;a href="http://twitter.com/rubypractices"&gt;@rubypractices.&lt;/a&gt;   While this isn&amp;#8217;t quite an announcement of ending the &lt;span class="caps"&gt;RBP&lt;/span&gt; Blog project, it&amp;#8217;s a step in that direction.  Thanks so much for your attention and feedback over the last year or so.  Hope to see you on the other side!&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 04 Jun 2010 14:50:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/026-ruby-mendicant-university-is-a-go.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/026-ruby-mendicant-university-is-a-go.html</guid></item><item><title>IDEA: Ruby Mendicant University</title><description>&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;:  You can &lt;a href="http://pledgie.com/campaigns/11063"&gt;donate now at Pledgie&lt;/a&gt; / I know this is early, but in the spirit of Ruby Mendicant, I&amp;#8217;m looking for those trusting individuals who will give with altruistic intentions first, I&amp;#8217;ll spend my time convincing everyone else later :)&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2: More details to come, but those who wish to receive a copy of the entrance exam should &lt;a href="http://is.gd/cBNH9"&gt;sign up via this google form.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Back in 2008, the highlight of my year was the &lt;a href="http://majesticseacreature.com/mendicant/"&gt;Ruby Mendicant&lt;/a&gt; project.  Through some stroke of luck, I ended up getting enough funding from independent community donations to take 22 weeks off of work to focus on building &lt;a href="http://prawn.majesticseacreature.com"&gt;Prawn&lt;/a&gt;, a pure ruby &lt;span class="caps"&gt;PDF&lt;/span&gt; generation library that is now nearing its 1.0 release.&lt;/p&gt;
&lt;p&gt;Now in 2010, I&amp;#8217;d like to do the same thing for Ruby training and education.  Similar to my &lt;a href="http://www.oreillynet.com/ruby/blog/2008/03/id_love_to_quit_my_job_sort_of.html"&gt;original post that spawned Ruby Mendicant&lt;/a&gt;, the details are currently fuzzy in my head, but I have enough of a rough outline to collect some comments on.&lt;/p&gt;
&lt;h3&gt;The Idea&lt;/h3&gt;
&lt;p&gt;I want to run a free online school designed to help intermediate Ruby programmers master their craft.  Each session would include up to 20 or so participants and run for 3 weeks.&lt;/p&gt;
&lt;p&gt;The basic activities would include:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;an entrance exam (basic coding problem and questionnaire)&lt;/li&gt;
	&lt;li&gt;selected readings in freely available Ruby books and online resources&lt;/li&gt;
	&lt;li&gt;individual and group code review&lt;/li&gt;
	&lt;li&gt;realistic problems to work on and discuss&lt;/li&gt;
	&lt;li&gt;Q&amp;amp;A sessions&lt;/li&gt;
	&lt;li&gt;a final exam (which if passed, would earn the student &amp;#8216;alumni&amp;#8217; status)&lt;/li&gt;
	&lt;li&gt;possibly some pre-recorded or live presentations&lt;/li&gt;
	&lt;li&gt;a mailing list for students to discuss questions with me&lt;/li&gt;
	&lt;li&gt;online discussions with guest speakers from our community.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The goal would be to keep these classes going on a rolling basis, so that we could reach a large number of students in the course of a year.  If we did a schedule like 3 weeks on, 1 week off, we could do 13 sessions in a year, allowing me to reach up to 260 students.&lt;/p&gt;
&lt;p&gt;Of the available slots, I would also like to reserve a certain amount of them for &amp;#8220;hardship&amp;#8221; situations.  People who are currently out of work, those who are in tough economic climates, or have some sort of difficulty in their lives that this opportunity could really help them overcome.  I have not yet figured out how to do this without introducing a ton of bias in my selections, and I have no way of verifying these details, but it seems like a good idea in theory to me at least.&lt;/p&gt;
&lt;p&gt;At least some of the sessions would be open to the community at large, and at least some of the material would be released under an open documentation license.  This way, while the core focus would be on affecting big change for small groups at a time, the whole community could benefit from the existence of this program as well.&lt;/p&gt;
&lt;h3&gt;Funding&lt;/h3&gt;
&lt;p&gt;The up front cost of building the material is large, enough where I would need to take a couple days off a week from consulting work for a period of 8 weeks or so to come up with adequate material.  While the replacement cost for that work is pretty high (over 6k), I could probably still get by well enough if I had about $3000 in donations to produce the initial material.&lt;/p&gt;
&lt;p&gt;The promising thing about the above lesson plan is that since a lot of it is interactive and tailored to individual efforts, once I build the core lesson plan, actually running the &amp;#8220;school&amp;#8221; will be comparatively less work.  I won&amp;#8217;t know at all until I actually run it, but I would expect that I could keep the school up and running for about $1500 a month.&lt;/p&gt;
&lt;p&gt;But in the Mendicant spirit, I only would want to keep this thing going if it was of value to the community.  So rather than looking for donations for the running of the school up front, I would only raise money to cover the initial costs of content creation.&lt;/p&gt;
&lt;p&gt;Whether the school lives or dies, grows or shrinks, would be based on the generosity of previous students as well as that of the community at large.&lt;/p&gt;
&lt;h3&gt;Schedule&lt;/h3&gt;
&lt;p&gt;If I could raise enough money by the end of June, I could then shift my schedule around in July and August to allow me to work on content creation.  That would let me start the first class around the first week of September.&lt;/p&gt;
&lt;h3&gt;Why Me?&lt;/h3&gt;
&lt;p&gt;Besides the &lt;a href="http://http://majesticseacreature.com/mendicant/"&gt;Ruby Mendicant&lt;/a&gt; project, I have also taught trainings at the Lone Star Ruby Conference (2008,2009), and am also part of the trio hosting &lt;a href="http://thecompleatrubyist.com/"&gt;The Compleat Rubyist&lt;/a&gt;.  I have written two books that are now completely free for people to download and read.  And other stuff.&lt;/p&gt;
&lt;p&gt;Trust me.  I&amp;#8217;d be good at this :)&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re wondering what&amp;#8217;s in it for me:  I believe that free education is one of the most valuable things in the world, and I also learn a ton by teaching others.  If I can make enough money to offset my costs and maintain a decent house-and-wife life, I feel like this is a super good use of my time.&lt;/p&gt;
&lt;h3&gt;What do you think?&lt;/h3&gt;
&lt;p&gt;Right now, feedback is key.  I only want to take this project on if I think it&amp;#8217;ll be a sure thing.   Please respond, answering these questions:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Would you want to attend this online school?&lt;/li&gt;
	&lt;li&gt;Would you be willing to donate to help with its creation?&lt;/li&gt;
	&lt;li&gt;Would you recommend this program to others?&lt;/li&gt;
	&lt;li&gt;What can I do to make it even more awesome?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thanks in advance for your suggestions.  Here&amp;#8217;s hoping that we can make this work :)&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;:  You can &lt;a href="http://pledgie.com/campaigns/11063"&gt;donate now at Pledgie&lt;/a&gt; / I know this is early, but in the spirit of Ruby Mendicant, I&amp;#8217;m looking for those trusting individuals who will give with altruistic intentions first, I&amp;#8217;ll spend my time convincing everyone else later :)&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2: More details to come, but those who wish to receive a copy of the entrance exam should &lt;a href="http://is.gd/cBNH9"&gt;sign up via this google form.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 03 Jun 2010 14:50:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/025-ruby-mendicant-university.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/025-ruby-mendicant-university.html</guid></item><item><title>If you want to see the wizard, go to Oz.</title><description>&lt;p&gt;Back in June of 2005, I booked a flight out to Oklahoma City, OK, but it might as well have been to Oz.  I was off to see the wizard who had been teaching me black magic in both Perl and Ruby throughout my high school days.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.grayproductions.net"&gt;James Edward Gray II&lt;/a&gt; and I had somehow convinced Ruby Central, Inc. that a &lt;a href="http://rubyforge.org/projects/gambit"&gt;web game framework&lt;/a&gt; was worth a Codefest grant, and we used the money as a chance to finally meet each other in person.  At the time, we decided to build things from scratch, since Rails 0.12 didn&amp;#8217;t quite have the features we needed :)&lt;/p&gt;
&lt;p&gt;Not counting the friends I talked into studying programming here and there, this was my first time ever working side by side with another hacker.  I didn&amp;#8217;t know what to expect, but by the time I left, I couldn&amp;#8217;t believe that what we had done was possible.  Safe to say, JEG2 blew my mind, in the way that any good wizard can.   Here&amp;#8217;s what I wrote just after returning home in June 2005:&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;br /&gt;
The last nine days were amazing. Exploited undocumented features of WEBRick.&lt;br /&gt;
Making use of circular logic flows, making code do things that it wasn&amp;#8217;t built&lt;br /&gt;
to do. Pushing the limits, building truly clever tools and libraries that will&lt;br /&gt;
make people&amp;#8217;s lives easier, and more fun. &lt;br /&gt;
&lt;br /&gt;
I myself feel like I&amp;#8217;m standing a little taller after all the hard work we&lt;br /&gt;
did. That was hacking in it&amp;#8217;s purest nature, and though I don&amp;#8217;t like to self&lt;br /&gt;
attribute that word, there is nothing else to describe it. Ideas flowed any &lt;br /&gt;
hour of the day, even in our dreams. No one tried to take charge, I didn&amp;#8217;t get &lt;br /&gt;
mad when James replaced bits of my code with something better, and James &lt;br /&gt;
didn&amp;#8217;t get mad when I questioned his logic and pushed him to raise the bar. In &lt;br /&gt;
the rare occasion that I proved James wrong on something, he&amp;#8217;d just shrug his &lt;br /&gt;
shoulders, embrace The Right Way, and go with it.&lt;br /&gt;
&lt;br /&gt;
We joked around a lot referring to certain programming practices as &amp;#8220;The way &lt;br /&gt;
of life&amp;#8221;. Though that is quite silly when it applies to 80 character linewrap &lt;br /&gt;
or a certain tab width, it was something that motivated us. We could look at &lt;br /&gt;
our code, and at a glance see if it was following &amp;#8220;The way&amp;#8221; or not. When it &lt;br /&gt;
wasn&amp;#8217;t, we waited for something better to come along, and then we did what we &lt;br /&gt;
had to do to make it happen.&lt;br /&gt;
&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, I was just a kid at the time, but I knew something important had just happened.  Most importantly I realized that the kind of interaction you can have over the internet, though amazing, pales in comparison to face-to-face interaction.  Here&amp;#8217;s what James had to say shortly after our first meeting:&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;br /&gt;
&amp;#8220;We&amp;#8217;ve just completed nine solid days of development on Gambit. Now&lt;br /&gt;
that&amp;#8217;s a &amp;#8220;Codefest&amp;#8221; alright! I thought I would share a little with&lt;br /&gt;
the curious about how I think it went&amp;#8230;&lt;br /&gt;
&lt;br /&gt;
Greg Brown flew out to my home in Oklahoma so we could work on&lt;br /&gt;
Gambit. It was nice to finally meet face-to-face. We&amp;#8217;ve actually&lt;br /&gt;
programmed together on many projects these last couple of years, but&lt;br /&gt;
this was the first time we didn&amp;#8217;t do it over the Internet. We&amp;#8217;re&lt;br /&gt;
both grateful to the wonderful people at Ruby Central for giving us&lt;br /&gt;
that opportunity.&lt;br /&gt;
&lt;br /&gt;
We&amp;#8217;ve had practice, so we work pretty well together by now. Greg&lt;br /&gt;
would probably say that I&amp;#8217;ve almost beat all his bad habits out of&lt;br /&gt;
him. ;) True or not, I know he&amp;#8217;s always challenging me and pushing&lt;br /&gt;
me to new limits of what I can build. I think we both learned a lot&lt;br /&gt;
from working together on this project.&lt;br /&gt;
&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;While there is a certain amount of altruism involved in mentoring a novice, it is an act that pays you back tenfold.  Every concept that you can teach another person is one that you must master through and through, in ways that you&amp;#8217;d never even scratch the surface of on your own.&lt;/p&gt;
&lt;p&gt;Whether he realizes it or not, James revealed a secret to me during that hackfest, one that is to blame for all of my future successes in programming: &lt;i&gt;Share your experiences and knowledge with others, and you win.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Fast forward to 2010, and obviously JEG2 and I have come a long way.  We&amp;#8217;ve both done talks throughout the US, and James even made it over to Japan last year.  We&amp;#8217;ve both published books on Ruby, and made more friends in the community than we ever imagined possible back in 2005.  While part of me feels indebted to James still, I feel like I&amp;#8217;ve paid it forward through my works.  That made our mentorship role turn into a friendly rivalry, where we both keep pushing each other to get better at what we do, even if indirectly.&lt;/p&gt;
&lt;p&gt;James had a big head start, so he normally beats me to the punch on things.  Up until recently, I had only one thing under my belt that JEG2 didn&amp;#8217;t: I had &lt;a href="http://2007.goruco.com/volunteers/"&gt;helped design and run a Ruby conference&lt;/a&gt;. Not one to be outdone, JEG2 has decided to level that playing field by running &lt;a href="http://reddirtrubyconf.com"&gt;Red Dirt RubyConf&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Red Dirt RubyConf: Your ticket to Oz&lt;/h3&gt;
&lt;p&gt;If you like what I&amp;#8217;ve been doing in Ruby, you now know that I owe it all to JEG2.  Here&amp;#8217;s the good news:  He&amp;#8217;s now inviting all of you to his neck of the woods to spend some time hacking, learning, and sharing.  &lt;b&gt;But you need to act fast, &lt;a href="http://reddirtrubyconf.com/register_to_attend"&gt;registration&lt;/a&gt; closes Friday, April 23!&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Red Dirt RubyConf is a unique two day conference which blends training in with a uniquely themed single track session lineup.  In it, you won&amp;#8217;t just get to meet James, but also some other amazing Ruby people as well, including Dave Thomas and Jim Weirich.&lt;/p&gt;
&lt;p&gt;The conference is run by James, his lovely wife Dana (who I could write a whole other post about), and some other great folks from the Oklahoma Ruby group.  If you head out there, I can promise you that you&amp;#8217;ll have an awesome time, learn a ton, and meet some amazing people.&lt;/p&gt;
&lt;p&gt;Unfortunately, I won&amp;#8217;t be able to attend, but I&amp;#8217;ll sure be there in spirit.  If you do make it there, tell James I said hi, and have a great time!&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;DISCLOSURE&lt;/span&gt;: I told James I&amp;#8217;d be writing something to promote Red Dirt, but he didn&amp;#8217;t ask me to do this, and he had no idea what I&amp;#8217;d come up with.  So I take 100% responsibility for the contents of this article.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 21 Apr 2010 14:50:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/024-wizard-of-oz.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/024-wizard-of-oz.html</guid></item><item><title>rubyproblems.com is now live!</title><description>&lt;p&gt;Back on St. Patrick&amp;#8217;s day, I had made the following plug about rubyproblems.com at the bottom of my last post:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This site will provide realistic exercises that test your Ruby and general coding skills, along with detailed tutorials written in the style of &lt;span class="caps"&gt;RBP&lt;/span&gt; explaining possible solutions to the problems. The exercises will be free and open to everyone, the solutions we’ll be selling as nicely typeset, professionally edited, &lt;span class="caps"&gt;DRM&lt;/span&gt;-free &lt;span class="caps"&gt;PDF&lt;/span&gt; download&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I am pleased to announce that this service did indeed launch yesterday, and that you can give it a try by heading over to &lt;a href="http://rubyproblems.com"&gt;rubyproblems.com&lt;/a&gt; now.  If you need a bit more information, consider checking out &lt;a href="http://rubybestpractices.com/rubyproblems-teaser.swf"&gt;this screencast&lt;/a&gt; or &lt;a href="http://rubyproblems.blogspot.com/2010/04/were-live.html"&gt;this announcement&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While this is still a bit of an experiment for us, we&amp;#8217;re really excited to see how things turn out.  Those who liked my book should love this site, which is why I&amp;#8217;m announcing it here.&lt;/p&gt;
&lt;p&gt;But now, the &lt;span class="caps"&gt;RBP&lt;/span&gt; blog should return to its non-marketing-hype self.  In fact, I really want to find the time to write about why I think this is the most horrible syntax ever:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
describe Array do
  its(:length) { should == 0 }
end
&lt;/pre&gt;
&lt;p&gt;But I suppose that bit of flame bait will need to wait until a later day :)&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 07 Apr 2010 14:50:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/023-rubyproblems.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/023-rubyproblems.html</guid></item><item><title>Full Book Now Available For Free!</title><description>&lt;p&gt;The last few weeks have been a wild ride.  Starting in January, I&amp;#8217;ve been releasing a chapter at a time here on the &lt;span class="caps"&gt;RBP&lt;/span&gt; blog, and many of those chapters were pretty well commented on.  Just for the sake of completeness, here&amp;#8217;s a link back to each of those posts:&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;Chapter 1: Driving Code Through Tests&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;Chapter 2: Designing Beautiful &lt;span class="caps"&gt;APIS&lt;/span&gt; / Chapter 3: Mastering the Dynamic Toolkit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/018-rbp-ch4.html"&gt;Chapter 4: Text Processing and File Management&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/019-rbp-ch5.html"&gt;Chapter 5: Functional Programming Techniques&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/020-rbp-ch6.html"&gt;Chapter 6: When Things Go Wrong&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/021-rbp-ch7.html"&gt;Chapter 7: Reducing Cultural Barriers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At this point, I&amp;#8217;d like to invite you to just &lt;a href="http://sandal.github.com/rbp-book/pdfs/rbp_1-0.pdf"&gt;grab the whole thing&lt;/a&gt; and read it at your leisure.  This includes the final chapter &amp;#8220;Skillful Project Maintenance&amp;#8221;, as well as the three appendixes: &amp;#8220;Writing Backwards Compatible Code&amp;#8221;, &amp;#8220;Leveraging Ruby&amp;#8217;s Standard Library&amp;#8221;, and &amp;#8220;Ruby Worst Practices&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Since the book is now open source, if you find something you don&amp;#8217;t like about it, it&amp;#8217;s your responsibility to help make it better!  But if you do like it as it is, it might be more pleasant to read in print or on your Kindle, so you might want to pick it up from &lt;a href="http://oreilly.com/catalog/9780596523015/"&gt;O&amp;#8217;Reilly&lt;/a&gt; or &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;Amazon&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Manuscript and Production Files&lt;/h3&gt;
&lt;p&gt;You can find the details about the &lt;a href="http://github.com/sandal/rbp-book"&gt;manuscript and production files on github&lt;/a&gt;, now released under a Creative Commons license.  It might be a little while before I can put some time into making this contributor friendly, but those looking to experiment or explore a bit are certainly welcome.  The manuscript is written using the &lt;a href="http://www.methods.co.nz/asciidoc/"&gt;amazing asciidoc toolchain&lt;/a&gt;, which means the plaintext files you see in the git repository actually are what I used to generate something very close to the final print version of &lt;span class="caps"&gt;RBP&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;So that&amp;#8217;s pretty much it.  While this will soon mark a new beginning, this figuratively and literally marks the last chapter in the history of &lt;span class="caps"&gt;RBP&lt;/span&gt; being &amp;#8220;my book&amp;#8221;.  It&amp;#8217;s yours now, do what you want with it.  If you have interesting ideas that you want to run by me, I&amp;#8217;d be happy to help however I can.  Otherwise, enjoy and have fun!&lt;/p&gt;
&lt;h3&gt;But I finished the &lt;span class="caps"&gt;RBP&lt;/span&gt; book and I want more!  What should I do?&lt;/h3&gt;
&lt;p&gt;Okay, you got me.  I&amp;#8217;ve been trying to take the &amp;#8220;spirit&amp;#8221; of &lt;span class="caps"&gt;RBP&lt;/span&gt; and apply it to two new projects, both a little more interactive in nature.  For those who enjoyed the book, you may be interested in these two resources, both on the horizon.&lt;/p&gt;
&lt;p&gt;The first is &lt;a href="http://rubyproblems.com"&gt;rubyproblems.com&lt;/a&gt;.  This site will provide realistic exercises that test your Ruby and general coding skills, along with detailed tutorials written in the style of &lt;span class="caps"&gt;RBP&lt;/span&gt; explaining possible solutions to the problems.  The exercises will be free and open to everyone, the solutions we&amp;#8217;ll be selling as nicely typeset, professionally edited, &lt;span class="caps"&gt;DRM&lt;/span&gt;-free &lt;span class="caps"&gt;PDF&lt;/span&gt; downloads.  We launch April 6th, 2010, but if you &lt;a href="http://rubyproblems.com/login"&gt;register now&lt;/a&gt;, you&amp;#8217;ll get a free solution download when the site goes live.  Whether you plan on buying content or not, the exercises themselves should provide great learning opportunities.  If you liked &lt;span class="caps"&gt;RBP&lt;/span&gt;, you&amp;#8217;ll definitely like Ruby Problems.&lt;/p&gt;
&lt;p&gt;The second chance to bolster what you&amp;#8217;ve learned in &lt;span class="caps"&gt;RBP&lt;/span&gt; is to attend a &lt;a href="http://thecompleatrubyist.com/"&gt;Compleat Rubyist&lt;/a&gt; training event.  This is a joint venture between David A. Black, Jeremy McAnally, and myself that&amp;#8217;s designed to provide a crash course in going from &amp;#8220;knowing Ruby&amp;#8221; to &amp;#8220;being awesome at Ruby&amp;#8221;.  We did one of these in Tampa, FL in January, and it was blast.  Our next one is scheduled for the Chicago area on June 18-19.  We plan to do this as a regular thing, so if you can&amp;#8217;t make it out to the Midwest, let us know where we should be going to find you!  If you want a little more detail, you can check out &lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/359006"&gt;my RubyTalk announcement about the upcoming event&lt;/a&gt;, and perhaps take a look at the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/014-the-compleat-rubyist.html"&gt;Tampa retrospective&lt;/a&gt; here on the &lt;span class="caps"&gt;RBP&lt;/span&gt; blog.&lt;/p&gt;
&lt;h3&gt;Disclaimer&lt;/h3&gt;
&lt;p&gt;While I typically hate direct marketing, I am absolutely excited about the above two things, and so I felt compelled to mention them as good &amp;#8216;next steps&amp;#8217; as you go beyond &lt;span class="caps"&gt;RBP&lt;/span&gt;.  Doing these as commercial projects won&amp;#8217;t ever make me rich, but it does allow me to take time away from billable work and really focus on producing awesome content and engaging with those who could learn something from it.  So just know that whenever you send a bit of cash my way, you&amp;#8217;re supporting a fellow hacker who will keep giving back at every opportunity.&lt;/p&gt;
&lt;p&gt;But for those a bit weary of marketing gimmicks, the &lt;span class="caps"&gt;RBP&lt;/span&gt; blog will be going back to its pristine &amp;#8220;no fluff, just stuff&amp;#8221; program after today.  It is a little hard to keep new content flowing, but we&amp;#8217;ll do the best we can.  Until then, happy hacking!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 17 Mar 2010 16:50:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/022-rbp-now-open.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/022-rbp-now-open.html</guid></item><item><title>Ruby Tuesdays: RBP Chapter 7</title><description>&lt;p&gt;If you&amp;#8217;re reading this blog, you probably know that the &lt;a href="http://rubybestpractices.com"&gt;Ruby Best Practices&lt;/a&gt; book exists.  Even if you haven&amp;#8217;t read it, you might have a sense for the sort of topics we cover based on the content you&amp;#8217;ve seen on this blog. But now, everyone is going to get a chance to read &lt;span class="caps"&gt;RBP&lt;/span&gt; the way its meant to be read: as a conversation.&lt;/p&gt;
&lt;h3&gt;Progress To Date&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;ve already released the first five chapters, and discussion has been great.  Be sure to check out these previous posts if you haven&amp;#8217;t already:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
   &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;Chapter 1: Driving Code Through Tests&lt;/a&gt;&lt;/li&gt;,&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;Chapter 2: Designing Beautiful &lt;span class="caps"&gt;APIS&lt;/span&gt; / Chapter 3: Mastering the Dynamic Toolkit&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/018-rbp-ch4.html"&gt;Chapter 4: Text Processing and File Management&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/019-rbp-ch5.html"&gt;Chapter 5: Functional Programming Techniques&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/020-rbp-ch6.html"&gt;Chapter 6: When Things Go Wrong&lt;/a&gt;&lt;/li&gt;&lt;/p&gt;
&lt;/ul&gt;

&lt;p&gt;These discussions should give you a sense of the wide range of ideas our readers have been sharing as a result of this ongoing book study.&lt;/p&gt;
&lt;h3&gt;How To Read &lt;span class="caps"&gt;RBP&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Readers are encouraged to &lt;a href="http://jsomers.net/blog/kenjitsu"&gt;fight &lt;span class="caps"&gt;RBP&lt;/span&gt; as they read it&lt;/a&gt;, rather than just soaking up the information.  Although I claim this book is about &amp;#8220;Best Practices&amp;#8221;, the only reason that is true is that it&amp;#8217;s a result of countless conversations with folks who are deep in the Ruby trenches getting stuff done.  The only way for &lt;span class="caps"&gt;RBP&lt;/span&gt; to remain current and relevant is to continue these discussions, using its content as a jumping off point for fresh ideas.&lt;/p&gt;
&lt;h3&gt;Today&amp;#8217;s Topic&lt;/h3&gt;
&lt;p&gt;In 2010, programmers no longer have an excuse for not having a baseline understanding of multilingualization and localization techniques.  But for those who have not yet taken a crash course, this chapter on &amp;#8220;Reducing Cultural Barriers&amp;#8221; provides a primer that should be good enough to get you started.&lt;/p&gt;
&lt;p&gt;Go ahead and &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch07.pdf"&gt;download Chapter 7 now&lt;/a&gt;.  You&amp;#8217;re encouraged to participate in the discussion here once you&amp;#8217;ve had a chance to play with some of the ideas from the book. Don&amp;#8217;t worry if it takes you more than a few days finish reading it, you can come back and comment any time.&lt;/p&gt;
&lt;p&gt;Enjoy, and come back next Tuesday (2010.03.16) for the final chapter, &amp;#8220;Skillful Project Maintenance&amp;#8221;, which covers Ruby&amp;#8217;s built in project management tools, and some of the techniques surrounding them.&lt;/p&gt;
&lt;h3&gt;For Your Consideration&lt;/h3&gt;
&lt;p&gt;O&amp;#8217;Reilly is being really nice to me by letting me give away my book, especially considering that I haven&amp;#8217;t quite cleared my advance yet (though I&amp;#8217;m getting closer now, finally!)&lt;/p&gt;
&lt;p&gt;If you like what you see, please buy the book.  You can get it &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;directly from the publisher&lt;/a&gt; or via &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;amazon&lt;/a&gt;.  I imagine that seeing decent sales numbers will encourage O&amp;#8217;Reilly to keep moving towards making their content openly available, which would totally rock.&lt;/p&gt;
&lt;p&gt;For those wishing to do interesting things with this material, note that it is released under the &lt;a href="http://creativecommons.org/licenses/by-nc-sa/3.0/"&gt;Creative Commons NC-SA&lt;/a&gt; license.  This will become immensely more useful once the source documents for the manuscript are posted in late March, but if you&amp;#8217;ve got any questions at all about this, you can ask me, and I&amp;#8217;ll ask my publisher, and we&amp;#8217;ll get back to you.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 09 Mar 2010 14:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/021-rbp-ch7.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/021-rbp-ch7.html</guid></item><item><title>Ruby Tuesdays: RBP Chapter 6</title><description>&lt;p&gt;If you&amp;#8217;re reading this blog, you probably know that the &lt;a href="http://rubybestpractices.com"&gt;Ruby Best Practices&lt;/a&gt; book exists.  Even if you haven&amp;#8217;t read it, you might have a sense for the sort of topics we cover based on the content you&amp;#8217;ve seen on this blog. But now, everyone is going to get a chance to read &lt;span class="caps"&gt;RBP&lt;/span&gt; the way its meant to be read: as a conversation.&lt;/p&gt;
&lt;h3&gt;Progress To Date&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;ve already released the first five chapters, and discussion has been great.  Be sure to check out these previous posts if you haven&amp;#8217;t already:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
   &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;Chapter 1: Driving Code Through Tests&lt;/a&gt;&lt;/li&gt;,&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;Chapter 2: Designing Beautiful &lt;span class="caps"&gt;APIS&lt;/span&gt; / Chapter 3: Mastering the Dynamic Toolkit&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/018-rbp-ch4.html"&gt;Chapter 4: Text Processing and File Management&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/019-rbp-ch5.html"&gt;Chapter 5: Functional Programming Techniques&lt;/a&gt;&lt;/li&gt;&lt;/p&gt;
&lt;/ul&gt;

&lt;p&gt;These discussions should give you a sense of the wide range of ideas our readers have been sharing as a result of this ongoing book study.&lt;/p&gt;
&lt;h3&gt;How To Read &lt;span class="caps"&gt;RBP&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Readers are encouraged to &lt;a href="http://jsomers.net/blog/kenjitsu"&gt;fight &lt;span class="caps"&gt;RBP&lt;/span&gt; as they read it&lt;/a&gt;, rather than just soaking up the information.  Although I claim this book is about &amp;#8220;Best Practices&amp;#8221;, the only reason that is true is that it&amp;#8217;s a result of countless conversations with folks who are deep in the Ruby trenches getting stuff done.  The only way for &lt;span class="caps"&gt;RBP&lt;/span&gt; to remain current and relevant is to continue these discussions, using its content as a jumping off point for fresh ideas.&lt;/p&gt;
&lt;h3&gt;Today&amp;#8217;s Topic&lt;/h3&gt;
&lt;p&gt;This chapter on what to do &amp;#8220;When Things Go Wrong&amp;#8221; turned out to be one of the most enjoyable ones to write.  Having a strong ability to troubleshoot and debug code is a powerful skill to have, and I do my best to share all my secrets here.&lt;/p&gt;
&lt;p&gt;Go ahead and &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch06.pdf"&gt;download Chapter 6 now&lt;/a&gt;.  You&amp;#8217;re encouraged to participate in the discussion here once you&amp;#8217;ve had a chance to play with some of the ideas from the book. Don&amp;#8217;t worry if it takes you more than a few days finish reading it, you can come back and comment any time.&lt;/p&gt;
&lt;p&gt;Enjoy, and come back next Tuesday (2010.03.09) for Chapter 7, &amp;#8220;Reducing Cultural Barriers&amp;#8221;, which covers multilingualization (m17n) and localization (L10n) techniques.&lt;/p&gt;
&lt;h3&gt;For Your Consideration&lt;/h3&gt;
&lt;p&gt;O&amp;#8217;Reilly is being really nice to me by letting me give away my book, especially considering that I haven&amp;#8217;t quite cleared my advance yet (though I&amp;#8217;m getting closer now, finally!)&lt;/p&gt;
&lt;p&gt;If you like what you see, please consider buying the book.  You can get it &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;directly from the publisher&lt;/a&gt; or via &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;amazon&lt;/a&gt;.  I imagine that seeing decent sales numbers will encourage O&amp;#8217;Reilly to keep moving towards making their content openly available, which would totally rock.&lt;/p&gt;
&lt;p&gt;For those wishing to do interesting things with this material, note that it is released under the &lt;a href="http://creativecommons.org/licenses/by-nc-sa/3.0/"&gt;Creative Commons NC-SA&lt;/a&gt; license.  This will become immensely more useful once the source documents for the manuscript are posted in late March, but if you&amp;#8217;ve got any questions at all about this, you can ask me, and I&amp;#8217;ll ask my publisher, and we&amp;#8217;ll get back to you.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 02 Mar 2010 07:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/020-rbp-ch6.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/020-rbp-ch6.html</guid></item><item><title>Ruby Tuesdays: RBP Chapter 5</title><description>&lt;p&gt;If you&amp;#8217;re reading this blog, you probably know that the &lt;a href="http://rubybestpractices.com"&gt;Ruby Best Practices&lt;/a&gt; book exists.  Even if you haven&amp;#8217;t read it, you might have a sense for the sort of topics we cover based on the content you&amp;#8217;ve seen on this blog. But now, everyone is going to get a chance to read &lt;span class="caps"&gt;RBP&lt;/span&gt; the way its meant to be read: as a conversation.&lt;/p&gt;
&lt;h3&gt;Progress To Date&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;ve already released the first four chapters, and discussion has been great.  Be sure to check out these previous posts if you haven&amp;#8217;t already:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
   &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;Chapter 1: Driving Code Through Tests&lt;/a&gt;&lt;/li&gt;,&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;Chapter 2: Designing Beautiful &lt;span class="caps"&gt;APIS&lt;/span&gt; / Chapter 3: Mastering the Dynamic Toolkit&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;
  &lt;li&gt;&lt;a href="http://blog.rubybestpractices.com/posts/gregory/018-rbp-ch4.html"&gt;Chapter 4: Text Processing and File Management&lt;/a&gt;&lt;/li&gt;&lt;/p&gt;
&lt;/ul&gt;

&lt;p&gt;These discussions should give you a sense of the wide range of ideas our readers have been sharing as a result of this ongoing book study.&lt;/p&gt;
&lt;h3&gt;How To Read &lt;span class="caps"&gt;RBP&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Readers are encouraged to &lt;a href="http://jsomers.net/blog/kenjitsu"&gt;fight &lt;span class="caps"&gt;RBP&lt;/span&gt; as they read it&lt;/a&gt;, rather than just soaking up the information.  Although I claim this book is about &amp;#8220;Best Practices&amp;#8221;, the only reason that is true is that it&amp;#8217;s a result of countless conversations with folks who are deep in the Ruby trenches getting stuff done.  The only way for &lt;span class="caps"&gt;RBP&lt;/span&gt; to remain current and relevant is to continue these discussions, using its content as a jumping off point for fresh ideas.&lt;/p&gt;
&lt;h3&gt;Today&amp;#8217;s Topic&lt;/h3&gt;
&lt;p&gt;If you&amp;#8217;re willing to participate in the discussion afterwards, go ahead and &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch05.pdf"&gt;download Chapter 5 now&lt;/a&gt;.  Don&amp;#8217;t worry if it takes you more than a few days finish reading it, you can come back and comment any time.&lt;/p&gt;
&lt;p&gt;This chapter was supposed to be my &amp;#8220;fun&amp;#8221; topic, but turned out to be more practical than I expected.  I had originally started out writing about how to do functional programming in Ruby, but later realized that was an incredibly bad idea.   Ruby isn&amp;#8217;t a great functional programming language, but certain functional programming techniques do still pay off.  This chapter is about those techniques.&lt;/p&gt;
&lt;p&gt;Enjoy, and come back next Tuesday (2010.03.02) for Chapter 6, &amp;#8220;When Things Go Wrong&amp;#8221;.&lt;/p&gt;
&lt;h3&gt;For Your Consideration&lt;/h3&gt;
&lt;p&gt;O&amp;#8217;Reilly is being really nice to me by letting me give away my book, especially considering that I haven&amp;#8217;t quite cleared my advance yet.  So if you like what you see, and want to be able to read it all now instead of waiting several more weeks for it, please consider buying the book.  You can get it &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;directly from the publisher&lt;/a&gt; or via &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;amazon&lt;/a&gt;.  While I do make a little more money when you buy from O&amp;#8217;Reilly, I wasn&amp;#8217;t expecting to get rich off of &lt;span class="caps"&gt;RBP&lt;/span&gt;, so don&amp;#8217;t feel bad buying the discounted copies from Amazon.  But I&amp;#8217;m pretty sure that seeing a spike in sales would encourage them to do more open source books, so&amp;#8230; keep that in mind.&lt;/p&gt;
&lt;p&gt;For those wishing to do interesting things with this material, note that it is released under the &lt;a href="http://creativecommons.org/licenses/by-nc-sa/3.0/"&gt;Creative Commons NC-SA&lt;/a&gt; license.  This will become immensely more useful once the source documents are posted in late March, but if you&amp;#8217;ve got any questions at all about this, you can ask me, and I&amp;#8217;ll ask my publisher, and we&amp;#8217;ll get back to you.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 23 Feb 2010 15:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/019-rbp-ch5.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/019-rbp-ch5.html</guid></item><item><title>Ruby Tuesdays: RBP Chapter 4</title><description>&lt;p&gt;If you&amp;#8217;re reading this blog, you probably know that the &lt;a href="http://rubybestpractices.com"&gt;Ruby Best Practices&lt;/a&gt; book exists.  Even if you haven&amp;#8217;t read it, you might have a sense for the sort of topics we cover based on the content you&amp;#8217;ve seen on this blog. But now, everyone is going to get a chance to read &lt;span class="caps"&gt;RBP&lt;/span&gt; the way its meant to be read: as a conversation.&lt;/p&gt;
&lt;h3&gt;Progress To Date&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;ve already released the first three chapters, and discussion has been great.  If you haven&amp;#8217;t seen them already, check out the first week&amp;#8217;s &lt;a href="http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;discussion on testing&lt;/a&gt;, and then head over and look at the &lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;double header on &lt;span class="caps"&gt;API&lt;/span&gt; design and metaprogramming&lt;/a&gt;.  These discussions should give you a sense of the wide range of ideas our readers have been sharing as a result of this book study.&lt;/p&gt;
&lt;h3&gt;How To Read &lt;span class="caps"&gt;RBP&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Readers are encouraged to &lt;a href="http://jsomers.net/blog/kenjitsu"&gt;fight &lt;span class="caps"&gt;RBP&lt;/span&gt; as they read it&lt;/a&gt;, rather than just soaking up the information.  Although I claim this book is about &amp;#8220;Best Practices&amp;#8221;, the only reason that is true is that it&amp;#8217;s a result of countless conversations with folks who are deep in the Ruby trenches getting stuff done.  The only way for &lt;span class="caps"&gt;RBP&lt;/span&gt; to remain current and relevant is to continue these discussions, using its content as a jumping off point for fresh ideas.&lt;/p&gt;
&lt;h3&gt;Today&amp;#8217;s Topic&lt;/h3&gt;
&lt;p&gt;If you&amp;#8217;re willing to participate in the discussion afterwards, go ahead and &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch04.pdf"&gt;download Chapter 4 now&lt;/a&gt;.  Don&amp;#8217;t worry if it takes you more than a few days finish reading it, you can come back and comment any time.&lt;/p&gt;
&lt;p&gt;While chapters 2 and 3 were about somewhat subtle features, this chapter is as concrete as they come.  Those with some Perl blood in them should find it familiar, as it&amp;#8217;s all about how to munge text and move stuff around on your filesystem.  But those who struggle with the scripting side of Ruby should find a number of techniques that will make their job easier.&lt;/p&gt;
&lt;p&gt;Enjoy, and come back next Tuesday (2010.02.23) for Chapter 5, &amp;#8220;Functional Programming Techniques&amp;#8221;.&lt;/p&gt;
&lt;h3&gt;For Your Consideration&lt;/h3&gt;
&lt;p&gt;O&amp;#8217;Reilly is being really nice to me by letting me give away my book, especially considering that I haven&amp;#8217;t quite cleared my advance yet.  So if you like what you see, and want to be able to read it all now instead of waiting several more weeks for it, please consider buying the book.  You can get it &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;directly from the publisher&lt;/a&gt; or via &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;amazon&lt;/a&gt;.  While I do make a little more money when you buy from O&amp;#8217;Reilly, I wasn&amp;#8217;t expecting to get rich off of &lt;span class="caps"&gt;RBP&lt;/span&gt;, so don&amp;#8217;t feel bad buying the discounted copies from Amazon.  But I&amp;#8217;m pretty sure that seeing a spike in sales would encourage them to do more open source books, so&amp;#8230; keep that in mind.&lt;/p&gt;
&lt;p&gt;For those wishing to do interesting things with this material, note that it is released under the &lt;a href="http://creativecommons.org/licenses/by-nc-sa/3.0/"&gt;Creative Commons NC-SA&lt;/a&gt; license.  This will become immensely more useful once the source documents are posted in late March, but if you&amp;#8217;ve got any questions at all about this, you can ask me, and I&amp;#8217;ll ask my publisher, and we&amp;#8217;ll get back to you.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 16 Feb 2010 15:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/018-rbp-ch4.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/018-rbp-ch4.html</guid></item><item><title>RBP is useless without you</title><description>&lt;p&gt;&lt;em&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; 2010.02.13: Lots of folks have taken the initiative to comment after reading this, and others have pointed out that they indeed need more time before they can comment.  But since people seem generally interested in seeing Ch4 sooner rather than later, I&amp;#8217;ll release it on Tuesday.  Coincidentally, there is a great post I found on HN that does a better job than I did summarizing how tech books (especially &lt;span class="caps"&gt;RBP&lt;/span&gt;) ought to be read.  If you haven&amp;#8217;t seen it yet, go read &lt;a href="http://jsomers.net/blog/kenjitsu"&gt;Kenjitsu&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Since I&amp;#8217;ve posted the &lt;a href="http://github.com/sandal/rbp-book/tree/gh-pages/pdfs/"&gt;first three chapters&lt;/a&gt;, my amazon ratings have gone way up, and I&amp;#8217;ve seen lots of people mention the book on twitter.  This is great for marketing, but as you may know, I care much more about the actual content here.&lt;/p&gt;
&lt;p&gt;Things started out great!  We had an &lt;a href="http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;awesome discussion about testing&lt;/a&gt; two weeks ago and I expected to see the same on &lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;APIs and metaprogramming&lt;/a&gt;.  But as it turned out, only two brave souls offered up their thoughts, even though lots of people downloaded the content.  That was a big disappointment to me, and now I&amp;#8217;m trying to guess at what went wrong.&lt;/p&gt;
&lt;p&gt;One possibility is that I&amp;#8217;ve oversaturated folks, so I&amp;#8217;m giving everyone an extra week to &lt;a href="http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html"&gt;comment on Ch2-3&lt;/a&gt; before I put out the &amp;#8220;Text Processing and File Management&amp;#8221; chapter.  Hopefully, by next Friday we&amp;#8217;ll have a good discussion going there, and the Ch4 release will just keep the ball rolling.&lt;/p&gt;
&lt;p&gt;But far more grave, is my fear that some folks might be too timid to express their thoughts about the material, perhaps because it&amp;#8217;s in book form rather than written up in some generic blog post.  This to me, is exactly what will prevent them from getting anything worthwhile out of my book.  So if you&amp;#8217;ve been holding back comments just because you didn&amp;#8217;t want to challenge RBP&amp;#8217;s &amp;#8220;bookiness&amp;#8221; out in the open, please don&amp;#8217;t.  I can handle all sorts of criticism, and the book will benefit as a result of it.&lt;/p&gt;
&lt;p&gt;I am very serious about the idea that &amp;#8220;Best Practices&amp;#8221; really don&amp;#8217;t have much value in a vacuum. I&amp;#8217;m certain that the benefits that my book can offer its readers are maximized in open conversation.  This is why I made sure to build an eventual open content licensing into RBP&amp;#8217;s contract, and why I&amp;#8217;m releasing free chapters now.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;RBP&lt;/span&gt; is useless without you, so please, participate.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 12 Feb 2010 05:24:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/017-rbp-useless.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/017-rbp-useless.html</guid></item><item><title>Weekend Reading: RBP Chapters 2-3</title><description>&lt;p&gt;If you&amp;#8217;re reading this blog, you probably know that the &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;Ruby Best Practices&lt;/a&gt; book exists.  Even if you haven&amp;#8217;t read it, you might have a sense for the sort of topics we cover based on the content you&amp;#8217;ve seen on this blog. But now, everyone is going to get a chance to read &lt;span class="caps"&gt;RBP&lt;/span&gt; the way its meant to be read: as a conversation.&lt;/p&gt;
&lt;p&gt;If you missed &lt;a href="http://www.blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html"&gt;last week&amp;#8217;s post&lt;/a&gt;, which introduces chapter 1, you&amp;#8217;ll want to check that out.  There was some great discussion among readers that add a lot to what I had to say in the book.  I expect we&amp;#8217;ll see the same thing happen this time around.&lt;/p&gt;
&lt;h3&gt;Today&amp;#8217;s Topics&lt;/h3&gt;
&lt;p&gt;This week, I&amp;#8217;m releasing two chapters, &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch02.pdf"&gt;Designing Beautiful APIs&lt;/a&gt; and &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch03.pdf"&gt;Mastering the Dynamic Toolkit&lt;/a&gt;.  Incase the latter sounds familiar, it is indeed the book&amp;#8217;s sample chapter.  But it also happens to be my favorite chapter in the book, so I didn&amp;#8217;t want to skip over it without a chance for discussion.&lt;/p&gt;
&lt;p&gt;So remember, if you click those links, your responsibility is to participate in the discussion here by offering up your thoughts about what you&amp;#8217;ve read.  Best practices cannot be rules that are simply absorbed and followed if they are to be of any use&amp;#8230; instead, they must be extracted from and influenced by those who work with the real problems that they are meant to solve.&lt;/p&gt;
&lt;p&gt;It would be great to hear your experiences with the topics these chapters cover, even if they&amp;#8217;re not directly related to the book content.&lt;/p&gt;
&lt;h3&gt;For Your Consideration&lt;/h3&gt;
&lt;p&gt;O&amp;#8217;Reilly is being really nice to me by letting me give away my book, especially considering that I haven&amp;#8217;t quite cleared my advance yet.  So if you like what you see, and want to be able to read it all now instead of waiting eight weeks for it, please consider buying the book.  You can get it &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;directly from the publisher&lt;/a&gt; or via &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;amazon&lt;/a&gt;.  While I do make a little more money when you buy from O&amp;#8217;Reilly, I wasn&amp;#8217;t expecting to get rich off of &lt;span class="caps"&gt;RBP&lt;/span&gt;, so don&amp;#8217;t feel bad buying the discounted copies from Amazon.  But I&amp;#8217;m pretty sure that seeing a spike in sales would encourage them to do more open source books, so&amp;#8230; keep that in mind.&lt;/p&gt;
&lt;p&gt;For those wishing to do interesting things with this material, note that it is released under the &lt;a href="http://creativecommons.org/licenses/by-nc-sa/3.0/"&gt;Creative Commons NC-SA&lt;/a&gt; license.  This will become immensely more useful once the source documents are posted in late March, but if you&amp;#8217;ve got any questions at all about this, you can ask me, and I&amp;#8217;ll ask my publisher, and we&amp;#8217;ll get back to you.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Anyway that&amp;#8217;s all for now, enjoy the chapters and let me know what you think!&lt;/em&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 05 Feb 2010 14:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/rbp-ch2-3.html</guid></item><item><title>Weekend Reading: RBP Chapter 1</title><description>&lt;p&gt;If you&amp;#8217;re reading this blog, you probably know that the &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;Ruby Best Practices&lt;/a&gt; book exists.  Even if you haven&amp;#8217;t read it, you might have a sense for the sort of topics we cover based on the content you&amp;#8217;ve seen on this blog. But now, everyone is going to get a chance to read &lt;span class="caps"&gt;RBP&lt;/span&gt; the way its meant to be read: as a conversation.&lt;/p&gt;
&lt;p&gt;For the next 8 weeks, I will post a chapter from &lt;span class="caps"&gt;RBP&lt;/span&gt; every Friday.  You don&amp;#8217;t need to pay me a cent if you download it, but what I do want you to do is leave a comment here sharing your thoughts once you&amp;#8217;ve finished reading.  This way, we can use RBP&amp;#8217;s content as a jumping off point for conversation, rather than treating it like some sort of static rulebook. Through this process, we can discover what our common practices really are.  Over time, I will incorporate these ideas back into the manuscript, helping to keep the book up to date and relevant.&lt;/p&gt;
&lt;h3&gt;Today&amp;#8217;s Topic&lt;/h3&gt;
&lt;p&gt;The first chapter I am releasing is &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch01.pdf"&gt;Driving Code Through Tests&lt;/a&gt;,  which covers a number testing philosophies and techniques.  It aims to be framework agnostic, but leans on the Test::Unit &lt;span class="caps"&gt;API&lt;/span&gt; for its examples.  The core idea of this chapter is to expose folks to the &amp;#8220;spirit&amp;#8221; of testing, rather than just the mechanics of it.&lt;/p&gt;
&lt;p&gt;Upon re-reading this chapter in the winter of 2010 having written it in the spring of 2008, there are definitely a couple things I think differently about now.  But as it turns out, I was more surprised by how much hasn&amp;#8217;t changed, and how well this chapter seems to have stood the test of time.  Of course, you may disagree, and I encourage you to tell me so if that&amp;#8217;s the case. :)&lt;/p&gt;
&lt;p&gt;Ready to get started?  Go ahead and &lt;a href="http://sandal.github.com/rbp-book/pdfs/ch01.pdf"&gt;download the chapter&lt;/a&gt; now.  Remember, if you click that link, I&amp;#8217;m expecting you to post back here with your thoughts (or alternatively, write a response on your own blog and send me a link).  If you find a technical error, you can report it via &lt;a href="http://github.com/sandal/rbp-book/issues"&gt;Github issues&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;For Your Consideration&lt;/h3&gt;
&lt;p&gt;O&amp;#8217;Reilly is being really nice to me by letting me give away my book, especially considering that I haven&amp;#8217;t quite cleared my advance yet.  So if you like what you see, and want to be able to read it all now instead of waiting eight weeks for it, please consider buying the book.  You can get it &lt;a href="http://oreilly.com/catalog/9780596523015"&gt;directly from the publisher&lt;/a&gt; or via &lt;a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009"&gt;amazon&lt;/a&gt;.  While I do make a little more money when you buy from O&amp;#8217;Reilly, I wasn&amp;#8217;t expecting to get rich off of &lt;span class="caps"&gt;RBP&lt;/span&gt;, so don&amp;#8217;t feel bad buying the discounted copies from Amazon.  But I&amp;#8217;m pretty sure that seeing a spike in sales would encourage them to do more open source books, so&amp;#8230; keep that in mind.&lt;/p&gt;
&lt;p&gt;For those wishing to do interesting things with this material, note that it is released under the &lt;a href="http://creativecommons.org/licenses/by-nc-sa/3.0/"&gt;Creative Commons NC-SA&lt;/a&gt; license.  This will become immensely more useful once the source documents are posted in late March, but if you&amp;#8217;ve got any questions at all about this, you can ask me, and I&amp;#8217;ll ask my publisher, and we&amp;#8217;ll get back to you.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Anyway that&amp;#8217;s all for now, enjoy the chapter and let me know what you think!&lt;/em&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 29 Jan 2010 14:32:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/015-rbp-ch1.html</guid></item><item><title>Reflections from "The Compleat Rubyist"</title><description>&lt;p&gt;Ever since 2005, I&amp;#8217;ve been attending and speaking at Ruby events.  But the one thing I always wanted to do was run training sessions.  Thankfully, over the last couple years, &lt;span class="caps"&gt;LSRC&lt;/span&gt; has allowed me to get my feet wet &amp;#8212; but only as one of many excellent choices in a lineup of Ruby&amp;#8217;s best and brightest.  Until last weekend, I had never participated in professional training event that stood on its own.&lt;/p&gt;
&lt;p&gt;What eventually became &amp;#8220;The Compleat Rubyist&amp;#8221; training, was originally an idea for &amp;#8220;The Compleat Rubyist&amp;#8221; book promotion.  Jeremy McAnally, David A. Black, and myself have all written Ruby books, and they seem to compliment each other well.  But in doing this, we realized that it would be more valuable, and more fun, to bring our show out on the open road rather than package it up in a box set.&lt;/p&gt;
&lt;p&gt;Thus, our training event was born, and we set our sights on Tampa, FL for its first incarnation.&lt;/p&gt;
&lt;h3&gt;The Training&lt;/h3&gt;
&lt;p&gt;Instead of having a linear, specific set of bullet points to teach from, we broke our training into four half-day sessions that are representive of some of the most important topics in the Ruby ecosystem.  This gave us a lot of leeway to adjust our materials and discussions based on individual student needs.   The topics we picked were &amp;#8220;Ruby Versions and Implementations&amp;#8221;, &amp;#8220;Metaprogramming&amp;#8221;, &amp;#8220;Testing&amp;#8221;, and &amp;#8220;Style and Substance&amp;#8221;.   Though these may seem a bit amorphous on first sight, they do a pretty good job of covering four key topics that a budding Rubyist must understand and come to appreciate.&lt;/p&gt;
&lt;p&gt;Here are just a few thoughts from each of the modules, after seeing how things played out in class.&lt;/p&gt;
&lt;h4&gt;Ruby Versions and Implementations&lt;/h4&gt;
&lt;p&gt;One of the main things that keep people from trying out the alternative Ruby implementations and the various versions of Ruby is that they&amp;#8217;re understandably afraid of messing up their systems.  So David took a three-pronged approach to helping students overcome that fear.&lt;/p&gt;
&lt;p&gt;The easiest stepping stone, perhaps, was to use &lt;a href="http://ruby-versions.net/"&gt;ruby-versions.net&lt;/a&gt;.  This great resource (maintained by David), lets you try out all sorts of ruby versions and implementations via a shell account so that you don&amp;#8217;t need to worry about touching anything on your own machine.  While this isn&amp;#8217;t meant to be a long term solution, it&amp;#8217;s probably the easiest way that you can try out pretty much any version of Ruby without having to go hunt down, install, and configure their packages.&lt;/p&gt;
&lt;p&gt;In addition to this, David showed folks how to make use of multiruby (a tool that&amp;#8217;s part of ZenTest).  My favorite example of the whole day was one showing that using Ruby 1.8.7, it&amp;#8217;s possible to write code that works on neither 1.8.6 nor 1.9.1, which lead into a whole other interesting discussion:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  $ multiruby -e "p 'hello'.map(&amp;amp;:upcase)"

  VERSION = 1.8.6-p388
  CMD     = ~/.multiruby/install/1.8.6-p388/bin/ruby -e p 'hello'.map(&amp;amp;:upcase)

  -e:1: wrong argument type Symbol (expected Proc) (TypeError)

  RESULT = pid 3988 exit 1

  VERSION = 1.8.7-p249
  CMD     = ~/.multiruby/install/1.8.7-p249/bin/ruby -e p 'hello'.map(&amp;amp;:upcase)

  ["HELLO"]

  RESULT = pid 3991 exit 0

  VERSION = 1.9.1-p378
  CMD     = ~/.multiruby/install/1.9.1-p378/bin/ruby -e p 'hello'.map(&amp;amp;:upcase)

  -e:1:in `&amp;lt;main&amp;gt;': undefined method `map' for "hello":String (NoMethodError)

  RESULT = pid 3995 exit 1

  TOTAL RESULT = 2 failures out of 3

  Passed: 1.8.7-p249
  Failed: 1.8.6-p388, 1.9.1-p378
&lt;/pre&gt;
&lt;p&gt;We also took a look at &lt;a href="http://rvm.beginrescueend.com/"&gt;rvm&lt;/a&gt; , which is another neat tool for managing multiple Ruby versions.  While not necessarily a tool for every possible need, it is a very easy way of managing multiple rubies side by side without trashing your system-wide configurations.&lt;/p&gt;
&lt;p&gt;Between these three tools, the students ended up with all they needed to explore the different implementations and versions for themselves.   But that was really just the beginning.   David then went on to discuss the various peculiarities across the board, and was sure to discuss where certain implementations or versions excel, and where they fall behind.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m happy to say that while very few of the students in our class had even tried Ruby 1.9 before our training, every one of them had the opportunity to do so before this session ended.&lt;/p&gt;
&lt;h4&gt;Meta-programming&lt;/h4&gt;
&lt;p&gt;At one point in time, everyone using Ruby thought meta-programming was some sort of magic.  But in a post-Rails world, it is increasingly easy to help folks understand that all of this &amp;#8220;voodoo&amp;#8221; we do is really just making use of public method calls provided by Ruby objects that allow you to modify Ruby itself.  Once a decent understanding of Ruby&amp;#8217;s object model is established, all of the rest of the puzzle pieces just seem to fall into place.&lt;/p&gt;
&lt;p&gt;This session had a lot of great discussions, and several exercises to encourage students to actively learn the tricks of the trade.   Along the way, we stumbled across this interesting distinction between &lt;tt&gt;undef_method&lt;/tt&gt; and &lt;tt&gt;remove_method&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class A
    def foo
      "bar"
    end
  end

  class B &amp;lt; A
    def foo
      "baz"
    end

    remove_method :foo
  end

  class C
    def foo 
      "bar"
    end
  end

  class D &amp;lt; C
    def foo
      "baz"
    end

    undef_method :foo
  end

  p B.new.foo # "bar"

  p D.new.foo # raise NoMethodError
&lt;/pre&gt;
&lt;h4&gt;Testing&lt;/h4&gt;
&lt;p&gt;Fortunately, virtually all of our students had experience with test driven development of some sort or another.  I think that&amp;#8217;s a big difference between the Ruby ecosystem today and of say, 5 years ago.  But similar to versions and implementations, there are so many choices out there that it&amp;#8217;s hard to know what&amp;#8217;s what.  Jeremy kicked off this module with a state of the union address, breaking down the various different philosophies and toolchains there are out there, and what each of them bring to the table.&lt;/p&gt;
&lt;p&gt;Once we got people thinking about the different options for testing out there, we decided to give them a glimpse behind the curtain to realize just how non-magical testing could be.  To do this, we demonstrated not one, but two minimal testing frameworks that we built in less than a page or two of code to demonstrate the key concepts.  While neither of these tools would be sensible to use in production, they both show how non-magical a test runner is under the hood.&lt;/p&gt;
&lt;p&gt;Finally, David gave an open ended testing exercise that was a big hit.   Pizza came, but some of our students ignored it until we reminded them to take a break.   Many kept working on through lunch, all pairing together and actively discussing all sorts of language questions that went far beyond the scope of the exercise.&lt;/p&gt;
&lt;h4&gt;Style and Substance&lt;/h4&gt;
&lt;p&gt;This was my topic, and it went way better than I could have expected it to.  I had a lot more content prepared than what I ended up getting to, mainly because we decided to collect some of the open questions that had come up in class and discuss those before running our scheduled content.  The infamous class variable problem was discussed in great detail, and while I thought I had mastered their weaknesses, I even managed to mess up one of my assumptions about their behavior.  Incase you&amp;#8217;ve never been exposed to this fun before:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class A
    @@foo = "from A"
  end
   
  class B &amp;lt; A
    @@foo = "from B"
  end
   
  # What's the value of @@foo in A now...?
   
  class A
    puts @@foo
  end
   
  # =&amp;gt; from B
   
  # They're shared up the hierarchy?? BOOOO!!!
  # What's even worse? This -&amp;gt;
   
  class C
  end
   
  class D &amp;lt; C
    @@bar = "Magically different..."
  end
   
  class C
    @@bar = "And delicious."
  end
   
  class D
    puts @@bar
    # =&amp;gt; Magically different...
  end
   
  class C
    puts @@bar
    # =&amp;gt; And delicious.
  end
   
  # ZOMG THEY'RE NOT SHARED NOW.
&lt;/pre&gt;
&lt;p&gt;Of course, the only sensible style advice here is to not use class variables at all, and since class instance variables can do the job just as well, that&amp;#8217;s not really a big deal.  We discussed how to do that, and then moved on to the regularly scheduled content.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m not sure whether we had an especially strong set of students or if people are just starting to &amp;#8220;get&amp;#8221; basic Ruby style idioms, but based on the first day and a half, it was evident that our students didn&amp;#8217;t need to be told these basic things.   Instead, I talked about &amp;#8220;Ruby Style and Substance&amp;#8221; at a more philosophical and design-oriented level, which is where I think our community tends to be weakest.  Without giving too much away, a lot of my examples stemmed from discussions we&amp;#8217;ve been having about refactoring Prawn before it&amp;#8217;s 1.0 release, so you can check out our mailing list for a whole bunch of interesting design discussions if you want access to some of the raw materials I used for this session.&lt;/p&gt;
&lt;p&gt;In the end, we gave everyone a chance to try their hand at basic &lt;span class="caps"&gt;API&lt;/span&gt; design for a fictional library we cooked up.  I&amp;#8217;m proud to say that every one of our students produced something that would be passable in an open source project.  Ideally speaking, I&amp;#8217;d like to say some of them took inspiration from what they learned in class, but no matter how they ended up there, I&amp;#8217;m happy to see people writing code that looks like it comes from Ruby. This is especially encouraging considering that every one of our students came to Ruby after Rails was popularized.&lt;/p&gt;
&lt;h3&gt;The Event&lt;/h3&gt;
&lt;p&gt;We wanted &amp;#8220;The Compleat Rubyist&amp;#8221; to be not just a training, but an &amp;#8220;event&amp;#8221; as well.  Because we were trying to keep registration costs low, we didn&amp;#8217;t have much officially scheduled for this, but Tampa.rb really helped us pull through.  Folks congregated at the nearby bar and restaurant Bahama Breeze from the time class ended until late into the evening Friday night.   It was nice to have a mixture of students from the training and locals who just stopped by to hang out and say hi.  I can honestly say that the sort of conversations I was having that night were on par with any I&amp;#8217;ve had at the various conferences I&amp;#8217;ve been at, but without all the associated commotion.&lt;/p&gt;
&lt;p&gt;Pizza lunch on Saturday was a great way for us to leave a large chunk of semi-unscheduled time to discuss whatever topics students had on their mind.  With a relatively small class size, I was able to get to know a number of the attendees, unlike my experience at most conferences these days.&lt;/p&gt;
&lt;p&gt;While I expected to have a good time, I ended up having a blast.  Based on my discussions with Jeremy and David, and the feedback from our students, I gather most others did as well.&lt;/p&gt;
&lt;h3&gt;The Future&lt;/h3&gt;
&lt;p&gt;While the Tampa, FL Compleat Rubyist event is now a thing of the past, we&amp;#8217;re already thinking about how we can bring this session to other places.  I was a little worried before the event that I wouldn&amp;#8217;t want to just go somewhere else and do the same show, but now I know that every one of these is going to be different, since so much of the content was based on interaction with our attendees.&lt;/p&gt;
&lt;p&gt;So, if the stuff I&amp;#8217;ve said here interests you, you should follow &lt;a href=http://twitter.com/compleatrubyist&gt;compleatrubyist on twitter&lt;/a&gt;.  We used our account actively during the event to share various little tips, and probably will continue to do that over time.  But that&amp;#8217;s also the place where you&amp;#8217;ll see announcements about upcoming events, or ask us any questions you might have.&lt;/p&gt;
&lt;p&gt;While I don&amp;#8217;t typically like to use this blog for active marketing, I can honestly say that &amp;#8220;The Compleat Rubyist&amp;#8221; was a lot of fun, and that I think our students learned a ton.  If folks like these summaries, I&amp;#8217;ll try to get one out after each event.  But it will be more fun, of course, if you come join us!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Mon, 25 Jan 2010 15:36:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/014-the-compleat-rubyist.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/014-the-compleat-rubyist.html</guid></item><item><title>Learning From Bad Ideas</title><description>&lt;p&gt;When we know something is ugly or &amp;#8220;evil&amp;#8221;, we&amp;#8217;re quick to replace it with what we know to be a better solution.  But if we don&amp;#8217;t know &lt;strong&gt;why&lt;/strong&gt; the solution is better, it makes it hard for us to investigate those things that look reasonable on the surface, yet have weaknesses just beneath the skin.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll run through a quick example of what I mean, and then, it&amp;#8217;ll be your turn to run with it and report back here.  While I don&amp;#8217;t use this approach all the time, I find it&amp;#8217;s a neat tool to have handy when you&amp;#8217;re trying to push your understanding of your code just a little bit farther.&lt;/p&gt;
&lt;h3&gt;Continuations are Evil?&lt;/h3&gt;
&lt;p&gt;It&amp;#8217;s pretty common knowledge that continuations in Ruby are evil.  But let&amp;#8217;s pretend we didn&amp;#8217;t know that.  We might end up catching ourselves writing code like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  table.each do |row|
    call_cc do |next_row|
      row.each do |field|
        if field.valid?
          do_something_with(field)
        else
          STDERR.puts "skipping a bad row"
          next_row[]
        end 
      end
    end
  end
&lt;/pre&gt;
&lt;p&gt;The continuation approach actually doesn&amp;#8217;t look so bad.  All we&amp;#8217;re doing is iterating over a two-dimensional structure and skipping ahead to the next row if there is a problem with any of the fields. But having to pass along the &lt;tt&gt;next_row&lt;/tt&gt; callback seems a bit heavy handed, and also makes one wonder what sort of magic it might be concealing.  Preserving the same basic flow, we could be using &lt;tt&gt;catch&lt;/tt&gt; / &lt;tt&gt;throw&lt;/tt&gt; instead, lowering the conceptual baggage:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  table.each do |row|
    catch(:next_row) do
      row.each do |field|
        if field.valid?
          do_something_with(field)
        else
          STDERR.puts "skipping a bad row"
          throw :next_row
        end 
      end
    end
  end
&lt;/pre&gt;
&lt;p&gt;So, now, we have something that looks more-or-less like a transactional block, which gets aborted when you throw &lt;tt&gt;:next_row&lt;/tt&gt;.  Not too bad, but if you&amp;#8217;re like me, you probably get annoyed about having your eyes bounce up and down while you trace the flow, in both of these examples.  We can fix that, of course:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def process(row)
    row.each do |field|
      if field.valid?
        do_something_with(field)
      else
        STDERR.puts "skipping a bad row"
        return
      end
    end
  end
  
  table.each { |row| process(row) }
&lt;/pre&gt;
&lt;p&gt;Now this is more like it!  This code is probably the same as what a beginner might write, but it&amp;#8217;s also the clearest out of what we&amp;#8217;ve seen here.  But without it as a point of reference, neither the &lt;tt&gt;catch&lt;/tt&gt; / &lt;tt&gt;throw&lt;/tt&gt; or &lt;tt&gt;callcc&lt;/tt&gt; solutions would look terrible.&lt;/p&gt;
&lt;p&gt;Through this quick little set of examples, I found a good reason &lt;strong&gt;not&lt;/strong&gt; to use continuations for this particular use case.  Arriving back at a simple solution by starting with a &amp;#8220;clever&amp;#8221; one and reducing it down to the fundamentals was a refreshing experience for me.&lt;/p&gt;
&lt;h3&gt;Your Homework&lt;/h3&gt;
&lt;p&gt;Now it&amp;#8217;s your turn.  I&amp;#8217;m encouraging you to ignore &amp;#8220;Best Practices&amp;#8221; and the common idioms that we often accept as gospel.&lt;/p&gt;
&lt;p&gt;Pick one technique that&amp;#8217;s typically considered a no-no and do the best you can to use it in a somewhat reasonable way.  You don&amp;#8217;t want to waste your time with something that not even the most muddy-eyed coder would touch with a ten-foot pole, as you won&amp;#8217;t learn much that way.  Instead, work with something that looks fine or even clever at a first glance.&lt;/p&gt;
&lt;p&gt;Then, try to fix up what you built by using more idiomatic, simpler code.  Your goal is to create something that still looks good, but isn&amp;#8217;t as much as a liability (conceptually) as what you started with.  If you fail to do so, the worst thing that could happen is that you may have come across a new idiom worth sharing with others.  But more likely, with a little effort you&amp;#8217;ll have no trouble uncovering the wisdom behind whatever idiom you were putting to the test.&lt;/p&gt;
&lt;p&gt;What&amp;#8217;s the difference?  Now, something that used to be a somewhat arbitrary rule to you is now common sense.  As contexts shift, you may need to conduct new experiments, but repeat this exercise often enough and you&amp;#8217;ll start to see a powerful intuition develop that allows you to internalize your design decisions rather than running through a punch list of patterns.  And at least for me, I find that experience a lot more fun.  I hope you will, too.&lt;/p&gt;
&lt;p&gt;If you try out this exercise, please share what you&amp;#8217;ve done, either via a link to your own blog, or a &lt;a href="http://gist.github.com"&gt;gist&lt;/a&gt;.  I&amp;#8217;m interested in how many bad ideas we can rack up here. :)&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Sun, 22 Nov 2009 22:57:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/013-good-to-be-bad.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/013-good-to-be-bad.html</guid></item><item><title>Be Nice and Have Fun</title><description>&lt;p&gt;Yet again, we&amp;#8217;re experiencing &lt;a href="http://news.ycombinator.com/item?id=773106"&gt;a firestorm&lt;/a&gt; that will shake things up and change the Ruby community in a big way.  But like anything else, the best thing we can do is remember that the sky is not falling, and that all internet drama is ephemeral at best.&lt;/p&gt;
&lt;p&gt;Sure, we&amp;#8217;ll all dearly miss a valued community member who inspired many of us.  But thanks to distributed revision control systems, we&amp;#8217;ll be able to commemorate _why through his works, currently being &lt;a href="http://github.com/whymirror"&gt;mirrored on github&lt;/a&gt; .&lt;/p&gt;
&lt;p&gt;So what do we do in this post _why world?  The answer to that is fairly simple: the same thing we&amp;#8217;ve been doing.  The true spirit of the Ruby community (and even the free software hacker community at large) is to work on fun stuff, and try to be nice to people.  If we can keep that up, the sky will not fall, and may even make room for new stars to light up the present darkness.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll let this brief statement stand on its own here, as &lt;span class="caps"&gt;RBP&lt;/span&gt; has no intentions of becoming a gossip blog, but feel free to leave your thoughts in the comments.&lt;/p&gt;
&lt;p&gt;Expect a new Ruby 1.9 series post some time before or just after &lt;span class="caps"&gt;LSRC&lt;/span&gt;.  Until then, keep your heads up folks.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: I have Aaron Patterson to thank for expressing what I was thinking in words &amp;#8216;do fun stuff and try to be nice&amp;#8217;&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Fri, 21 Aug 2009 11:25:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/012-be-nice-and-have-fun.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/012-be-nice-and-have-fun.html</guid></item><item><title>Should I Tap that Hash?  (Ruby 1.9 Style)</title><description>&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;: Be sure to read the comments.  I&amp;#8217;ve already learned a lot from them, and you might, too.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Though the &lt;a href="http://rubybestpractices.com"&gt;Ruby Best Practices&lt;/a&gt; book covers Ruby 1.9, it is decidedly not a &amp;#8220;Ruby 1.9 Best Practices&amp;#8221; book.   The reason for this, of course, is that common idioms for Ruby 1.9 haven&amp;#8217;t evolved yet.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s work together to change that.   Every week until Ruby 1.9.2 comes out, I&amp;#8217;ll be coming up with short, manageable bits of Ruby 1.9 code for discussion, as part of a &amp;#8220;Ruby 1.9 Style&amp;#8221; series.  These should not be taken to be authoritative in any way, but instead work as conversation starters so that we can move forward as a community and separate the good from the bad when hacking code on Ruby 1.9.&lt;/p&gt;
&lt;p&gt;Rather than describing in detail how this series will work, I&amp;#8217;ll just begin with some real content and you can get a sense of the format from that.  The only key thing is to remember to not be lazy, and to actually comment on these posts.  Ruby 1.9 is basically the wild west right now, and its going to take quite a few sound voices to tame it.  So be sure to share your thoughts after you take a look at the proposed idiom below.&lt;/p&gt;
&lt;h3&gt;Object#tap, a seemingly benign addition.&lt;/h3&gt;
&lt;p&gt;Ruby 1.9 gives us &lt;tt&gt;tap&lt;/tt&gt;.  And &lt;tt&gt;tap&lt;/tt&gt; is sort of neat because it can be useful as a debugging tool, especially in &lt;a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions.html"&gt;chained code&lt;/a&gt;.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  &amp;gt;&amp;gt;  [1,2,3].map { |x| x + 1 }.tap { |y| p y }.inject(:+)
  [2, 3, 4]
  =&amp;gt; 9
&lt;/pre&gt;
&lt;p&gt;There isn&amp;#8217;t much conceptual weight here, either.  Essentially, you&amp;#8217;re looking at something like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def tap
    yield(self)
    self
  end
&lt;/pre&gt;
&lt;p&gt;But hmm, doesn&amp;#8217;t that look familiar?&lt;/p&gt;
&lt;h3&gt;Object#tap as an alternative to ActiveSupport&amp;#8217;s returning&lt;/h3&gt;
&lt;p&gt;Those who work with Rails or with ActiveSupport in some other capacity will have seen &lt;tt&gt;returning&lt;/tt&gt;.  The problem it is meant to solve is seen in the code below.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def ugly
    results = {}

    [:x, :y, :z].each do |letter|
      results[letter] = rand(100)
    end

    results
  end
&lt;/pre&gt;
&lt;p&gt;Using &lt;tt&gt;returning&lt;/tt&gt;, this can be cleaned up a bit:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def sexy
  returning({}) do |results|
    [:x, :y, :z].each do |letter|
      results[letter] = rand(100)
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;This code &lt;strike&gt;is definitely more &lt;span class="caps"&gt;DRY&lt;/span&gt;, and&lt;/strike&gt; looks pretty clean.  The only problem with it is that since it relies on ActiveSupport, you&amp;#8217;re essentially sifting through DHH&amp;#8217;s junk drawer for a piece of non-standard functionality for a fairly basic purpose.  While it may not be a &amp;#8220;Rails Best Practice&amp;#8221;, I tend to shy away from this sort of thing to keep from being too dependent on a framework that I only use part of the time.&lt;/p&gt;
&lt;p&gt;But if we think for a second, how would &lt;tt&gt;returning&lt;/tt&gt; be implemented?  Something like this, right?&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def returning(obj)
    yield(obj)
    obj
  end
&lt;/pre&gt;
&lt;p&gt;Go check out the end of the previous section.  What we can see now is that &lt;tt&gt;returning&lt;/tt&gt; is just &lt;tt&gt;Object#tap&lt;/tt&gt; inside out!  So what happens when we use &lt;tt&gt;tap&lt;/tt&gt; that way?  Let&amp;#8217;s take a look.&lt;/p&gt;
&lt;h3&gt;Tapping that Hash.&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;ve started on a few Ruby 1.9 production apps, and that&amp;#8217;s where I first ran into attempting to use &lt;tt&gt;tap&lt;/tt&gt; as if it were &lt;tt&gt;returning&lt;/tt&gt;.  Take a look at this bit of code:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def times_for_period(date)
    days = work_days_for_period(date)
    regular_hours_by_week = Hash.new(0)

    { :regular_hours =&amp;gt; 0, :other_hours =&amp;gt; Hash.new(0) }.tap do |results|
      days.each do |d|
        times = d.times

        results[:regular_hours] += times[:regular_hours]
        regular_hours_by_week[d.date.cweek] += times[:regular_hours]

        times[:other_hours].each { |k,v| results[:other_hours][k] += v }
      end

      results[:weekly_hours] = regular_hours_by_week.keys.sort.map do |k| 
        regular_hours_by_week[k]
      end
    end
  end
&lt;/pre&gt;
&lt;p&gt;What I&amp;#8217;m doing here is calling &lt;tt&gt;tap&lt;/tt&gt; on a default hash and then populating it via the code within the block.  If I was simply iterating over some values, I might use &lt;tt&gt;inject&lt;/tt&gt; or &lt;tt&gt;each_with_object&lt;/tt&gt;, but my needs were a little more complicated here.&lt;/p&gt;
&lt;p&gt;This approach adds a little complexity, but in my experience, having to explicitly return a collection at the end of a method can be cumbersome, and I frequently forget about it during refactoring and cause my tests to go red on me.&lt;/p&gt;
&lt;p&gt;What do you think? Is &lt;tt&gt;Object#tap&lt;/tt&gt; the new &lt;tt&gt;returning&lt;/tt&gt;?  Or should we put this knife carefully back into DHH&amp;#8217;s junk drawer for safekeeping? I&amp;#8217;m pretty sure Rails didn&amp;#8217;t invent this functionality, but that&amp;#8217;s where a lot of us might have encountered it first.&lt;/p&gt;
&lt;p&gt;Help me decide whether this is elegant or nasty in the comments below.&lt;/p&gt;
&lt;h3&gt;A Gentle Reminder&lt;/h3&gt;
&lt;p&gt;I have to warn you, some of my ideas might be bad.  I&amp;#8217;ll be posting code that I find interesting or stuff that I don&amp;#8217;t know exactly how I feel about.  If we hit a dud from time to time, it&amp;#8217;s only to be expected.  But if you want to help me out, consider contributing content to this series.  If you have an idea for a guest post, go ahead and email me with the details, and I&amp;#8217;ll get back to you.&lt;/p&gt;
&lt;p&gt;Looking forward to your thoughts on this one and many more in the coming weeks!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Wed, 05 Aug 2009 12:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/011-tap-that-hash.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/011-tap-that-hash.html</guid></item><item><title>25th Post!</title><description>&lt;p&gt;This is the 25th post to &lt;span class="caps"&gt;RBP&lt;/span&gt; blog!  Hooray!&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ve been really happy with how things have gone so far.  Conversations have been for the most part civil and lively, we haven&amp;#8217;t run out of content, and the blog has seen new content nearly every week.&lt;/p&gt;
&lt;p&gt;I know that we aren&amp;#8217;t seeing as many voices on a week to week basis as we hoped for, but let&amp;#8217;s face it, people are bound to get busy.  That having been said, it&amp;#8217;d be great to know what readers are thinking about our content so far.&lt;/p&gt;
&lt;p&gt;So, what do you think?  Ranging from technical issues to ones of personal taste, it&amp;#8217;d be nice to know what keeps &lt;span class="caps"&gt;RBP&lt;/span&gt; in your feed reader, and what we can do to keep it there.  All feedback is welcome, so go ahead and let us know what&amp;#8217;s on your mind.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;ve got a great idea for a post, a series, or just some minor nitpick you want to voice, go for it.  So far we&amp;#8217;ve gained a lot from user responses, so I&amp;#8217;d like to keep with that tradition and keep the feedback loop tight.&lt;/p&gt;
&lt;p&gt;Now that you&amp;#8217;ve seen 25 posts, how should the next 25 look?&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 07 Jul 2009 05:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/010-twenty-five.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/010-twenty-five.html</guid></item><item><title>Code Blocks: Ruby's Swiss Army Knife</title><description>&lt;p&gt;&lt;b&gt;The following blog post is a direct excerpt from the &lt;a href="http://oreilly.com/catalog/9780596523008/"&gt;Ruby Best Practices&lt;/a&gt; book.  If you&amp;#8217;ve been enjoying this blog, you&amp;#8217;d probably love the book, so I&amp;#8217;ve decided to release some content here to give you a sense of what to expect.  Enjoy!&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;: For those coming from other languages, Ruby&amp;#8217;s &lt;a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29#Blocks_and_iterators"&gt;code blocks&lt;/a&gt; are inherently &lt;a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29"&gt;closures&lt;/a&gt; , and provide syntactic sugar for methods that accept &lt;tt&gt;Proc&lt;/tt&gt; objects (Ruby&amp;#8217;s anonymous functions).  While not strictly necessary for understanding this article, a solid grasp on what closures are and how they work will take you far.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;In Ruby, code blocks are everywhere. If you’ve ever used &lt;tt&gt;Enumerable&lt;/tt&gt;, you’ve worked with blocks. But what are they? Are they simply iterators, working to abstract away our need for the for loop? They certainly do a good job of that:&lt;/p&gt;
&lt;pre&gt;
  &amp;gt;&amp;gt; ["Blocks","are","really","neat"].map { |e| e.upcase } 
  =&amp;gt; ["BLOCKS", "ARE", "REALLY", "NEAT"]
&lt;/pre&gt;
&lt;p&gt;But other blocks don’t really iterate over collections—they just do helpful things for us. For example, they allow us to write something like:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  File.open("foo.txt","w") { |f| f &amp;lt;&amp;lt; "This is sexy" } 
&lt;/pre&gt;
&lt;p&gt;instead of forcing us to write this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  file = File.open("foo.txt","w") 
  file &amp;lt;&amp;lt; "This is tedious" 
  file.close 
&lt;/pre&gt;
&lt;p&gt;So blocks are useful for iteration, and also useful for injecting some code between pre-processing and post-processing operations in methods. But is that all they’re good for? Sticking with Ruby built-ins, we find that isn’t the case. Blocks can also shift our scope temporarily, giving us easier access to places we want to be:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
 "This is a string".instance_eval do 
    "O hai, can has reverse? #{reverse}. kthxbye" 
  end 
  #=&amp;gt; "O hai, can has reverse? gnirts a si sihT. kthxbye" 
&lt;/pre&gt;

&lt;p&gt;But blocks aren’t necessarily limited to code that gets run right away and then disappears. They can also form templates for what to do down the line, springing to action when called for:&lt;/p&gt;
&lt;pre&gt;
  &amp;gt;&amp;gt; foo = Hash.new { |h,k| h[k] = [] } 
  =&amp;gt; {} 
  &amp;gt;&amp;gt; foo[:bar] 
  =&amp;gt; [] 
  &amp;gt;&amp;gt; foo[:bar] &amp;lt;&amp;lt; 1 &amp;lt;&amp;lt; 2 &amp;lt;&amp;lt; 3 
  =&amp;gt; [1, 2, 3] 
  &amp;gt;&amp;gt; foo[:baz] 
  =&amp;gt; [] 
&lt;/pre&gt;
&lt;p&gt;So even if we label all methods that accept a block as iterators, we know the story runs deeper than that. With this in mind, we can leverage some basic techniques to utilize any of the approaches shown here, as well as some more advanced tricks. By doing things in a way that is consistent with Ruby itself, we can make life easier for our users. Rather than piling on new concepts, we can allow them to reuse their previous knowledge. Let’s take a look at a few examples of how to do that now.&lt;/p&gt;
&lt;h3&gt;Working with Enumerable&lt;/h3&gt;
&lt;p&gt;The most common use of blocks in Ruby might be the most trivial. The following class implements a basic sorted list, and then mixes in the &lt;tt&gt;Enumerable&lt;/tt&gt; module. The block magic happens in &lt;tt&gt;each()&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class SortedList 
    include Enumerable 
  
    def initialize 
      @data = [] 
    end 
  
    def &amp;lt;&amp;lt;(element) 
      (@data &amp;lt;&amp;lt; element).sort! 
    end 
  
    def each 
      @data.each { |e| yield(e) } 
    end 
  end
&lt;/pre&gt;
&lt;p&gt;Our &lt;tt&gt;each()&lt;/tt&gt; method simply walks over each element in our &lt;tt&gt;@data&lt;/tt&gt; array and passes it through the block provided to the method by calling yield. The resulting iterator works exactly the same as &lt;tt&gt;Array#each&lt;/tt&gt; and &lt;tt&gt;Hash#each&lt;/tt&gt; and all the Ruby built-ins, and indeed simply wraps &lt;tt&gt;Array#each&lt;/tt&gt; in this case:&lt;/p&gt;
&lt;pre&gt;
  &amp;gt;&amp;gt; a = SortedList.new 
  =&amp;gt; #&amp;lt;SortedList:0x5f0e74 @data=[]&amp;gt; 
  &amp;gt;&amp;gt; a &amp;lt;&amp;lt; 4 
  =&amp;gt; [4] 
  &amp;gt;&amp;gt; a &amp;lt;&amp;lt; 5 
  =&amp;gt; [4, 5] 
  &amp;gt;&amp;gt; a &amp;lt;&amp;lt; 1 
  =&amp;gt; [1, 4, 5] 
  &amp;gt;&amp;gt; a &amp;lt;&amp;lt; 7 
  =&amp;gt; [1, 4, 5, 7] 
  &amp;gt;&amp;gt; a &amp;lt;&amp;lt; 3 
  =&amp;gt; [1, 3, 4, 5, 7] 
  &amp;gt;&amp;gt; x = 0 
  =&amp;gt; 0 
  &amp;gt;&amp;gt; a.each { |e| x += e } 
  =&amp;gt; [1, 3, 4, 5, 7] 
  &amp;gt;&amp;gt; x 
  =&amp;gt; 20
&lt;/pre&gt;
&lt;p&gt;This shouldn’t be surprising. What is really the interesting bit is that by including the module &lt;tt&gt;Enumerable&lt;/tt&gt;, we gain access to most of the other features we’re used to working with when processing Ruby’s built-in collections. Here are just a few examples:&lt;/p&gt;
&lt;pre&gt;
  &amp;gt;&amp;gt; a.map { |e| "Element #{e}" } 
  =&amp;gt; ["Element 1", "Element 3", "Element 4", "Element 5", "Element 7"] 
  &amp;gt;&amp;gt; a.inject(0) { |s,e| s + e } 
  =&amp;gt; 20 
  &amp;gt;&amp;gt; a.to_a 
  =&amp;gt; [1, 3, 4, 5, 7] 
  &amp;gt;&amp;gt; a.select { |e| e &amp;gt; 3 } 
  =&amp;gt; [4, 5, 7] 
&lt;/pre&gt;
&lt;p&gt;In a lot of cases, the features provided by &lt;tt&gt;Enumerable&lt;/tt&gt; will be more than enough for traversing your data. However, it’s often useful to add additional features that build on top of the the &lt;tt&gt;Enumerable&lt;/tt&gt; methods. We can show this by adding a simple reporting method to &lt;tt&gt;SortedList&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class SortedList 
    def report(head) 
      header = "#{head}\n#{'-'*head.length}" 
      body = map{|e| yield(e)}.join("\n") + "\n" 
      footer = "This report was generated at #{Time.now}\n" 
      [header, body, footer].join("\n") 
    end 
  end
&lt;/pre&gt;
&lt;p&gt;which, when run, produces output like this:&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; puts a.report("So many fish") { |e| "#{e} fish" } 
  So many fish 
  ------------ 
  1 fish 
  3 fish 
  4 fish 
  5 fish 
  7 fish 
  This report was generated at 2008-07-22 22:47:20 -0400 
&lt;/pre&gt;
&lt;p&gt;Building custom iterators is really that simple. This provides a great deal of flexibility, given that the code block can execute arbitrary expressions and do manipulations of its own as it walks across the elements. But as mentioned before, blocks can be used for more than just iteration.&lt;/p&gt;
&lt;h3&gt;Using Blocks to Abstract Pre- and Postprocessing&lt;/h3&gt;
&lt;p&gt;We looked at the block form of File.open() as an example of how blocks can provide an elegant way to avoid repeating tedious setup and teardown steps. However, files are surely not the only resources that need to be properly managed. Network I/O via sockets is another place where this technique can come in handy.&lt;/p&gt;
&lt;p&gt;On the client side, we’d like to be able to create a method that allows us to send a message to a server, return its response, then cleanly close the connection. The first thing that comes to mind is something simple like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  require "socket" 

  class Client 

    def initialize(ip="127.0.0.1",port=3333) 
      @ip, @port = ip, port 
    end 

    def send_message(msg) 
      socket = TCPSocket.new(@ip,@port) 
      socket.puts(msg) 
      response = socket.gets 
    ensure 
      socket.close 
    end 
  
  end
&lt;/pre&gt;
&lt;p&gt;This is reasonably straightforward, but what happens when we want to add another method that waits to receive a message back from the server?&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  require "socket" 
  
  class Client 
    
    def initialize(ip="127.0.0.1",port=3333) 
      @ip, @port = ip, port 
    end 
    
    def send_message(msg) 
      socket = TCPSocket.new(@ip,@port) 
      socket.puts(msg) 
      response = socket.gets 
    ensure 
      socket.close 
    end 
    
    def receive_message 
      socket = TCPSocket.new(@ip,@port) 
      response = socket.gets 
    ensure 
      socket.close 
    end 
  end
&lt;/pre&gt;
&lt;p&gt;This is starting to look messy, as we have repeated most of the code between &lt;tt&gt;send_message&lt;/tt&gt; and &lt;tt&gt;receive_message&lt;/tt&gt;. Ordinarily, we’d break off the shared code into a private method that the two could share, but the trouble here is that the difference between these two methods is in the middle, not in a single extractable chunk. This is where blocks come to the rescue:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  require "socket" 

  class Client 
    def initialize(ip="127.0.0.1",port=3333) 
      @ip, @port = ip, port 
    end 
  
    def send_message(msg) 
      connection do |socket| 
        socket.puts(msg) 
        socket.gets 
      end 
    end 

    def receive_message 
      connection { |socket| socket.gets } 
    end
     
    private 
    
    def connection 
      socket = TCPSocket.new(@ip,@port) 
      yield(socket) 
    ensure 
      socket.close 
    end 
  
  end
&lt;/pre&gt;
&lt;p&gt;As you can see, the resulting code is a lot cleaner. As long as we use our &lt;tt&gt;connection()&lt;/tt&gt; method with a block, we won’t need to worry about opening and closing the &lt;tt&gt;TCPSocket&lt;/tt&gt;; it’ll handle that for us. This means we’ve captured that logic in one place, and can reuse it however we’d like.&lt;/p&gt;
&lt;p&gt;To make things a bit more interesting, let’s take a look at a simple server with which this code can interact, which gives us a chance to look at yet another way that blocks can be useful in interface design.&lt;/p&gt;
&lt;h3&gt;Blocks as Dynamic Callbacks&lt;/h3&gt;
&lt;p&gt;There is a lot of power in being able to pass around code blocks just like they were any other object. This allows for the capability of creating and storing dynamic callbacks, which can later be looked up and executed as needed.&lt;/p&gt;
&lt;p&gt;In order to play with our &lt;tt&gt;Client&lt;/tt&gt; code from the previous example, we’re going to create a trivial &lt;tt&gt;TCPServer&lt;/tt&gt; that attempts to match incoming messages against patterns to determine how it should respond. Rather than hardcoding behaviors into the server itself or relying on inheritance to handle responses, we will instead allow responses to be defined through ordinary method calls accompanied by a block. Our goal is to get an interface that looks like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  server = Server.new 

  server.handle(/hello/i) { "Hello from server at #{Time.now}" } 
  server.handle(/goodbye/i) { "Goodbye from server at #{Time.now}" } 
  server.handle(/name is (\w+)/) { |m| "Nice to meet you #{m[1]}!" } 

  server.run
&lt;/pre&gt;
&lt;p&gt;The first two examples are fairly simple, matching a single word and then responding with a generic message and timestamp. The third example is a bit more interesting, repeating the client’s name back in the response message. This will be accomplished by querying a simple &lt;tt&gt;MatchData&lt;/tt&gt; object, which is yielded to the block.&lt;/p&gt;
&lt;p&gt;Though making this work might seem like black magic to the uninitiated, a look at its implementation reveals that it is actually a fairly pedestrian task:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class Server 
    
    def initialize(port=3333) 
      @server   = TCPServer.new('127.0.0.1',port) 
      @handlers = {} 
    end 
    
    def handle(pattern, &amp;amp;block) 
      @handlers[pattern] = block 
    end 
    
    def run 
      while session = @server.accept 
        msg = session.gets 
        match = nil 
        @handlers.each do |pattern,block| 
          if match = msg.match(pattern) 
            break session.puts(block.call(match)) 
          end 
        end 
        unless match 
          session.puts "Server received unknown message: #{msg}" 
        end 
      end 
    end
     
  end
&lt;/pre&gt;
&lt;p&gt;The &lt;tt&gt;handle()&lt;/tt&gt; method slurps up the provided block using the &lt;tt&gt;&amp;amp;block&lt;/tt&gt; syntax, and stores it in a hash keyed by the given pattern. When &lt;tt&gt;Server#run&lt;/tt&gt; is called, an endless loop is started that waits for and handles client connections. Each time a message is received, the hash of handlers is iterated over. If a pattern is found that matches the message, the associated block is called, providing the match data object so that the callback can respond accordingly.&lt;/p&gt;
&lt;p&gt;If you’d like to try this out, use the following code to spin up a server:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  server = Server.new 
  server.handle(/hello/i) { "Hello from server at #{Time.now}" } 
  server.handle(/goodbye/i) { "Goodbye from server at #{Time.now}" } 
  server.handle(/name is (\w+)/) { |m| "Nice to meet you #{m[1]}!" } 
  server.run
&lt;/pre&gt;
&lt;p&gt;Once you have that running and listening for connections, execute the following client code:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  client = Client.new 
  
  ["Hello", "My name is Greg", "Goodbye"].each do |msg| 
    response = client.send_message(msg) 
    puts response 
  end 
&lt;/pre&gt;
&lt;p&gt;You will get back something like this:&lt;/p&gt;
&lt;pre&gt;
  Hello from server at Wed Jul 23 16:15:37 -0400 2008 
  Nice to meet you Greg! 
  Goodbye from server at Wed Jul 23 16:15:37 -0400 2008 
&lt;/pre&gt;
&lt;p&gt;It would be easy to extend both the client and server to do more interesting things that build on this very simple foundation. Feel free to take a few minutes to play around with that, and then we’ll look at one more block trick that’s fairly common in Ruby.&lt;/p&gt;
&lt;h3&gt;Blocks for Interface Simplification&lt;/h3&gt;
&lt;p&gt;Does it feel like the word “server” is written too many times in this code?&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  server = Server.new 
  server.handle(/hello/i) { "Hello from server at #{Time.now}" } 
  server.handle(/goodbye/i) { "Goodbye from server at #{Time.now}" } 
  server.handle(/name is (\w+)/) { |m| "Nice to meet you #{m[1]}!" } 
  server.run 
&lt;/pre&gt;
&lt;p&gt;When you see code like this, it might be a sign that you could do better. Although there are merits to this somewhat standard approach, we can cheat a little bit with blocks (of course) and make things prettier. It would be nice to be able to write this instead:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  Server.run do 
    handle(/hello/i) { "Hello from server at #{Time.now}" } 
    handle(/goodbye/i) { "Goodbye from server at #{Time.now}" } 
    handle(/name is (\w+)/) { |m| "Nice to meet you #{m[1]}!" } 
  end 
&lt;/pre&gt;
&lt;p&gt;As you may recall from an earlier example, it is possible to execute a block within the scope of an instantiated object in Ruby. Using this knowledge, we can implement this handy shortcut interface as a simple class method:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class Server 
    # other methods same as before 
    def self.run(port=3333,&amp;amp;block) 
      server = Server.new(port) 
      server.instance_eval(&amp;amp;block) 
      server.run 
    end 
  end
&lt;/pre&gt;
&lt;p&gt;This is all you need to get the new interface running, and rounds off our quick exploration of the different ways that you can use blocks to improve your &lt;span class="caps"&gt;API&lt;/span&gt; design while simplifying your method implementations.  Let&amp;#8217;s recap with a few tips before we wrap up here.&lt;/p&gt;
&lt;h3&gt;Things to Remember&lt;/h3&gt;
&lt;p&gt;Keep the following things in mind when using blocks as part of your interface:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;If you create a collection class that you need to traverse, build on top of Enumerable rather than reinventing the wheel.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;If you have shared code that differs only in the middle, create a helper method that yields a block in between the pre/postprocessing code to avoid duplication of effort.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;If you use the &amp;amp;block syntax, you can capture the code block provided to a method inside a variable. You can then store this and use it later, which is very useful for creating dynamic callbacks.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Using a combination of &amp;amp;block and instance_eval, you can execute blocks within the context of arbitrary objects, which opens up a lot of doors for highly customized interfaces.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;The return value of yield (and block.call) is the same as the return value of the provided block.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Did you enjoy this post?&lt;/h3&gt;
&lt;p&gt;If you had fun reading this, definitely consider picking up a copy of &lt;a href="http://oreilly.com/catalog/9780596523008/"&gt;Ruby Best Practices&lt;/a&gt; .  If you need more convincing, there is a whole &lt;a href="http://cdn.oreilly.com/books/9780596523008/Mastering_the_Dynamic_Toolkit.xml.pdf"&gt;sample chapter&lt;/a&gt; for you to check out in addition to this excerpt.  If you&amp;#8217;ve already picked up the book and are enjoying it, tell your friends, and consider leaving a review on the &lt;a href="http://www.amazon.com/gp/product/0596523009"&gt;Amazon product page&lt;/a&gt; .  If you don&amp;#8217;t have enough time for that, go help me win the &lt;a href="http://rubytrends.com/styles/book/trends/134"&gt;popularity contest&lt;/a&gt; over at RubyTrends, as there are several books that I admire that still need to be overcome :)&lt;/p&gt;
&lt;p&gt;As always, I welcome your thoughts and feedback.  Let me know what you think of the techniques shown here, or share your own personal favorite code block trick.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 07 Jul 2009 04:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/009-beautiful-blocks.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/009-beautiful-blocks.html</guid></item><item><title>The Decorator Delegator Disco</title><description>&lt;p&gt;As promised, we&amp;#8217;re back with more design discussion and hopefully some interesting ideas.  Let&amp;#8217;s start with a quick recap of what has happened so far.&lt;/p&gt;
&lt;p&gt;First, &lt;a href="http://sandimetz.com/"&gt;Sandi Metz&lt;/a&gt; posted on her blog about &lt;a href="http://sandimetz.com/2009/06/ruby-case-statements-and-kindof.html"&gt;type specific coupling&lt;/a&gt; that a &lt;tt&gt;case&lt;/tt&gt; statement can introduce.  Her proposed solution encouraged us to pass the responsibilities down to the individual objects, which is certainly desirable.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.rubybestpractices.com/about/aaronp.html"&gt;Aaron Patterson&lt;/a&gt; got in the mix, pointing out the issues that can happen when you are too aggressive about adding methods to core Ruby.  When Sandi suggested that &lt;a href="http://en.wikipedia.org/wiki/Double_dispatch"&gt;Double Dispatch&lt;/a&gt; might be a way around this problem, Aaron responded with an &lt;a href="http://blog.rubybestpractices.com/posts/aaronp/001_double_dispatch_dance.html"&gt;implementation using the visitor pattern&lt;/a&gt; to accomplish this sort of thing.&lt;/p&gt;
&lt;p&gt;I had not seen Aaron&amp;#8217;s initial work before setting out to solve the problem on my own, and ended up taking an entirely different path to get there.  I&amp;#8217;m not much of a patterns guy, but after I had a working implementation, it seems like the approach I took mostly represents a combination of &lt;a href="http://en.wikipedia.org/wiki/Delegation_pattern"&gt;Delegation&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decoration&lt;/a&gt; techniques.&lt;/p&gt;
&lt;h3&gt;A quick demo&lt;/h3&gt;
&lt;p&gt;I think this code will be easier to understand if I show how it is used right up front.  So here&amp;#8217;s a tiny slice to get started with:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  module ActionView
    module Helpers 
       class Extensions
          extend Decoration
        
          decorator_for(Array) do
            def as_array
              self
            end
          end
        
          decorator_for(Object) do
            def as_array
              [self]
            end
          end
       end
     
       # totally incomplete, just returns decorated object
       def form_for(record_or_name_or_array, *args, &amp;amp;block)
         Extensions.decorate(record_or_name_or_array)
       end
    end
  end

  include ActionView::Helpers

  p form_for(:foo).as_array #=&amp;gt; [:foo]
  p form_for(Object.new).as_array #=&amp;gt; [#&amp;lt;Object:0x3f47ec&amp;gt;]
  p form_for([1,2,3]).as_array #=&amp;gt; [1,2,3]
&lt;/pre&gt;
&lt;p&gt;In the code above, I&amp;#8217;ve implemented Sandi&amp;#8217;s &lt;tt&gt;as_array&lt;/tt&gt; helper.  I left the actual &lt;tt&gt;form_for&lt;/tt&gt; implementation very incomplete, causing it just to return the decorated object.  This is for example purposes only, so you could see that we can indeed call &lt;tt&gt;as_array&lt;/tt&gt; successfully.&lt;/p&gt;
&lt;p&gt;Before we check out how this all works, here are two extra hints about what this approach is affording us.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  my_obj = [1,2,3]
  decorated = form_for(my_obj)
  
  p decorated.as_array #=&amp;gt; [1,2,3]
  
  # Raises NoMethodError
  [1,2,3].as_array
  
  # Also raises NoMethodError
  p my_obj.as_array 
&lt;/pre&gt;
&lt;p&gt;As you can see, if core extensions are like a meat cleaver, this approach is more like using a scalpel, no changes bleed out into unexpected places.  Now that we see how it is used, let&amp;#8217;s take a look under the hood of &lt;tt&gt;Decorate&lt;/tt&gt; to see how it is built.&lt;/p&gt;
&lt;h3&gt;The &lt;tt&gt;Decorate&lt;/tt&gt; module&lt;/h3&gt;
&lt;p&gt;Though fairly densely packed, the whole implementation of &lt;tt&gt;Decorate&lt;/tt&gt; is pretty tiny.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require 'delegate'

# with minor tweaks from tjstankus
module Decoration
  def decorator_for(*types, &amp;amp;block)
    types.each do |type|
      decorators[type] = Module.new(&amp;amp;block)
    end
  end
 
  def decorators
    @decorators ||= {}
  end
 
  def decorate(target)
    obj = SimpleDelegator.new(target)
    
    # walk in reverse order so most specific patches get applied LAST
    target.class.ancestors.reverse.each do |a|
      if decorators[a]
        obj.extend(decorators[a])
      end
    end
    
    return obj
  end
end
&lt;/pre&gt;
&lt;p&gt;Here, we are taking advantage of &lt;tt&gt;SimpleDelegator&lt;/tt&gt;, a class provided from Ruby&amp;#8217;s &lt;i&gt;delegate&lt;/i&gt; standard library.  Here&amp;#8217;s a quick glimpse of how it works:&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; require "delegate"
=&amp;gt; true

&amp;gt;&amp;gt; a = [1,2,3]
=&amp;gt; [1, 2, 3]

&amp;gt;&amp;gt; b = SimpleDelegator.new(a)
=&amp;gt; [1, 2, 3]
&amp;gt;&amp;gt; b &amp;lt;&amp;lt; 4
=&amp;gt; [1, 2, 3, 4]
&amp;gt;&amp;gt; b.delete_at(1)
=&amp;gt; 2

&amp;gt;&amp;gt; b
=&amp;gt; [1, 3, 4]
&amp;gt;&amp;gt; a
=&amp;gt; [1, 3, 4]

&amp;gt;&amp;gt; b.class
=&amp;gt; SimpleDelegator
&amp;gt;&amp;gt; a.class
=&amp;gt; Array
&lt;/pre&gt;
&lt;p&gt;Essentially, &lt;tt&gt;SimpleDelegator&lt;/tt&gt; provides an easy way to wrap an object within a proxy.  This gives us a convenient place to hang per-object behavior without mutating the original object.&lt;/p&gt;
&lt;pre&gt;
  &amp;gt;&amp;gt; def b.kitties
  &amp;gt;&amp;gt;   "MEEEOW"
  &amp;gt;&amp;gt; end
  &amp;gt;&amp;gt; b.kitties
  =&amp;gt; "MEEEOW"
  &amp;gt;&amp;gt; a.kitties
  NoMethodError: undefined method `kitties' for [1, 3, 4]:Array
  	from (irb):15
&lt;/pre&gt;
&lt;p&gt;You probably see where this is going now, so let&amp;#8217;s get back to &lt;tt&gt;Decorate&lt;/tt&gt; module.  If we look at &lt;tt&gt;decorator_for&lt;/tt&gt;, we can get a good sense of how all this magic comes together:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def decorator_for(*types, &amp;amp;block)
  types.each do |type|
    decorators[type] = Module.new(&amp;amp;block)
  end
end

def decorators
  @decorators ||= {}
end
&lt;/pre&gt;
&lt;p&gt;Here, we see that &lt;tt&gt;decorator_for&lt;/tt&gt; basically takes a type or list of types, and a block.  The block is then used to define the body of an anonymous module that gets stashed away in a hash for later use.  So going back to our example code, we see that the following definitions are actually module definitions.  They could have even been written this way:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Extensions
   extend Decoration
 
   module ArrayDecorator
     def as_array
       self
     end
   end
 
  module ObjectDecorator
     def as_array
       [self]
     end
  end
  
  decorators[Array]  = ArrayDecorator
  decorators[Object] = ObjectDecorator
end
&lt;/pre&gt;
&lt;p&gt;Of course, this lacks the necessary sugar shock, so I show it only to make the implementation clearer.  But now that we have a way to stack up modules with per-object behavior in them, and a place to hang that per-object behavior off of, we only need a mechanism to apply it. For this functionality, we turn to the &lt;tt&gt;decorate&lt;/tt&gt; method:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def decorate(target)
    obj = SimpleDelegator.new(target)
  
    # walk in reverse order so most specific patches get applied LAST
    target.class.ancestors.reverse.each do |a|
      if decorators[a]
        obj.extend(decorators[a])
      end
    end
  
    return obj
  end
&lt;/pre&gt;
&lt;p&gt;We want to be able to preserve the ability to use &lt;tt&gt;super&lt;/tt&gt; in our decorated method calls, and we definitely wanted to pick up extensions to objects higher in the inheritance chain without any duplication.  So this meant we needed to do something a little more complicated than key the target object&amp;#8217;s type directly to a decorator module.&lt;/p&gt;
&lt;p&gt;Rather than just picking the most recent ancestor, this code walks the chain in reverse order so that the modules can neatly stack on top of one another.  In short, this means that the decorator for &lt;tt&gt;Object&lt;/tt&gt; gets applied before the one for &lt;tt&gt;Array&lt;/tt&gt; so that you can emulate an inheritance relationship in your extensions.&lt;/p&gt;
&lt;p&gt;This pretty much sums up the whole implementation, so let&amp;#8217;s just take a look at one more example before evaluating the pros and cons of this approach.&lt;/p&gt;
&lt;h3&gt;An alternative to core extensions&lt;/h3&gt;
&lt;p&gt;Here is a bit of code that completely covers the &lt;a href="http://github.com/skmetz/rails/blob/cffbc4399288be2fb7e15f67680089dbb36a7152/actionpack/lib/action_view/helpers/form_helper_core_extensions.rb"&gt;core extensions&lt;/a&gt; that Sandi needed for her &lt;a href="http://github.com/skmetz/rails/commit/cffbc4399288be2fb7e15f67680089dbb36a7152"&gt;Rails patch&lt;/a&gt;, without the core extensions:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;   
  class Extensions

     extend Decoration
     
     decorator_for Symbol, String do
       def form_object_name
         self
       end

       def as_fields_for_form_object(args)
         args.first
       end

       def acts_like_model?
         false
       end
     end

     decorator_for Array do
       def as_form_object
         last
       end

       def as_array
         self
       end
     end

     decorator_for Object do
       def as_form_object
         self
       end

       def as_fields_for_form_object(args)
         as_form_object
       end

       def form_object_name
         ActionController::RecordIdentifier.singular_class_name(self)
       end

       def as_array
         [self]
       end

       def acts_like_model?
         true
       end
     end
     
  end
&lt;/pre&gt;
&lt;p&gt;You would use the same approach I suggested before to wire this up, something like &lt;tt&gt;Extensions.decorate(my_obj)&lt;/tt&gt;&lt;/p&gt;
&lt;h3&gt;Pros for this solution&lt;/h3&gt;
&lt;p&gt;There are definitely things about this technique that I like.  The obvious benefit is that it prevents core hackery, and even goes a step beyond something like &lt;tt&gt;my_object.extend(SomethingSpecial)&lt;/tt&gt;, since it only extends behavior into a throwaway proxy object.&lt;/p&gt;
&lt;p&gt;Another clear benefit is that defining extensions is about as easy as it is to do raw core extensions.  Because the blocks are used as module definitions, you can do pretty much anything you&amp;#8217;d ordinarily do when defining methods.  This lowers the amount of &amp;#8216;special stuff&amp;#8217; you need to remember.&lt;/p&gt;
&lt;p&gt;Perhaps the most interesting thing however, is that this approach can be used to apply customized behavior to several types of objects at once, and can also be used to apply behaviors based on what mixins are included in an object.  This means there is nothing stopping you from doing something like &lt;tt&gt;decorator_for(Enumerable)&lt;/tt&gt; or &lt;tt&gt;decorator_for(Comparable)&lt;/tt&gt;, which provides you a little extra flexibility beyond the traditional inheritance hierarchy.&lt;/p&gt;
&lt;p&gt;Another interesting aspect of this is that decorators can be implemented at a per-module (or per-class) level, which means that different libraries can have different extensions without interfering with each other.&lt;/p&gt;
&lt;p&gt;Keep in mind, it&amp;#8217;s not all roses.  Let&amp;#8217;s take a look at some of the pitfalls.&lt;/p&gt;
&lt;h3&gt;Cons for this solution&lt;/h3&gt;
&lt;p&gt;A major drawback here is that we&amp;#8217;re still relying on types.  Whether its a class or module, we still expect an object to be a certain thing, which sort of goes against traditional duck typing in which we&amp;#8217;d rather make assumptions based on what an object can do rather than what it is.  So it might be nicer to apply these decorators based on what the target object responds to, not what ancestors it has.&lt;/p&gt;
&lt;p&gt;There is also the performance overhead factor.  I have not benchmarked yet, but I suspect that the cost of both building up a &lt;tt&gt;SimpleDelegator&lt;/tt&gt; and doing proxy calls is non-negligible.  While this may not be an issue in many cases, for code that needs to run in tight loop or be scaled heavily, this approach may be too heavy for its benefits to outweigh its costs.  But of course, this is a huge &lt;span class="caps"&gt;YMMV&lt;/span&gt; situation, and without hard numbers it&amp;#8217;s tough to say to what extent this would be a huge issue.&lt;/p&gt;
&lt;p&gt;Perhaps the most fatal flaw with the current setup is that &lt;tt&gt;respond_to?&lt;/tt&gt; gets delegated to the target object, so with the current implementation, unless you define &lt;tt&gt;respond_to?&lt;/tt&gt; yourself, it will not pick up any of your new methods.  I imagine that this is something that can be fixed, I just have not looked into it yet.&lt;/p&gt;
&lt;p&gt;Finally, there is the legitimacy problem.  As Sandi said in the comments on Aaron&amp;#8217;s article, &lt;a href="http://blog.rubybestpractices.com/posts/aaronp/001_double_dispatch_dance.html#comment-11553586"&gt;sometimes a case statement is just a case statement&lt;/a&gt;.  If we go around filling all our code with high-architecture bazookas when all that was needed was a pen-knife, there are bound to be consequences.  Is this overkill or awesome?  I don&amp;#8217;t really know, and I imagine it depends on the situation.&lt;/p&gt;
&lt;h3&gt;Conclusions&lt;/h3&gt;
&lt;p&gt;When Aaron and I compared our solutions to Sandi&amp;#8217;s problem, we basically concluded that both of our approaches might be useful for different things.  Aaron&amp;#8217;s shines when dealing with providing type-specific implementations of a particular behavior, which is mostly just the visitor pattern shining through.  My approach is probably better for situations in which you need to collect several behaviors in one place.&lt;/p&gt;
&lt;p&gt;Ultimately, this may be a matter of preference, depending on whether you want to abstract things based on the method level or the object level.  Of course, sometimes neither is necessary, and that may be an important fact to remember.&lt;/p&gt;
&lt;p&gt;I am very much interested in what readers have to say.  Please check out &lt;a href="http://sandimetz.com/2009/06/ruby-case-statements-and-kindof.html"&gt;Sandi&amp;#8217;s article&lt;/a&gt; , then &lt;a href="http://blog.rubybestpractices.com/posts/aaronp/001_double_dispatch_dance.html"&gt;Aaron&amp;#8217;s&lt;/a&gt; , then let me know here which approaches you might take along with their context.  If you have your own ideas that are totally different than ours, I want to know about that too.&lt;/p&gt;
&lt;p&gt;We need more conversations like these in our community, so let&amp;#8217;s start here and see where it takes us.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Sat, 27 Jun 2009 16:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/008-decorator-delegator-disco.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/008-decorator-delegator-disco.html</guid></item><item><title>Quack Attack: Making Your Code More Rubyish</title><description>&lt;p&gt;I&amp;#8217;ve been doing some &lt;span class="caps"&gt;FFI&lt;/span&gt; work recently, which means that I&amp;#8217;ve needed to  wrap underlying C libraries so that I can call them from Ruby.  While I won&amp;#8217;t get into how the low level wrappers work, I can show you what the raw &lt;span class="caps"&gt;API&lt;/span&gt; calls look like for just a few functions:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  trip = API.PCMSNewTrip(server_id)
  API.PCMSNumStops(trip)
  API.PCMSAddStop(trip, string)
  API.PCMSGetStop(trip, index, string_ptr, buffer_size)
  API.PCMSDeleteStop(trip, index)
  API.PCMSClearStop(trip)
&lt;/pre&gt;
&lt;p&gt;All things considered, this looks pretty good for a direct wrapper on top of a C library.  In fact, it&amp;#8217;s relatively simple to mirror this to a more normalized Ruby layout.  We can start by noticing that these calls are basically object oriented, focusing on the &lt;tt&gt;Trip&lt;/tt&gt; object.  While a &lt;tt&gt;Trip&lt;/tt&gt; has other responsibilities, among them is managing a list of stops.  With this in mind, we can flesh out a basic &lt;tt&gt;Trip&lt;/tt&gt; object:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Trip
  def initialize(server_id)
    @server_id = server_id
    @pointer = API.PCMSNewTrip(@server_id)
    @stops = StopList.new(self)
  end
  
  attr_reader :stops
  
  def call(*args)
    API.send("PCMS#{args.first}",  @pointer, *args[1..-1])
  end
end
&lt;/pre&gt;
&lt;p&gt;The &lt;tt&gt;Trip#call&lt;/tt&gt; helper removes some of the duplication for us, but it&amp;#8217;ll be easier to see how it works in just a moment.  For now, it&amp;#8217;s worth pondering what a &lt;tt&gt;StopList&lt;/tt&gt; should be.&lt;/p&gt;
&lt;p&gt;If you look at the functions listed for dealing with stops, you&amp;#8217;ll notice they map nicely to one of Ruby&amp;#8217;s structures.  We&amp;#8217;re dealing with an ordered list of objects that can be added to and removed from.  It can also be queried for its length, and deleted entirely.  These features sure sound a lot like Ruby&amp;#8217;s &lt;tt&gt;Array&lt;/tt&gt; object, don&amp;#8217;t they?&lt;/p&gt;
&lt;p&gt;With this in mind, let&amp;#8217;s do a quick experiment in interface design:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class StopList

  include Enumerable

  def initialize(trip)
    @trip = trip
  end

  def length
    @trip.call :NumStops
  end

  def &amp;lt;&amp;lt;(loc)
    @trip.call :AddStop, loc
  end

  def [](index)
    ptr = FFI::MemoryPointer.new(:char, 100)
    @trip.call :GetStop, index, ptr, 101
    ptr.read_string
  end

  def delete_at(index)
    @trip.call :DeleteStop, index
  end

  def each
    length.times do |i|
      yield self[i]
    end
  end

  def clear
    @trip.call :ClearStops
  end

end
&lt;/pre&gt;
&lt;p&gt;Without paying too much attention to the implementation details, let&amp;#8217;s take a look at what behaviors this new object supports:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  t = Trip.new(server_id)
  
  t.stops &amp;lt;&amp;lt; "New Haven, CT"
  t.stops &amp;lt;&amp;lt; "Naugatuck, CT"
  t.stops &amp;lt;&amp;lt; "Boston, MA"
  
  p t.stops.length #=&amp;gt; 3
  
  p t.stops[2] #=&amp;gt; "02205 Boston, MA, Suffolk"
  
  t.stops.delete_at(1)
  p t.stops[1] #=&amp;gt; "02205 Boston, MA, Suffolk"
  
  p t.stops.map { |e| e.upcase } #=&amp;gt; [ "06511 NEW HAVEN, CT, NEW HAVEN",
                                 #     "02205 BOSTON, MA, SUFFOLK" ]
  
  t.stops.clear
  p t.stops.length #=&amp;gt; 0
&lt;/pre&gt;
&lt;p&gt;If this sort of interaction looks familiar to you, it&amp;#8217;s because you&amp;#8217;ve likely already done things like this thousands of times.  But to make it blindingly obvious, let&amp;#8217;s replace Trip#stops with an Array.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
stops = []

stops &amp;lt;&amp;lt; "New Haven, CT"
stops &amp;lt;&amp;lt; "Naugatuck, CT"
stops &amp;lt;&amp;lt; "Boston, MA"

p stops.length #=&amp;gt; 3

p stops[2] #=&amp;gt; "Boston, MA"

stops.delete_at(1)
p stops[1] #=&amp;gt; "Boston, MA"

p stops.map { |e| e.upcase } #=&amp;gt; ["NEW HAVEN, CT", "BOSTON, MA"]

stops.clear
p stops.length #=&amp;gt; 0
&lt;/pre&gt;
&lt;p&gt;You&amp;#8217;ll notice that aside from lacking the location name normalization that our real code does implicitly, the key points we&amp;#8217;ve highlighted have exactly the same behavior, using exactly the same interface.  One benefit is immediately obvious after seeing this; the &lt;span class="caps"&gt;API&lt;/span&gt; for &lt;tt&gt;StopList&lt;/tt&gt; doesn&amp;#8217;t require you to learn anything new.&lt;/p&gt;
&lt;p&gt;A more subtle gain that comes with this approach is that so long as it is restricted to the subset which &lt;tt&gt;StopList&lt;/tt&gt; supports, code which expects an &lt;tt&gt;Array&lt;/tt&gt;-like thing does not need to change, either.&lt;/p&gt;
&lt;p&gt;For example, the following code will work just fine with either an &lt;tt&gt;Array&lt;/tt&gt; or a &lt;tt&gt;StopList&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def humanized(list)
  list.each_with_index do |e,i|
    puts "#{e} (#{i+1} / #{list.length})"
  end
end
&lt;/pre&gt;
&lt;p&gt;This makes things easier to test, and easier to be re-used for different purposes.  Both are solid reasons for using this technique.&lt;/p&gt;
&lt;p&gt;Of course, I&amp;#8217;ve been glossing over a semi-major issue in the original code here, which I am sure has frustrated our more pedantic readers.  The current &lt;tt&gt;StopList&lt;/tt&gt; code, while quite useful, does not quack perfectly with Array.  We needn&amp;#8217;t look far for signs of divergence.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
p t.stops &amp;lt;&amp;lt; "Chicago, IL" #=&amp;gt; 1
p t.stops.delete_at(2)     #=&amp;gt; 1
p t.stops.clear            #=&amp;gt; nil
&lt;/pre&gt;
&lt;p&gt;These side-effect bearing functions are returning their C based values, which are different than what you&amp;#8217;d expect from a Ruby &lt;tt&gt;Array&lt;/tt&gt;.  Luckily, each of these are easy to remedy.&lt;/p&gt;
&lt;p&gt;In the case of the append operator (&amp;lt;&amp;lt;), we should return the  &lt;tt&gt;StopList&lt;/tt&gt; itself, to permit chaining:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def &amp;lt;&amp;lt;(loc)
  @trip.call :AddStop, loc
  return self
end
&lt;/pre&gt;
&lt;p&gt;To play nice with &lt;tt&gt;Array&lt;/tt&gt;, our &lt;tt&gt;delete_at&lt;/tt&gt; method should return the deleted object:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def delete_at(index)
    obj = self[index]
    @trip.call :DeleteStop, index
    return obj
  end
&lt;/pre&gt;
&lt;p&gt;Finally, since &lt;tt&gt;clear&lt;/tt&gt; may also be chained, it should return the original object as well.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def clear
  @trip.call :ClearStops
  return self
end
&lt;/pre&gt;
&lt;p&gt;With these fixes in place, we can re-visit our previous example:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
p t.stops &amp;lt;&amp;lt; "Chicago, IL" #=&amp;gt; #&amp;lt;Trip::StopList:0x0fcac ...&amp;gt;
p t.stops.delete_at(2)     #=&amp;gt; "60607 Chicago, IL, Cook"
p t.stops.clear            #=&amp;gt; #&amp;lt;Trip::StopList:0x0fcac ...&amp;gt;
&lt;/pre&gt;
&lt;p&gt;There are probably some other minor details to catch, but now that our &lt;tt&gt;Array&lt;/tt&gt;-ish &lt;tt&gt;StopList&lt;/tt&gt; is &amp;#8220;Good Enough For Government Work&amp;#8221;, we have a nice stopping point.  Let&amp;#8217;s wrap things up with a little summary of things to remember.&lt;/p&gt;
&lt;h3&gt;Guidelines For Making Your Code More &amp;#8220;Rubyish&amp;#8221;&lt;/h3&gt;
&lt;p&gt;This is just one technique among many for improving your code, but I&amp;#8217;d argue its a fairly important one. If you have a structure that mimics a subset of a core Ruby object&amp;#8217;s capabilities, you can gain a lot by standardizing on a compatible interface.  While sometimes the similarities end at the &lt;tt&gt;Enumerable&lt;/tt&gt; and &lt;tt&gt;Comparable&lt;/tt&gt; mixins, it&amp;#8217;s reasonable to stretch things farther when it makes sense to do so.  If you go that route (as we did here), there are just a few things to keep in mind:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;You don&amp;#8217;t need to implement every last feature of a core Ruby object in order to use this technique.  So many functions rely on just a handful of available methods, that it makes sense to use this technique even when your problem domain is very small.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;For the features you do implement, take care to maintain the same interface both on input and output.  It&amp;#8217;s fine to not support certain use cases, or to add extensions for new ones, but you should not diverge in the behaviors you do implement unless you have a good reason to.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Pay close attention to the return values of side-effect inducing functions, especially the ones mentioned in this article.  Many Ruby methods are designed to be chainable, and breaking that feature can create a mess.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;While this technique opens the door for using primitive objects for testing higher level functions, do not forget to adequately test the functionality of the actual objects you are implementing.  Basically, make sure your code really quacks like a duck before substituting it with a duck somewhere else.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;i&gt;To our readers:  Where have you seen good uses of this technique in action?  Have you tried it yourself and run into any problems?  Please give your answers to these questions, as well as any other discussion points in the comments.&lt;/i&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 09 Jun 2009 13:00:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/007-rubyish-code.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/007-rubyish-code.html</guid></item><item><title>RubySpec as a Community Resource (Interview w. Brian Ford)</title><description>&lt;p&gt;Though I still need to collect the details, I&amp;#8217;m about to start on another major open source project, called &lt;a href="http://github.com/madriska/unity"&gt;Unity&lt;/a&gt; .  It will be a &lt;a href="http://pledgie.com/campaigns/4640"&gt;community driven effort&lt;/a&gt; to provide a nice web resource that builds on top of the &lt;a href="http://rubyspec.org/"&gt;RubySpec&lt;/a&gt; project with reporting and search capabilities.&lt;/p&gt;
&lt;p&gt;But readers might wonder what RubySpec has to do with them, if they are not language implementers.  Here&amp;#8217;s what &lt;a href="http://blog.brightredglow.com/"&gt;Brian Ford&lt;/a&gt; had to say.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;i&gt;&lt;h4&gt;In an ideal world, what would the RubySpec project do for the Ruby community?&lt;/h4&gt;&lt;/p&gt;
&lt;p&gt;1. Provide a reasonably precise definition of the Ruby programming language so that there can be a certain degree of uniformity among the implementations. The goal is for the Ruby user to not have to think about which implementation her code runs on until she needs to, rather than writing code that works on one implementation and porting it if necessary. All software has a life cycle and the needs of the software are not always constant. A Ruby programmer should be able to develop using &lt;span class="caps"&gt;MRI&lt;/span&gt; on their laptop and deploy on Rubinius or JRuby without missing a beat, for example. Note that I use &amp;#8220;Ruby user&amp;#8221; and &amp;#8220;Ruby programmer&amp;#8221; with a distinction. A Ruby user might be anyone who relies on or provides software written in Ruby, not just someone who writes software in Ruby. So I envision a uniform definition of Ruby benefiting every strata of the Ruby community.&lt;/p&gt;
&lt;p&gt;2. Provide quality assurance and regression notification for &lt;span class="caps"&gt;MRI&lt;/span&gt; and all implementations. People put a huge amount of trust in an implementation when they decide to invest large amounts of money in developing a software product. We&amp;#8217;d like them to get some assurance for the trust they put in. Programmers are human and make mistakes. Automating the detection of mistakes is a no-brainer.&lt;/p&gt;
&lt;p&gt;3. Improving the quality of Ruby as a language by discovering dusty corners and inconsistencies. Recently, a number of inconsistencies in bang methods (e.g. String#lstrip!) were discovered by one of the RubySpec contributors. Matz quickly fixed up the issues. (You can read the gory details here: http://redmine.ruby-lang.org/issues/show/1550). Ruby as a programming language certainly exists in Matz&amp;#8217;s brain. But Ruby as an implementation (&lt;span class="caps"&gt;MRI&lt;/span&gt;) exists in many developers&amp;#8217; brains. RubySpec is an attempt to expose all that knowledge and those assumptions in an objective manner.&lt;/p&gt;
&lt;p&gt;4. Experimentation with implementations and the Ruby language itself. I have recently implemented nearly a dozen variations of Hash for Rubinius to determine which algorithms perform best. In each case, I start by writing code that will pass the RubySpec Hash specs. The specs have been generalized to use a helper method rather than referencing the Hash class constant directly. This has been a huge benefit, especially in Rubinius where the core library Hash class is written entirely in Ruby. I can inherit from Hash and override any methods to write a completely new implementation that does not interfere with the existing Hash class. The RubySpecs also provide a huge safety net and testing ground for trying out new language features and seeing how they interact with existing features.&lt;/p&gt;
&lt;p&gt;5. A learning tool that provides Ruby programmers with concrete examples of code that demonstrates every single Ruby language feature. The Pickaxe book may have better prose, but when it comes to comprehensiveness, I want RubySpec to be number one. And while we have a lot of work to do to make the spec description strings better, you can already learn a lot about Ruby by reading the specdoc output.&lt;/p&gt;
&lt;h4&gt; With everything changing so fast, I imagine keeping the specs up to date is a constant struggle. How do implementers keep up with what&amp;#8217;s going on?&lt;/h4&gt;
&lt;p&gt;Some things are changing fast but when you look at how many distinct behaviors RubySpec covers, a lot isn&amp;#8217;t changing. It&amp;#8217;s hard keeping up with the 1.9 changes. So far in Rubinius, we haven&amp;#8217;t targeted 1.9 specifically, but we have a contributor, Marc-Andre Lafortune, who has added a ton of 1.8.7 and 1.9 features to the core libraries. As with anything, you do it one thing at a time. Having RubySpec available to tell you if you are doing it right is a big help.&lt;/p&gt;
&lt;h4&gt; I would really love to make RubySpec a household name in the general Ruby community. The project I&amp;#8217;m breaking ground on now (called Unity), will hopefully help do this. What sort of information do you think is most valuable to pull out from RubySpec, and who do you see benefiting from it? &lt;/h4&gt;
&lt;p&gt;Again, the primary goal of RubySpec is to provide a definition of the Ruby programming language along with a way to test that an implementation conforms to the definition. So primary pieces of information are which specs pass and which fail on a given implementation. Evan and I have discussed adding a scoring mechanism to MSpec that provides a single percentage number as a measure of an implementation&amp;#8217;s conformance to the standard set by RubySpec. This is something that can give users better information about an implementation&amp;#8217;s completeness when it goes bandying about benchmarking numbers. I&amp;#8217;ll be implementing this in MSpec shortly.&lt;/p&gt;
&lt;h4&gt; RubySpec spans a &lt;strong&gt;ton&lt;/strong&gt; of functionality. It&amp;#8217;s amazing to see how much work has already been done. However, there must be some dark spots still left. What areas of Ruby are still not adequately specified? &lt;/h4&gt;
&lt;p&gt;That is the 10 million dollar question. We can infer something about completeness based on implementations like JRuby that run applications the same as &lt;span class="caps"&gt;MRI&lt;/span&gt; and pass the vast majority of the RubySpec cases. When implementations discover bugs in their own code and enhance the RubySpec coverage, we get a nice closed feedback loop.&lt;/p&gt;
&lt;p&gt;A lot of the standard library still needs to be covered. There are also some little used methods in the core library that lack specs. The procedural-style methods in Kernel come to mind (e.g. using methods without receivers like #gets, #puts, #sub, etc. that operate on $_). We have a utility that creates the spec files for methods and leaves marker specs that we tag as incomplete. In Rubinius, for example, you can run &amp;#8216;bin/mspec tag &amp;#8212;list incomplete&amp;#8217; and get a printout of specs that need to be reviewed for completeness.&lt;/p&gt;
&lt;p&gt;Ultimately, we need a uniform methodology for auditing the completeness and accuracy of the specs. This is still a work in progress.&lt;/p&gt;
&lt;h4&gt; Charles Oliver Nutter (of JRuby) chimes in with some additional feedback on this question: &lt;/h4&gt;
&lt;p&gt;I would inject another small answer here:&lt;/p&gt;
&lt;p&gt;In my experience with RubySpec, the areas least covered are unfortunately often the areas least understood and therefore those most in need of coverage. For example, OpenSSL specs are practically nonexistent, and yet it&amp;#8217;s such a crucial bit of functionality for any app doing &lt;span class="caps"&gt;SSL&lt;/span&gt; (as a client or a server) or working with any &lt;span class="caps"&gt;PKI&lt;/span&gt; or certificate stuff. But it&amp;#8217;s also very hard to test&amp;#8230;most people have zero knowledge of crypto, and so finding people that can write those specs is about as easy as finding people that can write OpenSSL wrappers for new implementations. So there&amp;#8217;s definitely specific areas of RubySpec that need someone with domain knowledge to dive in.&lt;/i&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;As you can see, RubySpec is a vibrant, exciting project with a lot of challenges it needs to face.  My plans are to do my part by building up the Unity app to make this awesome body of work more accessible to the Ruby community at large.   But definitely don&amp;#8217;t wait on it before getting involved, check out RubySpec today and see if there is a way you can pitch in, especially if you think you can help with some of the projects mentioned here.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;To our readers: What sorts of things can you see RubySpec being used for?  What kind of data are you interested in seeing reports on?   Do you think Brian&amp;#8217;s goals for RubySpec are attainable?  Share your thoughts in the comments below.&lt;/b&gt;&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 04 Jun 2009 07:52:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/006-brian-ford-rubyspec.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/006-brian-ford-rubyspec.html</guid></item><item><title>Reading Ruby's Standard Library for Fun and Profit</title><description>&lt;p&gt;&lt;b&gt;Note: This post is inspired by JEG2&amp;#8217;s excellent code reading talk at &lt;span class="caps"&gt;MWRC&lt;/span&gt;&lt;br /&gt;
2009, called &lt;a href="http://www.confreaks.com/videos/48-mwrc2009-littlebigruby"&gt;LittleBIGRuby.&lt;/a&gt;  Go watch it if you have time, then come back and read this.  You might also want to check out the &lt;a href="http://on-ruby.blogspot.com/2009/05/questions-five-ways-code-reading.html"&gt;Question 5 Ways&lt;/a&gt; interview at Pat Eyler&amp;#8217;s &amp;#8220;On Ruby&amp;#8221; blog for more code reading goodness.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;We all need to hunt bugs and we all need to integrate our code with other systems. Some of us need to make use of undocumented libraries, and others need to examine code to perform security audits.  Why is it then, that so many developers suck at reading code?&lt;/p&gt;
&lt;p&gt;Almost a decade ago, Joel Spolsky would have told you it&amp;#8217;s harder to &lt;a href="http://www.joelonsoftware.com/articles/fog0000000069.html"&gt;read code than it is to write it&lt;/a&gt; , and that was probably true at one point.  But times have changed, and in a language like Ruby, this definitely does not have to be the case.  As an avid code reader, I&amp;#8217;ll let you in on a secret: All you need to do is practice!&lt;/p&gt;
&lt;p&gt;Beginners and even intermediate developers may need a lot of hand holding at first, that&amp;#8217;s only to be expected.  However, if you want to progress to higher levels of understanding, you must turn to the source.  The merits of higher level idioms and design strategies won&amp;#8217;t be very clear until you need to understand someone else&amp;#8217;s code.  Once you start digging around in someone else&amp;#8217;s code base, you&amp;#8217;ll learn a lot about your own strengths and weaknesses.&lt;/p&gt;
&lt;p&gt;But where should you start?  It&amp;#8217;s tempting to suggest that you should just follow your favorite projects on Github and keep an eye on the patches as they fly.  However, this can be a bit overwhelming at first, and it may be hard to keep up with the velocity and transient nature of active open source projects.  For this reason, I suggest picking a slower moving target that is still useful to you.  Ruby&amp;#8217;s Standard Library fits this description perfectly.&lt;/p&gt;
&lt;h3&gt;How I Read Code&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;m going to walk through the process I generally follow when I&amp;#8217;m reading code for the purpose of understanding its implementation.  I sometimes change things up when I have a particular goal in mind, but most of what I&amp;#8217;m about to cover is generally applicable to digging into any new code base.  Keep in mind though, our goal here is mainly just to explore a bit and maybe learn a thing or to along the way.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve chosen the Observable library here because it is small, self-contained, and easy to understand.  There are many libraries like this in the stdlib, so if you feel motivated after reading this post, you might want to go searching for a few other low hanging fruit to test your skills on.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve done what I can to boil the process down to a few repeatable steps, so you&amp;#8217;re welcome to follow along at home if you&amp;#8217;d like.&lt;/p&gt;
&lt;h4&gt;Start with a simple example.&lt;/h4&gt;
&lt;p&gt;I don&amp;#8217;t like diving into code without at least a little bit of context.  So this means that before I go source diving, I&amp;#8217;ll typically use the &lt;span class="caps"&gt;API&lt;/span&gt; documentation (if it exists), to code up a simple example.   If I can&amp;#8217;t find documentation, I&amp;#8217;ll search the web for examples.   It&amp;#8217;s relatively rare to come up completely empty, and a bit of a warning sign if you do.  But since Observable is well &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html"&gt;documented&lt;/a&gt; , it&amp;#8217;s easy enough to come up with something.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;re dealing with the Observer design pattern (also known as Pub-Sub), and the first idea that came to mind here for me was a simple chat model.  Users could subscribe to a room and the room would publish the messages that were processed to them.  I wanted the transcripts to be personalized, so that they looked something like this:&lt;/p&gt;
&lt;pre&gt;
  = Greg's Transcript =
  Me: Hi
  Jia: Hello
  Me: Fun, huh?
  Jia: Yeah!

  = Jia's Transcript =
  Gregory: Hi
  Me: Hello
  Gregory: Fun, huh?
  Me: Yeah!
&lt;/pre&gt;
&lt;p&gt;Thinking from the outside in, we can imagine the Ruby code used to generate this output:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  room = Room.new

  greg   = Person.new("Gregory", room)
  jia    = Person.new("Jia", room)

  greg.says "Hi"
  jia.says "Hello"

  greg.says "Fun, huh?"
  jia.says "Yeah!"

  puts "= Greg's Transcript ="
  puts greg.transcript + "\n"

  puts "= Jia's Transcript ="
  puts jia.transcript
&lt;/pre&gt;
&lt;p&gt;Ultimately, what the &lt;tt&gt;Observable&lt;/tt&gt; module buys us is the ability to track subscribers implicitly rather than explicitly.  While it&amp;#8217;d be pretty easy to roll your own code here, you can see in the following definition that &lt;tt&gt;Observable&lt;/tt&gt; provides a few shortcuts for us:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  require "observer"

  class Room 
    include Observable

    def receive(message, sender)
      changed
      notify_observers(message, sender)
    end
  end

  class Person
    def initialize(name, room)
      @name, @room = name, room
      @transcript  = ""
      @room.add_observer(self)
    end

    attr_reader :transcript, :name

    def update(message, sender)
      if sender == self
        message = "Me: #{message}\n"
      else
        message = "#{sender.name}: #{message}\n"
      end
    
      @transcript &amp;lt;&amp;lt; message
    end

    def says(message)
      @room.receive(message, self)
    end
  end
&lt;/pre&gt;
&lt;p&gt;This is all the code you need to reproduce the desired output, so if you&amp;#8217;re&lt;br /&gt;
interested in tinkering with it a bit, go ahead and do that now.  The real purpose of doing this however was not to implement something useful.   Instead, it was to identify some entry points into &lt;tt&gt;Observable&lt;/tt&gt; that can be our first areas of focus.&lt;/p&gt;
&lt;p&gt;If you look over the code carefully, you find that &lt;tt&gt;changed&lt;/tt&gt;, &lt;tt&gt;notify_observers&lt;/tt&gt;, and &lt;tt&gt;add_observer&lt;/tt&gt; are the keys to what makes our code tick (And incidentally, the only parts that we didn&amp;#8217;t implement ourselves.)  Let&amp;#8217;s dig a little deeper with those targets in mind.&lt;/p&gt;
&lt;h4&gt;Hunt for tests, if they exist.&lt;/h4&gt;
&lt;p&gt;Many of Ruby&amp;#8217;s standard libraries have tests distributed with the source, some do not.  Unfortunately in the case of &lt;tt&gt;Observable&lt;/tt&gt;, I couldn&amp;#8217;t find any tests for it in the Ruby source distribution.  However, that didn&amp;#8217;t mean that it was time to give up.  In cases where &lt;span class="caps"&gt;MRI&lt;/span&gt;/&lt;span class="caps"&gt;YARV&lt;/span&gt; do not have tests, you can try to turn to the great &lt;a href="http://github.com/rubyspec/rubyspec"&gt;RubySpec project&lt;/a&gt;, which is attempting to codify the behavior of Ruby with executable RSpec-like tests.&lt;/p&gt;
&lt;p&gt;As it turns out, RubySpec has &amp;#8220;several tests&amp;#8221;: for &lt;tt&gt;Observable&lt;/tt&gt;.  They are split up by each individual function.  As an example, here are the specs for &lt;tt&gt;notify_observers&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  describe "Observer#notify_observers" do
 
    before(:each) do
      @observable = ObservableSpecs.new
      @observer = ObserverCallbackSpecs.new
      @observable.add_observer(@observer)
    end
 
    it "must call changed before notifying observers" do
      @observer.value.should == nil
      @observable.notify_observers("test")
      @observer.value.should == nil
    end
 
    it "verifies observer responds to update" do
      lambda {
        @observable.add_observer(@observable)
      }.should raise_error(NoMethodError)
    end
 
    it "receives the callback" do
      @observer.value.should == nil
      @observable.changed
      @observable.notify_observers("test")
      @observer.value.should == "test"
    end
 
  end
&lt;/pre&gt;
&lt;p&gt;This code uses some helper objects, which are defined as follows:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  require 'observer'
 
  class ObserverCallbackSpecs
    attr_reader :value
 
    def initialize
      @value = nil
    end
 
    def update(value)
      @value = value
    end
  end
 
  class ObservableSpecs
    include Observable
  end
&lt;/pre&gt;
&lt;p&gt;You can see how the structure of these mirror our earlier example.  When you combined them with the specs, you learn quite a bit about this method.&lt;/p&gt;
&lt;p&gt;First of all, &lt;tt&gt;notify_observers&lt;/tt&gt; doesn&amp;#8217;t do anything if you don&amp;#8217;t first call the &lt;tt&gt;changed&lt;/tt&gt; method:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  it "must call changed before notifying observers" do
    @observer.value.should == nil
    @observable.notify_observers("test")
    @observer.value.should == nil
  end
&lt;/pre&gt;
&lt;p&gt;Also, we see that any of the observers used need to implement the &lt;tt&gt;update&lt;/tt&gt; callback, matching the signature of &lt;tt&gt;notify_observers&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  it "verifies observer responds to update" do
    lambda {
      @observable.add_observer(@observable)
    }.should raise_error(NoMethodError)
  end
&lt;/pre&gt;
&lt;p&gt;Of course, when the callback exists and the observable object has been marked as changed, we see a successful result:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  it "receives the callback" do
    @observer.value.should == nil
    @observable.changed
    @observable.notify_observers("test")
    @observer.value.should == "test"
  end
&lt;/pre&gt;
&lt;p&gt;While these specs aren&amp;#8217;t necessarily giving us every last detail, they provide a general outline of what to expect when we look directly at the source.  This will help us avoid getting overwhelmed when working through the actual implementation. I&amp;#8217;ll leave it to you to check out the rest of the  &lt;a href="http://github.com/rubyspec/rubyspec/tree/1250e42264782c860259046ff93719a3e6a9f03e/library/observer"&gt;Observable specs&lt;/a&gt; if you&amp;#8217;d like.  Doing so may help you understand the code in the following section.&lt;/p&gt;
&lt;h4&gt;Dig into the source with kindness and curiosity&lt;/h4&gt;
&lt;p&gt;The main purpose of reading over the tests first is that they give you a bit of insight into what behaviors the implementation code needs to satisfy.  Assuming you didn&amp;#8217;t skip the last section, it should be pretty clear what&amp;#8217;s going on in the following method definition:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def notify_observers(*arg)
    if defined? @observer_state and @observer_state
      if defined? @observer_peers
        @observer_peers.each { |k, v| k.send v, *arg }
      end
      @observer_state = false
    end
  end
&lt;/pre&gt;
&lt;p&gt;Of course, the &amp;#8216;why&amp;#8217; is as important as the &amp;#8216;how&amp;#8217; when you&amp;#8217;re exploring someone else&amp;#8217;s code. Without looking any farther, one thing that tripped me up was this line:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  if defined? @observer_state and @observer_state
&lt;/pre&gt;
&lt;p&gt;It was immediately clear to me what it did, but not why it was needed.  Upon further investigation, I remembered one of my bad habits.  The following code will issue a warning as written:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  module A
    def foo
      @foo
    end
  end

  class B
    include A
  end

  B.new.foo
&lt;/pre&gt;
&lt;p&gt;However, we can avoid this warning using the exact same approach used in &lt;tt&gt;Observable&lt;/tt&gt;, simply by checking whether the instance variable is defined before accessing it:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  module A
    def foo
      @foo if defined?(@foo)
    end
  end
&lt;/pre&gt;
&lt;p&gt;Yes, this is more verbose, and seems a bit redundant.  But writing code this way does have a real benefit.   If you follow this convention of checking whether an instance variable is defined whenever you aren&amp;#8217;t sure it will be, this warning can be used to easily catch typos.  The following example, when run with warnings enabled, will illustrate how:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class A
    def foo
      @foo = 10
      bar
    end

    def bar
      @bar = @fooo
    end
  end

  A.new.foo
&lt;/pre&gt;
&lt;p&gt;At this point, I already found a helpful reminder by walking through this code.  While the rest of the source was an interesting read, this was my takeaway.  Please go ahead and read the &lt;a href="http://pastie.org/488486"&gt;full source&lt;/a&gt; and let me know in the comments if you found anything of interest.&lt;/p&gt;
&lt;p&gt;In fact, I have a question for our readers.  Can you find any reason why the following code is written as it is?&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  # Query the changed state of this object.
  #
  def changed?
    if defined? @observer_state and @observer_state
      true
    else
      false
    end
  end
&lt;/pre&gt;
&lt;p&gt;This looks a bit like a wart to me, and if anything, seems like a long way to go in order to avoid &lt;tt&gt;!!obj&lt;/tt&gt;.   But maybe someone else sees it in a different light?  Actually, that reminds me&amp;#8230;&lt;/p&gt;
&lt;h3&gt;Caveats about the Standard Library&lt;/h3&gt;
&lt;p&gt;While Ruby&amp;#8217;s stdlib is a great place to learn new things, not everything in it is downright beautiful.   Some things are older than others, some reflecting the rust that comes when code is not updated to reflect the latest techniques.&lt;/p&gt;
&lt;p&gt;Furthermore, standard library code tends to be less clever than the sort of stuff we might write for our scripts and gems, by design.  You&amp;#8217;ll find that in many places, explicit and simple code is favored over tricky stuff.  I imagine this is for maintainability purposes.&lt;/p&gt;
&lt;p&gt;Like anything else, you&amp;#8217;ll need to do a bit of hunting to find the right code to learn from.   This practice in and of itself will prove to be useful, so don&amp;#8217;t be afraid if you run across some cruft now and again.&lt;/p&gt;
&lt;p&gt;Of course, another good thing about reading code is that it often inspires us to write code.  While looking through the standard library, consider putting together documentation patches, adding tests, and fixing things you don&amp;#8217;t like.  I&amp;#8217;m sure the folks on &lt;a href="http://www.ruby-lang.org/en/community/mailing-lists/"&gt;ruby-core&lt;/a&gt; would be happy to look at your contributions, and it&amp;#8217;s another great way to learn.&lt;/p&gt;
&lt;h3&gt;Key Things To Remember&lt;/h3&gt;
&lt;p&gt;Code reading is just as important as code writing.  The standard library has about 100 places for you to look for inspiration, and its more likely than not that you&amp;#8217;ll find something of interest if you pick one at random.  If you repeat this process, you&amp;#8217;ll get a better understanding of how Ruby itself works, and be able to carry this experience into your own work.&lt;/p&gt;
&lt;p&gt;Start by playing with some examples, then look for tests, either in the source or in the RubySpec project.  Once you feel like you understand how to use a feature, investigate how it works, and then ask yourself why it is written the way it is.  Don&amp;#8217;t expect to understand the whole thing all at once, and don&amp;#8217;t worry if it takes some time to adjust to someone else&amp;#8217;s style.&lt;/p&gt;
&lt;p&gt;If you do this often enough, you&amp;#8217;ll gain a valuable skill that&amp;#8217;ll pay off like crazy, especially if you work on open source software.  You&amp;#8217;ll notice both patterns and anti-patterns, and this will influence the code you write.  In the end, your fellow code readers will thank you :)&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Sun, 24 May 2009 18:25:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/005-code-reading-stdlib.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/005-code-reading-stdlib.html</guid></item><item><title>Hello World (Literally)</title><description>&lt;p&gt;If Google Analytics is right, we&amp;#8217;ve reached 68 countries in the last three days.  Although I guess this doesn&amp;#8217;t mean much at all on the internet, I think it still makes for a pretty map:&lt;/p&gt;
&lt;table style="width:auto;"&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="http://picasaweb.google.com/lh/photo/-OyFgPdlht14elpNP9-G8A?feat=embedwebsite"&gt;&lt;img src="http://lh4.ggpht.com/_hoiny8_4IEY/Sf_dRKHgzcI/AAAAAAAAALM/yFs0gVNO1To/s800/blog-rbp.png" width="600" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;p&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="font-family:arial,sans-serif; font-size:11px; text-align:right"&gt;From &lt;a href="http://picasaweb.google.com/gregory.t.brown/RBP?feat=embedwebsite"&gt;&lt;span class="caps"&gt;RBP&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/p&gt;
&lt;p&gt;Of course, I&amp;#8217;d like to back up these statistics with some direct data, so I&amp;#8217;ve got three questions for our readers:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Where are you from?&lt;/li&gt;
	&lt;li&gt;What is the Ruby scene like where you live?&lt;/li&gt;
	&lt;li&gt;What would you like to see on the &lt;span class="caps"&gt;RBP&lt;/span&gt; blog?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Please join in the conversation and let us know who you are.  So far, the discussions on this blog have been great and I want to get to know more of our readers.  We&amp;#8217;ll resume regularly scheduled deep tech content soon, but hopefully this small diversion will prove to be interesting.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 05 May 2009 06:42:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/hello_world.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/hello_world.html</guid></item><item><title>Fun with Class.new</title><description>&lt;p&gt;&lt;i&gt;Update: I came across an inlink to this page that was layering in advertisements in a frame. Luckily, there are &lt;a href="http://github.com/sandal/rbp-blog/commit/97df49430262beb8ddbb067fba5f1756efd4aac5"&gt;technical solutions&lt;/a&gt; to at least some social problems, and you may now browse again in peace :)&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;ve worked with Ruby for at least a little while, you might already know that classes in Ruby are objects themselves, in particular, instances of &lt;tt&gt;Class&lt;/tt&gt;.  Before I get into the fun stuff, let&amp;#8217;s quickly recap what that means.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s the ordinary way we define classes, as you all have seen.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class Point
    def initialize(x,y)
      @x, @y = x,y
    end
    
    attr_reader :x, :y
    
    def distance(point)
      Math.hypot(point.x - x, point.y - y)
    end
  end 
&lt;/pre&gt;
&lt;p&gt;The interesting thing is that the previous definition is essentially functionally equivalent to the following:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  Point = Class.new do
    def initialize(x,y)
      @x, @y = x,y
    end
  
    attr_reader :x, :y
  
    def distance(point)
      Math.hypot(point.x - x, point.y - y)
    end
  end
&lt;/pre&gt;
&lt;p&gt;This always struck me as a beautiful design decision in Ruby, because it reflects the inherent simplicity of the object model while exposing useful low level hooks for us to use.  Building on this neat concept of anonymously defined classes, I&amp;#8217;ll share a few tricks that have been useful to me in real projects.&lt;/p&gt;
&lt;h3&gt;Cleaner Exception Definitions&lt;/h3&gt;
&lt;p&gt;This sort of code has always bugged me:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  module Prawn
    module Errors
     
       class FailedObjectConversion &amp;lt; StandardError; end
     
       class InvalidPageLayout &amp;lt; StandardError; end        
     
       class NotOnPage &amp;lt; StandardError; end

       class UnknownFont &amp;lt; StandardError; end   

       class IncompatibleStringEncoding &amp;lt; StandardError; end     

       class UnknownOption &amp;lt; StandardError; end

    end
  end
&lt;/pre&gt;
&lt;p&gt;Although there are valid reasons for subclassing a &lt;tt&gt;StandardError&lt;/tt&gt;, typically the only reason I am doing it is to get a named exception to rescue.  I don&amp;#8217;t plan to ever add any functionality to its subclass beyond a named constant.  However, if we notice that &lt;tt&gt;Class.new&lt;/tt&gt; can be used to create subclasses, you can write something that more clearly reflects this intention:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  module Prawn
    module Errors
       FailedObjectConversion = Class.new(StandardError)
            
       InvalidPageLayout = Class.new(StandardError)     
     
       NotOnPage = Class.new(StandardError)

       UnknownFont = Class.new(StandardError)

       IncompatibleStringEncoding = Class.new(StandardError)     

       UnknownOption = Class.new(StandardError) 

    end
  end
&lt;/pre&gt;
&lt;p&gt;This feels a bit nicer to me, because although it&amp;#8217;s somewhat clear that these are still subclasses, I don&amp;#8217;t have an ugly empty class definition that will never be filled.  Of course, we could make this more &lt;span class="caps"&gt;DRY&lt;/span&gt; if we mix in a little &lt;tt&gt;const_set&lt;/tt&gt; hackery:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module Prawn
  module Errors
  
    exceptions = %w[ FailedObjectConversion InvalidPageLayout NotOnPage
                    UnknownFont IncompatibleStringEncoding UnknownOption ]

    exceptions.each { |e| const_set(e, Class.new(StandardError)) }

  end
end
&lt;/pre&gt;
&lt;p&gt;Even with this concise definition, you&amp;#8217;ll still get the functionality you&amp;#8217;d expect:&lt;/p&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; Prawn::Errors::IncompatibleStringEncoding.ancestors
=&amp;gt; [Prawn::Errors::IncompatibleStringEncoding, StandardError, Exception, Object, Kernel]
&amp;gt;&amp;gt; raise Prawn::Errors::IncompatibleStringEncoding, "Bad string encoding"
Prawn::Errors::IncompatibleStringEncoding: Bad string encoding
	from (irb):33
	from :0
&lt;/pre&gt;
&lt;p&gt;You can even take it farther than this, dynamically building up error classes by a naming convention.  If that sounds interesting to you and you don&amp;#8217;t mind the fuzziness of a &lt;tt&gt;const_missing&lt;/tt&gt; hook, you&amp;#8217;ll want to check out James Gray&amp;#8217;s blog post &lt;a href="http://blog.grayproductions.net/articles/summoning_error_classes_as_needed"&gt;&amp;#8220;Summoning Error Classes As Needed&amp;#8221;&lt;/a&gt;.  Though I tend to be a bit more conservative, there are definitely certain places where a hack like this can come in handy.&lt;/p&gt;
&lt;p&gt;The drawback of using any of these methods discussed here is that they don&amp;#8217;t really play well with RDoc.   However, there are easy ways to work around this, and James details some of them in his post.   In general, it&amp;#8217;s a small price to pay for greater clarity and less work in your code.&lt;/p&gt;
&lt;p&gt;Continuing on the theme of dynamically building up subclasses, we can move on to the next trick.&lt;/p&gt;
&lt;h3&gt;Safer Class Level Unit Testing&lt;/h3&gt;
&lt;p&gt;In my experience, it&amp;#8217;s a little bit tricky to test code that maintains state at the class level.  I&amp;#8217;ve tried everything from mocking out calls, to creating a bunch of explicit subclasses, and even have done something like &lt;tt&gt;klass = SomeClass.dup&lt;/tt&gt; in my unit tests.  While all of these solutions may do the trick depending on the context, there is a simple and elegant way that involves (you guessed it) &lt;tt&gt;Class.new&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s a quick example from a patch I submitted to the &lt;em&gt;builder&lt;/em&gt; library that verifies a fix I made to the &lt;tt&gt;BlankSlate&lt;/tt&gt; class:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def test_reveal_should_not_bind_to_an_instance
    with_object_id = Class.new(BlankSlate) do
      reveal(:object_id)
    end

    obj1 = with_object_id.new
    obj2 = with_object_id.new

    assert obj1.object_id != obj2.object_id,
       "Revealed methods should not be bound to a particular instance"
  end
&lt;/pre&gt;
&lt;p&gt;Notice here that although we are doing class level logic, this test is still atomic and does not leave a mess behind in its wake.  We get to use the real &lt;tt&gt;reveal()&lt;/tt&gt; method, which means we don&amp;#8217;t need to think up the mocking interface.  Because we only store this anonymous subclass in a local variable, we know that our other tests won&amp;#8217;t be adversely affected by it, it&amp;#8217;ll just disappear when the code falls out of scope.  The code is clear and expressive, which is a key factor in tests.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re looking for more examples like this, you&amp;#8217;ll want to look over the rest of the &lt;tt&gt;BlankSlate&lt;/tt&gt; &lt;a href="http://github.com/jimweirich/builder/blob/c41c8914bec84f0009869e96ad6ebb39987896d3/test/test_blankslate.rb"&gt;test case&lt;/a&gt; .  I just followed Jim Weirich&amp;#8217;s lead here, so you&amp;#8217;ll see a few other examples that use a similar trick.&lt;/p&gt;
&lt;p&gt;For those interested, the thing that sparked my interest in writing this article today was a tiny bit of domain specific code I had cooked up for my day to day work, which needed to implement a nice interface for filtering search queries at the class level before they were executed.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class FreightOffer::Rail &amp;lt; FreightOffer
    hard_constraint do |query|
      query[:delivery] - query[:pickup] &amp;gt; 5.days
    end

    soft_constraint do |query|
      query[:asking_price] &amp;lt; 5000.to_money
    end
  end
&lt;/pre&gt;
&lt;p&gt;While it&amp;#8217;s easy to test these constraints once they&amp;#8217;re set up for a particular model, I wanted to be able to test drive the constraint interface itself at the abstract level. The best way I could come up with was to write tests that used &lt;tt&gt;Class.new&lt;/tt&gt; to generate subclasses of &lt;tt&gt;FreightOffer&lt;/tt&gt; to test against, which worked well.  While I won&amp;#8217;t post those tests here, it&amp;#8217;s a good exercise if you want to get the feel for this technique.&lt;/p&gt;
&lt;p&gt;The next trick is a little bit different than the first two, but can really come in handy when you want to inject a little syntactic diabetes into the mix.&lt;/p&gt;
&lt;h3&gt;Parameterized Subclassing&lt;/h3&gt;
&lt;p&gt;You might recognize the following example, since it is part of the &lt;a href="http://cachefly.oreilly.com/catalogs/Mastering_the_Dynamic_Toolkit.pdf"&gt;sample chapter&lt;/a&gt; of &lt;a href="http://rubybestpractices.com"&gt;Ruby Best Practices&lt;/a&gt; .  More than just a shameless plug though, I think this particular example shows off an interesting technique for dynamically building up subclasses in a low level system.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s start with some vanilla code and see how we can clean it up, then we&amp;#8217;ll finally take a look under the hood.  What follows is a &lt;tt&gt;Fatty::Formatter&lt;/tt&gt;, which abstracts the task of producing output in a number of different formats.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class MyReport &amp;lt; Fatty::Formatter 
  
    module Helpers 
      def full_name 
        "#{params[:first_name]} #{params[:last_name]}" 
      end 
    end 
    
    class Txt &amp;lt; Fatty::Format 
      include MyReport::Helpers 
       def render 
        "Hello #{full_name} from plain text" 
       end 
    end 
    
    # use a custom Fatty::Format subclass for extra features 
    class PDF &amp;lt; Prawn::FattyFormat 
      include MyReport::Helpers 
      def render 
        doc.text "Hello #{full_name} from PDF" 
        doc.render 
      end 
    end 
    
    formats.update(:txt =&amp;gt; Txt, :pdf =&amp;gt; PDF) 
  end
&lt;/pre&gt;
&lt;p&gt;The &lt;tt&gt;MyReport&lt;/tt&gt; class in the previous code sample is little more than a glorified &amp;#8220;Hello World&amp;#8221; example that outputs text and &lt;span class="caps"&gt;PDF&lt;/span&gt;.   It is pretty easy to follow and doesn&amp;#8217;t really do anything fancy.  However, we can certainly clean it up and make it look better:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class MyReport &amp;lt; Fatty::Formatter 
    
    helpers do 
      def full_name 
        "#{params[:first_name]} #{params[:last_name]}" 
      end 
    end 
    
    format :txt do 
      def render 
        "Hello #{full_name} from plain text" 
      end 
    end 
    
    format :pdf, :base =&amp;gt; Prawn::FattyFormat do 
      def render 
        doc.text "Hello #{full_name} from PDF" 
        doc.render 
      end 
    end 
    
  end
&lt;/pre&gt;
&lt;p&gt;I think you&amp;#8217;ll agree that this second sample induces the kind of familiar sugar shock that Ruby coders know and love.  It accomplishes the same goals and actually wraps the lower level code rather than replacing it.  But is there some sort of dark magic behind this?  Let&amp;#8217;s peek behind the curtain:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def format(name, options={}, &amp;amp;block) 
    formats[name] = Class.new(options[:base] || Fatty::Format, &amp;amp;block) 
  end
&lt;/pre&gt;
&lt;p&gt;As you can see, it is nothing more than a hash of &lt;tt&gt;formats&lt;/tt&gt; maintained at the class level, combined with a call to &lt;tt&gt;Class.new&lt;/tt&gt; to generate the subclass.  No smoke and mirrors, just the same old Ruby tricks we&amp;#8217;ve been using throughout this article.  The curious may wonder how &lt;tt&gt;helpers()&lt;/tt&gt; works here, and though it&amp;#8217;s slightly tangential, you&amp;#8217;ll see it is similarly simple.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def helpers(helper_module=nil, &amp;amp;block) 
    @helpers = helper_module || Module.new(&amp;amp;block) 
  end 
&lt;/pre&gt;
&lt;p&gt;Though I won&amp;#8217;t get into it here, fun tricks can be done with anonymous modules as well.  Can you think of any that are particularly interesting?  If so, let me know in the comments.&lt;/p&gt;
&lt;p&gt;Although I only showed a part of the picture, you might want to check out the rest of &lt;a href="http://github.com/sandal/fatty/blob/392d65ee9b4be167cfe568f0669b64694ef2bbff/lib/fatty.rb"&gt;Fatty&amp;#8217;s source&lt;/a&gt;.  I call it a &amp;#8216;67 line replacement for &lt;a href="http://rubyreports.org"&gt;Ruport&lt;/a&gt;&amp;#8217;, which is a major exaggeration, but it is really surprising how much you can get out of a few dynamic Ruby tricks when you combine them together properly.  A lot of these ideas were actually inspired by the &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; web framework, so that&amp;#8217;s another project to add to your code reading list if you&amp;#8217;re looking to learn new tricks.&lt;/p&gt;
&lt;p&gt;Anyway, I&amp;#8217;m getting off topic now, and it&amp;#8217;s about time to wrap up anyway.  I&amp;#8217;ll go on one more tiny tangent, and then send you on your way.&lt;/p&gt;
&lt;h3&gt;Shortcutting with Struct&lt;/h3&gt;
&lt;p&gt;With all this talk about using anonymous sub-classes, I can&amp;#8217;t help but recap one of the &lt;a href="http://blog.grayproductions.net/articles/all_about_struct"&gt;oldest tricks in the book&lt;/a&gt;.  The method &lt;tt&gt;Struct.new&lt;/tt&gt; can be handy for shortcutting class creation.  Using it, the very first example in this post could be simplified down to:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Point &amp;lt; Struct.new(:x, :y)
  def distance(point)
    Math.hypot(point.x - x, point.y - y)
  end
end
&lt;/pre&gt;
&lt;p&gt;The constructor and accessors are provided for free by &lt;tt&gt;Struct&lt;/tt&gt; here.  Of course, the real reason I mentioned this was to get you thinking.  If &lt;tt&gt;Struct.new&lt;/tt&gt; can work this way, and we know that &lt;tt&gt;Class.new&lt;/tt&gt; can accept a block definition that provides a closure, what other cool uses of parameterized subclasses can we cook up?&lt;/p&gt;
&lt;h3&gt;Please Share Your Thoughts&lt;/h3&gt;
&lt;p&gt;I wrote this article in hopes that it&amp;#8217;ll be a jumping off point for discussion on more cool tricks I haven&amp;#8217;t seen before, or failing that, ones that other readers haven&amp;#8217;t seen before.  While there certainly is a lot of nasty, scary looking &amp;#8220;meta-programming&amp;#8221; out there that makes people feel like this stuff is hard and esoteric, there are many techniques that lead to simple, elegant, and beautiful dynamic code.  Please let me know what you think of these techniques, and definitely share one of your own if you&amp;#8217;d like.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 30 Apr 2009 17:55:00 -0400</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/anonymous_class_hacks.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/anonymous_class_hacks.html</guid></item><item><title>Rails Modularity for Lazy Bastards</title><description>&lt;p&gt;When we develop standalone systems or work on libraries and frameworks, modularity seems to come naturally.  When something seems to gain a life of its own, we pull it off into its own package or subsystem to keep things clean.  This is a natural extension of the sorts of design decisions we make at the object level in our software, and helps us work on complicated problems, often working alongside other hackers, without losing our wits.&lt;/p&gt;
&lt;p&gt;It seems a bit surprising that these helpful guiding principals can often evaporate when we bring our software to the web.  Sure, we may make use of plugins and gems for foundational support, but if you take a good look at a Rails application that has more than 50 models or so, you&amp;#8217;ll be hard pressed to find a unified purpose behind all that logic.&lt;/p&gt;
&lt;p&gt;However, if you try to break things down into core sets of functionality, you may find that certain vertical slices can be made that allow you to break out bits of functionality into their own separate focus areas. For example, you may discover that part of your application is actually a mini &lt;span class="caps"&gt;CRM&lt;/span&gt; system.  Or maybe you&amp;#8217;ve snuck in a small reporting system without noticing it consciously.   The list goes on, but the underlying idea here is that the larger a system gets, the harder it is to define its core purpose.&lt;/p&gt;
&lt;p&gt;While splitting out these subsystems may seem like the obvious choice in a standalone application, there seems to be a certain amount of &lt;span class="caps"&gt;FUD&lt;/span&gt; about this strategy when it comes to the web.  This probably originates from a time before &lt;span class="caps"&gt;REST&lt;/span&gt;, in which interaction between web applications was overly complex,making the costs of fragmentation higher than the benefits of a modular architecture.  These days, we live in better times and work with better frameworks, and should reap the benefits that come along with it.&lt;/p&gt;
&lt;p&gt;But actions speak louder than words, so let&amp;#8217;s take a look at some of the underlying tech and how to use it.  Building on the &lt;span class="caps"&gt;CRM&lt;/span&gt; scenario, I&amp;#8217;ll start by showing how to access a Customer model from another application using ActiveResource.&lt;/p&gt;
&lt;h3&gt;Sharing Model Data via ActiveResource&lt;/h3&gt;
&lt;p&gt;Suppose that we&amp;#8217;ve got a &lt;span class="caps"&gt;CRM&lt;/span&gt; system that has a Customer model that has a schema that looks something like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  create_table :customers do |t|
    t.string :first_name
    t.string :last_name
    t.string :email
    t.string :daytime_phone
    t.string :evening_phone
    t.timestamps
  end
&lt;/pre&gt;
&lt;p&gt;With this, we can do all the ordinary &lt;span class="caps"&gt;CRUD&lt;/span&gt; operations within our application, so I won&amp;#8217;t bore you with the details.  What we&amp;#8217;re interested in is how to accomplish these same goals from an external application.  So within our &lt;span class="caps"&gt;CRM&lt;/span&gt; system, this essentially boils down to simply providing a RESTful interface to our Customer resource.  After adding &lt;tt&gt;map.resources :customers&lt;/tt&gt; to our &lt;em&gt;config/routes.rb&lt;/em&gt; file, we code up a &lt;tt&gt;CustomersController&lt;/tt&gt; that looks something like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class CustomersController &amp;lt; ApplicationController
  
    def index
      @customers = Customer.find(:all)
      respond_to do |format|
        format.xml { render :xml =&amp;gt; @customers }
        format.html
      end
    end
 
    def show
      customer = Customer.find(params[:id])
      respond_to do |format|
        format.xml { render :xml =&amp;gt; customer.to_xml }
        format.html
      end
    end
 
    def create
      customer = Customer.create(params[:customer])
      respond_to do |format|
        format.html { redirect_to entry }
        format.xml { render :xml =&amp;gt; customer, :status   =&amp;gt; :created, 
                                              :location =&amp;gt; customer }
      end
    end
 
    def update
      customer = Customer.update(params[:id], params[:customer])
      respond_to do |format|
        format.xml { render :xml =&amp;gt; customer.to_xml }
        format.html
      end
    end
 
    def destroy
      Customer.destroy(params[:id])
      respond_to do |format|
        format.xml { render :xml =&amp;gt; "", :status =&amp;gt; 200 }
        format.html
      end
    end
 
  end
&lt;/pre&gt;
&lt;p&gt;This may look familiar even if you haven&amp;#8217;t worked with ActiveResource previously, as it&amp;#8217;s basically the same boiler plate you&amp;#8217;ll find in a lot of Rails documentation.  In the &lt;tt&gt;respond_to&lt;/tt&gt; block, &lt;tt&gt;format.xml&lt;/tt&gt; is what matters here, as it is what connects our resource to the services which consume it.  The good news is we won&amp;#8217;t have to actually work with the &lt;span class="caps"&gt;XML&lt;/span&gt; data, as you&amp;#8217;ll see in a moment.&lt;/p&gt;
&lt;p&gt;While there are a few things left to do to make this code usable in a real application, we can already test basic interactions with a secondary application.  Using any other rails app we&amp;#8217;d like, we can add an &lt;tt&gt;ActiveResource&lt;/tt&gt; model by creating a file called &lt;em&gt;app/models/customer.rb&lt;/em&gt; and setting it up like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class Customer &amp;lt; ActiveResource::Base
    self.site = "http://localhost:3000/"
  end
&lt;/pre&gt;
&lt;p&gt;Now here comes the interesting part.  If you fire up &lt;tt&gt;script/console&lt;/tt&gt; on the client side application that is interfacing with the &lt;span class="caps"&gt;CRM&lt;/span&gt; system, you can see the same familiar &lt;span class="caps"&gt;CRUD&lt;/span&gt; operations, but taking place from a completely separate application:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  &amp;gt;&amp;gt; Customer.create(:first_name =&amp;gt; "Gregory", :last_name =&amp;gt; "Brown")
  =&amp;gt; #&amp;lt;Customer:0x20d2120 @prefix_options={}, @attributes={"evening_phone"=&amp;gt;nil, "updated_at"=&amp;gt;Thu Apr 16 03:53:59 UTC 2009, "daytime_phone"=&amp;gt;nil, "id"=&amp;gt;1, "first_name"=&amp;gt;"Gregory", "last_name"=&amp;gt;"Brown", "created_at"=&amp;gt;Thu Apr 16 03:53:59 UTC 2009, "email"=&amp;gt;nil}&amp;gt;
 
  &amp;gt;&amp;gt; Customer.find(:all)
  =&amp;gt; [#&amp;lt;Customer:0x20a939c @prefix_options={}, @attributes={"evening_phone"=&amp;gt;nil, "updated_at"=&amp;gt;Thu Apr 16 03:53:59 UTC 2009, "daytime_phone"=&amp;gt;nil, "id"=&amp;gt;1, "first_name"=&amp;gt;"Gregory", "last_name"=&amp;gt;"Brown", "created_at"=&amp;gt;Thu Apr 16 03:53:59 UTC 2009, "email"=&amp;gt;nil}&amp;gt;]
 
  &amp;gt;&amp;gt; Customer.find(1).first_name
  =&amp;gt; "Gregory"
 
  &amp;gt;&amp;gt; Customer.delete(1)
  =&amp;gt; #&amp;lt;Net::HTTPOK 200 OK readbody=true&amp;gt;
 
  &amp;gt;&amp;gt; Customer.find(:all)
  =&amp;gt; []
&lt;/pre&gt;
&lt;p&gt;While the interface and behavior isn&amp;#8217;t downright identical to &lt;tt&gt;ActiveRecord&lt;/tt&gt;, it bears a striking resemblance and allows you to retain much of the functionality that is needed for basic data manipulation.&lt;/p&gt;
&lt;p&gt;Now that we can see the basic functionality in action, let&amp;#8217;s go back and fix a few key issues.  We definitely want to add some sort of authentication to this system, as it is currently allowing any third party application to modify and destroy records.  We also will most likely want a more flexible option for locating services than just hard coding a server address in each model file.  Once these two things are in place, we&amp;#8217;ll have the beginnings of a decentralized Rails based application.&lt;/p&gt;
&lt;h3&gt;&lt;span class="caps"&gt;API&lt;/span&gt; Keys with &lt;span class="caps"&gt;HTTP&lt;/span&gt; Basic Authentication&lt;/h3&gt;
&lt;p&gt;I want to preface this section by saying I&amp;#8217;m typically not the one responsible for any sort of security hardening in the applications I work on.   That means that I&amp;#8217;m by no means an expert in how to make your applications safe from the malignant forces of the interweb.   That having been said, what follows is a simple technique that seems to work for me when it comes to slapping a simple authentication model in place.&lt;/p&gt;
&lt;p&gt;First, in the app that is providing the service, in this case, our fictional &lt;span class="caps"&gt;CRM&lt;/span&gt; system, you&amp;#8217;ll want something like this in your &lt;tt&gt;ApplicationController&lt;/tt&gt;:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  def basic_http_auth
    authenticated = false
    authenticate_with_http_basic do |login, password|
      if login == "api" &amp;amp;&amp;amp; password == API_KEY
        authenticated = true
      end
    end
 
    raise "Authentication failed" unless authenticated
  end
&lt;/pre&gt;
&lt;p&gt;Here, &lt;tt&gt;API_KEY&lt;/tt&gt; is some shared secret that is known by both your service providing application, and any client that wishes to use your service.  In this blog post, I&amp;#8217;ll be using the string &lt;tt&gt;&amp;#8220;kittens&amp;#8221;&lt;/tt&gt;, but you&amp;#8217;ll obviously want to pick something longer, and with significantly more entropy.&lt;/p&gt;
&lt;p&gt;After dropping a &lt;tt&gt;before_filter&lt;/tt&gt; in your &lt;tt&gt;CustomersController&lt;/tt&gt; that points to &lt;tt&gt;basic_http_auth&lt;/tt&gt;, you&amp;#8217;ll need to update your ActiveResource model definition.&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class Customer &amp;lt; ActiveResource::Base
    self.site = "http://localhost:3000/"
    self.user = "api"
    self.password = "kittens"
  end
&lt;/pre&gt;
&lt;p&gt;If you forget to do this, you won&amp;#8217;t be able to retrieve or modify any of the customer data.  This means that any application that does not know the shared secret may not use the resource.  Although this is hardly a fancy solution, it gets the job done.   Now, let&amp;#8217;s take a look at how to make integration even easier and get rid of some of these hard coded values at the per-model level.&lt;/p&gt;
&lt;h3&gt;Simplifying Integration&lt;/h3&gt;
&lt;p&gt;So far, the work has been pretty simple, but it&amp;#8217;s important to keep in mind that if we really want to break up our applications into small, manageable subsystems, we might need to deal with a lot of remote resources.&lt;/p&gt;
&lt;p&gt;Pulled directly from some commercial work I&amp;#8217;ve been doing with Brad Ediger of Madriska Media Group (and of &lt;a href="http://oreilly.com/catalog/9780596510329/"&gt;Advanced Rails&lt;/a&gt; fame), what follows is a helper file that provides two useful features for working with remote resources via ActiveResource:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  require 'yaml'
  require 'activeresource'
 
  class ServiceLocator
 
    API_KEY = "kittens"
 
    def self.services
      return @services if @services
      config_file = File.join(RAILS_ROOT, %w[config services.yml])
      config = YAML.load_file(config_file)
      @services = config[RAILS_ENV]
    end
 
    def self.[](name)
      services[name.to_s]
    end
  end
 
  def Service(name)
    Class.new(ActiveResource::Base) do
      self.site = "http://#{ServiceLocator[name]}"
      self.user = "api"
      self.password = ServiceLocator::API_KEY
    end
  end
&lt;/pre&gt;
&lt;p&gt;The &lt;tt&gt;ServiceLocator&lt;/tt&gt; part was Brad&amp;#8217;s idea, and it represents a simple way to map the URLs of different services to a label based on what environment you are currently running in.  A basic &lt;em&gt;config/services.yml&lt;/em&gt; file might look something like this:&lt;/p&gt;
&lt;pre name="code"&gt;
  development:
    crm: localhost:3000
    reports: localhost:3001

  production:
    crm: crm.example.com
    reports: reports.example.com
&lt;/pre&gt;
&lt;p&gt;This is nice, because it allows us to configure the locations of our various services all in one place.  The interface is very simple and straightforward:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  &amp;gt;&amp;gt; ServiceLocator[:crm]
  =&amp;gt; "localhost:3000"
&lt;/pre&gt;
&lt;p&gt;However, upon seeing this feature, I decided to take it a step farther.  Though it might sacrifice a bit of purity, the &lt;tt&gt;Service()&lt;/tt&gt; method is actually a parameterized class constructor that builds up a subclass filling out the &lt;span class="caps"&gt;API&lt;/span&gt; key and service address for you. What that means is that you can replace your initial &lt;tt&gt;Customer&lt;/tt&gt; definition with this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
  class Customer &amp;lt; Service(:crm)
    # my custom methods here.
  end
&lt;/pre&gt;
&lt;p&gt;Since Rails handles the mapping of resource names to class names for you, you can easily support as many remote classes from a single service as you&amp;#8217;d like this way.   When I read this aloud in my head, I tend to think of &lt;tt&gt;SomeClass &amp;lt; Service(:some_service)&lt;/tt&gt; as &amp;#8220;SomeClass is a resource provided by some_service&amp;#8221;.   Feel free to forego the magic here if this concerns you, but I personally find it pleasing to the eyes.&lt;/p&gt;
&lt;h3&gt;Just Starting a Conversation&lt;/h3&gt;
&lt;p&gt;I didn&amp;#8217;t go into great detail about how to use the various technologies I&amp;#8217;ve touched on here, but I&amp;#8217;ve hopefully provided a glimpse into what is possible to those who are uninitiated, as well as provided some food for thought to those who already have some experience in building decentralized Rails apps.&lt;/p&gt;
&lt;p&gt;To provide some extra insight into the approach I&amp;#8217;ve been using on my latest project, we basically keep everything in one big git repository, with separate folders for each application.  At the root, there is a &lt;em&gt;shared/&lt;/em&gt; folder in which we keep some shared library files, including some support infrastructure for things like a simple &lt;span class="caps"&gt;SSO&lt;/span&gt; mechanism and a database backed cross-application logging system.   We also vendor one copy of Rails there and simply symlink &lt;em&gt;vendor/rails&lt;/em&gt; in our individual apps, except for when we need a specific version for a particular service.&lt;/p&gt;
&lt;p&gt;The overarching idea is that there is a foundational support library that our individual apps sit on top of, and that they communicate with each other only through the service interfaces we expose.  We&amp;#8217;ve obviously got more complicated needs, and thus finer grained controls than what I&amp;#8217;ve covered in this article, but the basic ActiveResource functionality seems to be serving us well.&lt;/p&gt;
&lt;p&gt;What I&amp;#8217;d like to know is what other folks have been doing to help manage the complexity of their larger Rails apps.  Do you think the rough sketch of ideas I&amp;#8217;ve laid out here sounds promising?  Do you foresee potential pitfalls that I haven&amp;#8217;t considered?  Leave a comment and let me know.&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Thu, 16 Apr 2009 04:31:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/rails_modularity_1.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/rails_modularity_1.html</guid></item><item><title>Welcome to the RBP blog</title><description>&lt;p&gt;Hello, and welcome to the Ruby Best Practices blog.  I am happy to announce this new collaborative writing project, along with the great team that&amp;#8217;ll be running it.  A couple weeks ago I put out a call for volunteers to help me provide a new community resource for discovering and discussing best practices in Ruby, and now, we&amp;#8217;re ready to get rolling.&lt;/p&gt;
&lt;p&gt;In addition to myself: &lt;a href="/about/jamesbritt.html"&gt;James Britt&lt;/a&gt;, &lt;a href="/about/wyhaines.html"&gt;Kirk Haines&lt;/a&gt;, &lt;a href="/about/rklemme.html"&gt;Robert Klemme&lt;/a&gt;, &lt;a href="/about/jm.html"&gt;Jeremy McAnally&lt;/a&gt;, &lt;a href="/about/seanohalpin.html"&gt;Sean O&amp;#8217;Halpin&lt;/a&gt;, &lt;a href="/about/judofyr.html"&gt;Magnus Holm&lt;/a&gt; and &lt;a href="/about/laktek.html"&gt;Lakshan Perera&lt;/a&gt; round out the team.  I am absolutely thrilled to have such a diverse and talented group to work with, and I can&amp;#8217;t wait to see the content that we&amp;#8217;ll produce together.&lt;/p&gt;
&lt;p&gt;The general theme we&amp;#8217;ve agreed upon for this blog is that each post should exhibit Ruby code that we can be proud of.  We want to expose best practices through practical examples, so you will surely see a lot of open source code being discussed here.  We want to give you less of &amp;#8220;Here&amp;#8217;s six great Rails plugins&amp;#8221; and more of &amp;#8220;Examples of Connascense in ActiveRecord&amp;#8221;, or &amp;#8220;Common Pitfalls in &lt;span class="caps"&gt;DSL&lt;/span&gt; Design, and How to Avoid Them&amp;#8221;.  Not every single post will be focused directly on best practices and idioms, but all will contain them.&lt;/p&gt;
&lt;p&gt;This will be a code and high-level design heavy blog.  We don&amp;#8217;t plan to bore you with war stories or skimp on the technical value of our content to promote our latest cute but useless hack.  That having been said, we plan to have fun while we write, and the occasional diversion will happen as a result.  However, it&amp;#8217;s safe to say that our content will be geared more towards the motivated developer looking to improve her craft rather than the general &amp;#8220;plzgivemethecodes&amp;#8221; crowd.  If that sounds like your kind of thing, I think you&amp;#8217;ll really enjoy this blog.&lt;/p&gt;
&lt;p&gt;The blog engine we&amp;#8217;ve decided to use is &lt;a href="http://github.com/sandal/korma"&gt;Korma&lt;/a&gt; which I&amp;#8217;m sure you&amp;#8217;ll hear more about in the near future.  It was designed specifically to run this blog, is git based, and gives us a lot of cool features that we wanted to make sure to have in place before launch.  Because we want to provide both the authors and the readers freedom to choose which content they are interested in, it is possible to subscribe to individual author feeds on this blog.  This means that if you like 6 of us, the two you don&amp;#8217;t like won&amp;#8217;t bother you.   Of course, we hope you like us all.&lt;/p&gt;
&lt;p&gt;Over time, we&amp;#8217;ll eventually begin accepting outside contributions to this blog, likely through something as simple as a fork and pull request on github.  This means if you have an idea for an article that you&amp;#8217;d like to see here, we may be accepting new content from third party contributors soon.  However, before we can do that, we need to wait for the dust to settle, so look for an announcement in a few weeks.&lt;/p&gt;
&lt;p&gt;It may take us a little while to work out the kinks in our blogging software.  We want to keep things simple, but there still might be a few bugs here or there.  If you run into problems using this blog, please email gregory.t.brown at gmail.com and let me know.&lt;/p&gt;
&lt;p&gt;I think that pretty much wraps up the welcome letter.  New posts should be cycling in within the next couple days, and we hope to remain active moving forward.  Thanks for checking out this new blog, and happy hacking!&lt;/p&gt;</description><author>gregory.t.brown@gmail.com (Gregory Brown)</author><pubDate>Tue, 07 Apr 2009 17:02:00 -0000</pubDate><link>http://blog.rubybestpractices.com/posts/gregory/welcome.html</link><guid>http://blog.rubybestpractices.com/posts/gregory/welcome.html</guid></item></channel></rss>