Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

Features and Bugs Addendum

I was inspired by both Jared’s previous post and the Ruby Quiz for last Friday and wrote my FizzBuzz using the Open-Closed principle. Here’s the main program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# fizzerbuzzer.rb
class FizzerBuzzer

  def self.filters
    @filters ||= []
  end

  Dir.glob("*_filter.rb").sort.each{|f| require f }
  
  def self.filter(i)
    filters.collect do |filter|
      filter.filter(i)
    end.compact.join("")
  end
  
  def self.fizzbuzz
    (1..100).each do |i|
      result = filter(i)
      puts result.empty? ? i : result
    end
  end
end

FizzerBuzzer.fizzbuzz

It’s built the way it is so that you can change the way the code executes without having to change fizzerbuzzer.rb yourself. It looks for files ending in _filter.rb in the same directory and executes them. The classes they implement need to respond_to? :filter and they need to place an instance of themselves into the filters array. Wonderful extensability!

As for the filters themselves, they have to be named in order so the glob picks them up the right way (because we’re not writing BuzzFizz). Here’s the filter for Fizzing.

1
2
3
4
5
6
7
8
9
10
# 01_fizz_filter.rb
class FizzFilter
  def filterable?(index)
    index % 3 == 0
  end
  def filter(index)
    "Fizz" if filterable? index
  end
end
FizzerBuzzer.filters << FizzFilter.new

And the same one for Buzzing.

1
2
3
4
5
6
7
8
9
10
# 02_buzz_filter.rb
class BuzzFilter
  def filterable?(index)
    index % 5 == 0
  end
  def filter(index)
    "Buzz" if filterable? index
  end
end
FizzerBuzzer.filters << BuzzFilter.new

There, doesn’t that feel nice and extensible? For comparison, though, here’s a much shorter one

1
2
3
4
5
6
7
(1..100).each do |i|
  result = []
  result << "Fizz" if i % 3 == 0
  result << "Buzz" if i % 5 == 0
  result << i if result.empty?
  puts result.join("")
end

Doesn’t the nonflexibility of that just kill you?


About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.