<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Giordano Scalzo&#039;s Personal Blog &#187; bowling</title>
	<atom:link href="http://giordano.scalzo.biz/tag/bowling/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Fri, 09 Jul 2010 09:41:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bowling Kata with Ruby and RSpec</title>
		<link>http://giordano.scalzo.biz/2009/09/22/bowling-kata-with-ruby-and-rspec/</link>
		<comments>http://giordano.scalzo.biz/2009/09/22/bowling-kata-with-ruby-and-rspec/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 18:59:34 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[bowling]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=117</guid>
		<description><![CDATA[Looking around in daily feeds reading, suddenly I realized I never practiced the first code kata: that Bowling Kata that started all.
I decided to implement it while exploring RSpec and configuring my Ruby environment for Windows:
I used to do my programming under  friendly Ubuntu, but  because my recent jobs duties in Delphi, mainly [...]]]></description>
			<content:encoded><![CDATA[<p>Looking around in daily feeds reading, suddenly I realized I never practiced the first code kata: that <a href="http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata">Bowling Kata</a> that started all.<br />
I decided to implement it while exploring RSpec and configuring my Ruby environment for Windows:<br />
I used to do my programming under  friendly Ubuntu, but  because my recent jobs duties in Delphi, mainly I use a Windows Xp system.</p>
<p>Installing Ruby is straightforward thanks to <a href="http://rubyinstaller.org/">RubyInstaller</a>, a wonderful project that let you configure a Ruby environment under Windows; to dive into a complete Bdd flow I configured <a href="http://www.zenspider.com/ZSS/Products/ZenTest/">autospec</a> and <a href="http://www.growlforwindows.com/gfw/default.aspx">Growl</a> following this useful <a href="http://monket.net/blog/2009/07/autotest-growl-for-windows/">post</a>: I advice every Bdd practitioner to give <a href="http://www.zenspider.com/ZSS/Products/ZenTest/">autospec</a> a try, it can save a lot of windows and mental switch&#8230; but it can&#8217;t be told, try it and enjoy it.</p>
<p>While I&#8217;m very happy with Eclipse during Java coding, I never found a satisfactory editor for Ruby code.<br />
So I decided to try to enter in guru world and use <a href="http://www.vim.org/">Vim</a>,  adding a bunch of useful plugin, as the wondeful <a href="http://www.vim.org/scripts/script.php?script_id=2540">snipMate</a> that import the Textmate snippets under Vim.</p>
<p>Back to kata, these are my specs:</p>
<pre class='brush: ruby'>
require File.join(File.dirname(__FILE__), "//spec_helper")

describe Bowling do

	before(:each) do
		@game = Bowling.new
	end

	def roll_many(num, pins)
		num.times do |hit|
			@game.hit(pins)
		end
	end

	it "should score 0 for gutter game" do
		roll_many(20, 0)
		@game.score.should == 0
	end

	it "should score 20 for a pin each frame" do
		roll_many(20, 1)
		@game.score.should == 20
	end

	def roll_spare()
		@game.hit(5)
		@game.hit(5)
	end

	it "should score 20 when make a spare and 3 and 4 after " do
		roll_spare
		@game.hit(3)
		@game.hit(4)
		roll_many(16, 0)
		@game.score.should == 20
	end

	def roll_strike()
		@game.hit(10)
	end

	it "should score 24 when make a strike and 3 and 4 after " do
		roll_strike
		@game.hit(3)
		@game.hit(4)
		roll_many(16, 0)
		@game.score.should == 24
	end
end
</pre>
<p>They are virtually identical to UncleBob&#8217;s ones.</p>
<p>And this is my code:</p>
<pre class='brush: ruby'>
class Bowling
	private
	class Frame
		def initialize
			@rolls = []
			@rolls[0] = @rolls[1] = 0
			@index = 0
		end

		def sum
			@rolls[0]+@rolls[1]
		end

		def strike?
			@rolls[0] == 10
		end

		def spare?
			sum == 10 &#038;&#038; !strike?
		end

		def bonus_for_strike
			sum
		end

		def bonus_for_spare
			@rolls[0]
		end

		def pins=(value)
			@rolls[@index] = value
			@index = @index + 1
		end

		def finished?
			@index > 1 || strike?
		end

	end

	def add_frame?
		@frames.empty? || @frames.last.finished?
	end

	public

	def initialize
		@frames = []
	end

	def hit(pins)
		@frames << Frame.new if add_frame?
		@frames.last.pins=pins
	end

	def score
		was_spare = false
		was_strike = false
		@frames.inject(0) do |score, current_frame|
			if(was_strike)
				score = score + current_frame.bonus_for_strike
			end
			if(was_spare)
				score = score + current_frame.bonus_for_spare
			end
			was_spare = current_frame.spare?
			was_strike = current_frame.strike?

			score + current_frame.sum

		end
	end
end
</pre>
<p>I'm quite satisfied for the result, I like the encapsulation of responsibility inside <code>Frame</code>, but I don't like at all the fact I saved a state during the score's calculation: I will focus on that in next practice.  </p>
<p>At last, the environment created has been very friendly, I didn't miss Eclipse for normal developing, maybe I miss a bit a helper for extracting method, but with snippets and the Vim shortcuts, I gained a lot of productivity.</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/bdd' rel='tag' target='_self'>bdd</a>, <a class='technorati-link' href='http://technorati.com/tag/bowling' rel='tag' target='_self'>bowling</a>, <a class='technorati-link' href='http://technorati.com/tag/kata' rel='tag' target='_self'>kata</a>, <a class='technorati-link' href='http://technorati.com/tag/rspec' rel='tag' target='_self'>rspec</a>, <a class='technorati-link' href='http://technorati.com/tag/ruby' rel='tag' target='_self'>ruby</a>, <a class='technorati-link' href='http://technorati.com/tag/vi' rel='tag' target='_self'>vi</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/09/22/bowling-kata-with-ruby-and-rspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
