<?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>Kerry Buckley &#187; BT</title>
	<atom:link href="http://www.kerrybuckley.org/category/bt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrybuckley.org</link>
	<description>What's the simplest thing that could possibly go wrong?</description>
	<lastBuildDate>Wed, 30 Nov 2011 11:26:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Adastral Park code retreat</title>
		<link>http://www.kerrybuckley.org/2011/11/01/adastral-park-code-retreat/</link>
		<comments>http://www.kerrybuckley.org/2011/11/01/adastral-park-code-retreat/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 11:42:27 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[BT]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[adastral park]]></category>
		<category><![CDATA[bt]]></category>
		<category><![CDATA[code retreat]]></category>
		<category><![CDATA[conway's game of life]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.org/?p=426</guid>
		<description><![CDATA[Ever since attending code retreat at Bletchley Park (the UK leg of Corey Haines&#8217;s code retreat tour) last year, I&#8217;d been vaguely intending to try and run an internal one at work. I finally got round to it this month, when a few people expressed an interest in a code retreat as a one-off alternative [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/kerrybuckley/6258543252/" title="Code retreat poster by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6240/6258543252_6219ecbb29.jpg" width="198" height="530" alt="Code retreat poster" class="alignleft"></a></p>
<p>Ever since attending <a href="http://coderetreat.ning.com/events/code-retreat-bletchley-park">code retreat at Bletchley Park</a> (the UK leg of <a href="http://twitter.com/#!/coreyhaines">Corey Haines</a>&rsquo;s code retreat tour) last year, I&#8217;d been vaguely intending to try and run an internal one at work. I finally got round to it this month, when a few people expressed an interest in a code retreat as a one-off alternative to the usual developer conferences we run a couple of times a year. I thought I&#8217;d share my experiences facilitating a code retreat for the first time, in case it&#8217;s helpful to anyone thinking of doing the same.</p>
<h3>Beforehand</h3>
<p>The &#8220;official&#8221; <a href="http://coderetreat.com/">code retreat site</a> has some good information on things to consider when <a href="http://coderetreat.com/hosting.html">hosting</a> and <a href="http://coderetreat.com/facilitation.html">facilitating</a>. I suspect organising an internal company event is easier than a public one, because hopefully you&#8217;ll have a venue available and you won&#8217;t need to worry about sponsorship. The only one of Corey&#8217;s rules I ignored was the one about giving people decent food at lunchtime, but we did at least manage to beg enough budget (thanks Mel!) to be able to provide cheap Tesco sandwiches at lunchtime and a few random snacks and coffee during the day. I don&#8217;t feel too bad because rather than asking people to &#8220;come out at eight in the morning to spend the day coding&#8221;, we were giving them an excuse to spend a work day away from their projects, getting paid to practice.</p>
<p>We also planned to start at nine o&#8217;clock rather than the traditional eight. I think for public events the early start, usually on a Saturday, is a good way of selecting only people who really want to take part, but running it at work I wanted to swing the balance a bit towards encouraging people to come (I work on a large site with probably a couple of hundred developers scattered amongst the 3000-odd BT people, and part of the motivation for running this kind of event is to try and put developers from different teams in touch with each other).</p>
<p>With a bit of publicity via mailing lists and posters on site, 27 people had signed up. That seemed like about the right number, although I had no idea how many of those would turn up, or whether anyone would turn up without registering. I turned up at the office in plenty of time, and was lucky enough to get some last-minute tips from the master:</p>
<p><img src="http://www.kerrybuckley.org/wp-content/uploads/2011/10/corey_tips.png" alt="tips" title="Tips" width="549" height="263" class="aligncenter size-full wp-image-435" /></p>
<h3>Getting started</h3>
<p>As it turned out we had a few glitches to deal with before we could get going &ndash; firstly the venue was locked and we only managed to get in because <a href="http://twitter.com/#!/roovo">Rupert</a> knew how to find the secret back entrance, then <a href="http://twitter.com/#!/adamwhittingham">Adam</a> and <a href="http://twitter.com/#!/yedennek">Anna</a> got kicked out of Tesco for a fire alarm while buying the food &ndash; so we ended up starting at closer to 9.30.</p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290950476/" title="Coffee machine hack by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6116/6290950476_22a171bc20_t.jpg" width="91" height="100" alt="Coffee machine hack" class="alignright"></a></p>
<p>Potentially the biggest disaster though was that the hopper was missing from the coffee machine (most likely locked away to stop people like us using it with our own beans instead of paying for catering). Fortunately it takes more than a little detail like that to defeat a room full of engineers, and we soon had a workaround in place using a plastic cup with the bottom cut out.</p>
<p>Once the caffeine supply was assured and we were finally ready to start, I gave a quick presentation covering the purpose of the day, the four rules of simple design and Conway&#8217;s game of life:</p>
<div style="width:425px; margin: 0 auto" id="__ss_9945517"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/kjbuckley/adastral-park-code-retreat-introduction" title="Adastral Park code retreat introduction" target="_blank">Adastral Park code retreat introduction</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/9945517" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/" target="_blank">presentations</a> from <a href="http://www.slideshare.net/kjbuckley" target="_blank">Kerry Buckley</a> </div>
</p></div>
<h3>First session</h3>
<p>With everyone hopefully having a reasonable idea of what was going on, people paired up and started the first session. There were about 25&ndash;30 people (I forgot to count), split something like 70:20:10 between Java, Ruby and Groovy. In the first session, as expected, everyone was mostly just getting a feel for the problem, and realising that it&#8217;s not quite as simple as it appears from the list of rules.</p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290429483/" title="Getting started by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6036/6290429483_fb2f82c192_z.jpg" width="640" height="316" alt="Getting started" class="aligncenter"></a></p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290429891/" title="First session by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6218/6290429891_9912a2e934_z.jpg" width="640" height="317" alt="First session" class="aligncenter"></a></p>
<p>After 45 minutes, I put on my best exam invigilator voice and got everyone to stop typing, delete their code and gather round. Pairs were split roughly evenly between those that started with a grid and those that started with a cell, and we had a short discussion about how a grid approach using a two-dimensional array fitted in with the &#8220;infinite grid&#8221; constraint. Some of those who had started from a cell were thinking about approaches for allowing cells to know about their neighbours, but no-one had got that far yet.</p>
<h3>Mixing it up</h3>
<p>For the second session, I suggested that those that had started with a grid tried starting from a cell instead, and vice versa. There were also quite a few people who wanted a chance to try Ruby, so they tried to grab the rubyists before anyone else did. Little did they know that they would find themselves exposed to Vim (and in some cases Mac OS X) at the same time, but that&#8217;s just a bonus.</p>
<p>In general people who tried both seemed to prefer the cell approach, partly because it made it easier to test-drive the rules. With a grid, people found they were having to implement some kind of <code>to_string</code> or <code>render</code> method to allow the transformed grid to be compared with the expected result. The biggest puzzle with the cell approach was how to handle the relationship between multiple cells.</p>
<p>I introduced a list of possible constraints people might want to consider to make things more interesting:</p>
<ul>
<li>No if statements (or other conditionals).</li>
<li>No loops.</li>
<li>No methods longer than five lines (or four, or three&hellip;)</li>
<li>No more than three methods per class.</li>
<li>No &#8216;naked&#8217; language primitives.</li>
<li>No built-in data structures.</li>
</ul>
<p>I also attempted to explain <a href="http://gojko.net/2009/02/27/thought-provoking-tdd-exercise-at-the-software-craftsmanship-conference/">TDD as if you meant it</a>, in case anyone fancied giving it a go. Unfortunately my description was rather garbled, not helped by the fact that I&#8217;m not sure I fully understand it myself (I&#8217;m still annoyed that I picked the wrong track at <a href="/2009/03/02/software-craftsmanship-2009/">SC2009</a> and missed Keith&#8217;s original presentation of the exercise, and it turns out Adam made the same mistake at SPA2010).</p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290432001/" title="Pairing by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6106/6290432001_aef3d32e26_z.jpg" width="320" height="240" alt="Pairing" class="alignleft"></a></p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290952678/" title="Flying fingers by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6036/6290952678_48402a5675_z.jpg" width="320" height="240" alt="Flying fingers" class="alignright"></a></p>
<h3 class="clear" style="padding-top: 1em;">Session three</h3>
<p>By the third session several pairs were starting to experiment with storing cell objects either in a set (with each cell knowing its position) or a hash, keyed by position. Some people used a comma-separated &#8220;x,y&#8221; string as the key, but with a bit of prodding most ended up replacing it with a point class of some kind.</p>
<p>One pair decided to start with a single cell, then initialise other cells with their neighbours. This initially seemed promising, but soon led to complications with circular references, as well as the need to keep dead cells around to maintain a connection between cells with space between them.</p>
<p>Another pair started developing the rules with a cell class, and initially had an <code>alive?</code> method which performed the life calculation. After some discussion they split the class into two &ndash; one for a living cell and another for a dead one. This removed a level of nesting from the calculations (which ended up in a different method), but interestingly left <code>alive?</code> in both classes, hardcoded to true and false respectively. We decided that this would have looked less odd in Java, where the method would have been declared abstractly in a superclass or interface.</p>
<p>One pair had decided to try implementing a solution with no if statements, but resorted to cheating by using a case instead!</p>
<p>In general, a lot of people &ndash; particularly those new to TDD &ndash; were starting to get frustrated, feeling like they were just getting to the point where they were &#8220;about to start the interesting bit&#8221; as the 45 minutes were up. I told them that we&#8217;d just have two sessions after lunch rather than three, with the second session lengthened to give people a chance of implementing a working game.</p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290951344/" title="The countdown begins by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6117/6290951344_7bd41d6f6c_z.jpg" width="640" height="378" alt="The countdown begins" class="aligncenter"></a></p>
<h3>Fourth &ndash; and as it turned out final &ndash; session</h3>
<p>After lunch we started the fourth session, but it soon became clear that several pairs had already decided to abandon TDD and clean code in favour of trying to solve the problem. I completely understand how they felt &ndash; I wasted most of the sessions at the code retreat I attended simply pushing for the finish line instead of letting it go and concentrating on perfecting the internal quality of the code &ndash; but it seemed like we&#8217;d lost sight of the key goal of the day a little. I suspect I&#8217;m mostly to blame for that, and I&#8217;m sure a more experienced facilitator could have done a better job of gently guiding people in the right direction. Anyway, it seemed sensible to bow to the inevitable, let everyone carry on for another 45 minutes and wrap up a bit earlier than planned.</p>
<p><a href="http://www.flickr.com/photos/kerrybuckley/6290953086/" title="Basking in the glow by Kerry Buckley, on Flickr"><img src="http://farm7.static.flickr.com/6058/6290953086_66f867669c_z.jpg" width="640" height="306" alt="Basking in the glow" class="aligncenter"></a></p>
<p>With the extra time, a few pairs did manage to get a working implementation, but the prize for the most impressive (cancelled out by a booby prize for abandoning TDD and neglecting the quality of their code!) goes to <a href="http://twitter.com/#!/jon_d_leach">Jon</a> and Jia Yan, who produced an SWT app that allowed graphical editing of the grid, variable speed and saving and loading files. They used the &#8220;set of live cells&#8221; approach, with an effectively infinite grid. Resizing the window on screen allowed you to show or hide cells, but the ones off the edge of the visible area continued to multiply.</p>
<p><object type="application/x-shockwave-flash" width="400" height="225" class="aligncenter" data="http://www.flickr.com/apps/video/stewart.swf?v=109786" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=72b87bb2a3&#038;photo_id=6294084155"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=109786"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=109786" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=72b87bb2a3&#038;photo_id=6294084155" height="225" width="400"></embed></object></p>
<p>Matthew and Rupert decided to look at <a href="http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/">someone else&#8217;s solution in Clojure</a>, to understand how a functional approach using list comprehensions can lead to a very concise solution. They also played around with a Ruby version using the same techniques. Looking at the Clojure code led to a slightly rearranged version of the rules for life at a particular location after a clock tick:</p>
<ol>
<li>Cells which had three neighbours are alive</li>
<li>Cells which had two neighbours are unchanged</li>
<li>All other cells are dead</li>
</ol>
<p>Paul, stuck on his own after his pair got dragged off onto a conference call (the perils of being a senior manager!), had a play with TDD as if you meant it. I think by the end of the session he was starting to get somewhere. Further than me, at any rate.</p>
<h3>Wrapping it all up</h3>
<p>After letting Jon show off a glider gun running in their SWT universe, we gathered round for the closing circle, asking everyone what they&#8217;d learned, what had surprised them, and what they&#8217;d do differently back at work on Monday as a result. Probably the most common comments revolved around the fact that neither of the two most obvious solutions turned out to be the best, suggesting that it&#8217;s worth spiking a few alternative approaches before heading blindly off down what may be a blind alley. Quite a few people were new to pairing, TDD or both, and generally seemed to agree that both provided benefits, although a couple of people felt that TDD was making things take much longer. Reassuringly, of the pairs that had abandoned TDD to concentrate on trying to get a working solution, most had found themselves descending into confusion without the tests to guide them.</p>
<h3>Final thoughts</h3>
<p>Overall, the event seemed to be a success. For quite a few people this was their first experience of TDD, and the opportunity to get first-hand experience by pairing with someone more experienced seemed to be useful. I was particularly pleased to see someone who&#8217;s recently come back into software development after a long time away doing a great job of holding his pair back from rushing into implementation without a failing test.</p>
<p>On a less positive note, I didn&#8217;t feel we really concentrated enough on &#8220;perfect&#8221; code and the four rules of simple design. I know from experience that it&#8217;s hard to let go of the natural desire to try and finish solving the problem, and I felt that a lot of people were getting frustrated enough without me peering over their shoulders picking holes in their code (however nicely I tried to do it). Perhaps this is a case of horses for courses, with the concentration on design perfection more suitable for a different mix of participants, or perhaps it just reflects my lack of experience facilitating this kind of event.</p>
<p>Also, I don&#8217;t think we really had enough sessions for the code retreat magic to start kicking in properly. We&#8217;d planned to have the recommended five or six, but a combination of the late start and a general loss of direction after lunch reduced that to four (with the fourth being double-length).</p>
<p>The important thing, of course, is that people learned something by participating, and I hope that was the case. Maybe we&#8217;ll do another one one day, and use the experience of this one to make it even better.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2011/11/01/adastral-park-code-retreat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>InfoWorld Article on BT and Agile</title>
		<link>http://www.kerrybuckley.org/2008/03/12/infoworld-article-on-bt-and-agile/</link>
		<comments>http://www.kerrybuckley.org/2008/03/12/infoworld-article-on-bt-and-agile/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 09:52:49 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[BT]]></category>

		<guid isPermaLink="false">http://kerry.ontoa.st/2008/03/12/infoworld-article-on-bt-and-agile/</guid>
		<description><![CDATA[Some time last year I was interviewed by Tom Hoffman for Infoworld, as part of a piece he was writing on BT&#8217;s (ongoing) transition to an agile software development model. Here&#8217;s the article: BT: A case study in agile programming]]></description>
			<content:encoded><![CDATA[<p>Some time last year I was interviewed by Tom Hoffman for Infoworld, as part of a piece he was writing on BT&#8217;s (ongoing) transition to an agile software development model. Here&#8217;s the article:</p>
<p><a href="http://www.infoworld.com/article/08/03/11/BT-a-case-study-in-agile-programming_1.html">BT: A case study in agile programming</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2008/03/12/infoworld-article-on-bt-and-agile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No Internet and no Software</title>
		<link>http://www.kerrybuckley.org/2007/11/02/no-internet-and-no-software/</link>
		<comments>http://www.kerrybuckley.org/2007/11/02/no-internet-and-no-software/#comments</comments>
		<pubDate>Fri, 02 Nov 2007 13:21:27 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[BT]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/11/02/no-internet-and-no-software/</guid>
		<description><![CDATA[One of the questions for the panel at Wednesday's Open Source event at BT Centre concerned the fact that most of the company doesn't have a connection to the Internet; just proxied web access. Also, installing any software not on a relatively small approved list is forbidden. "Given that we don't have the Internet or [...]]]></description>
			<content:encoded><![CDATA[<p>One of the questions for the panel at Wednesday's Open Source event at BT Centre concerned the fact that most of the company doesn't have a connection to the Internet; just proxied web access. Also, installing any software not on a relatively small approved list is forbidden.</p>
<blockquote><p>"Given that we don't have the Internet or any software, how are we supposed to work with Open Source?"</p></blockquote>
<p><a href="http://blogs.law.harvard.edu/doc/about/">Doc Searls</a>'s response:</p>
<blockquote><p>"The current policy is <strong>freaking insane!</strong> It's utterly inconsistent with BT's strategy, and I can't understand why anyone would want to come and work here... Maybe we ought to go and storm the head office or something."
</p></blockquote>
<p>Technorati Tags: <a href="http://technorati.com/tag/policy" rel="tag">policy</a>, <a href="http://technorati.com/tag/internet" rel="tag"> internet</a>, <a href="http://technorati.com/tag/open+source" rel="tag"> open source</a>, <a href="http://technorati.com/tag/doc+searls" rel="tag"> doc searls</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/11/02/no-internet-and-no-software/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Concurrent design and development &#8211; a better spin?</title>
		<link>http://www.kerrybuckley.org/2007/09/26/concurrent-design-and-development-a-better-spin/</link>
		<comments>http://www.kerrybuckley.org/2007/09/26/concurrent-design-and-development-a-better-spin/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 21:24:35 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[BT]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/09/26/concurrent-design-and-development-a-better-spin/</guid>
		<description><![CDATA[Another day, another argument discussion with a colleague over the common misapprehension that you can have a process with discrete requirements capture, design, development and test phases, yet somehow still claim to be agile. Once again, I tried (with limited success) to explain that you don't need to do all your designing up front before [...]]]></description>
			<content:encoded><![CDATA[<p>Another day, another <del datetime="2007-09-26T16:53:31+00:00">argument</del> discussion with a colleague over the common misapprehension that you can have a process with discrete requirements capture, design, development and test phases, yet somehow still claim to be agile. Once again, I tried (with limited success) to explain that you don't need to do all your designing up front before you start writing code, and that starting to write code almost immediately isn't 'hacking', provided that you are continuously paying attention to good design as you go.</p>
<p>In other words, agile developers do just as much designing as those still following waterfall methods, but we do it at the same time as writing the code.</p>
<p>Reflecting afterwards, I realised that I've been phrasing the message the wrong way. As anyone who really 'gets' test-driven (and particularly behaviour-driven) development knows, it's not really about the testing, but about the design process. A waterfall 'designer' starts from an understanding of the problem and builds up some kind of model for a solution, which they then pass on to the implementors An agile developer does exactly the same, but the language they use for the model happens to be executable source code rather than documents or UML. This choice of modeling language has the rather large benefit of being testable, and once your design is finished, you're done. No need for someone to try to interpret and implement it. No scope for misunderstandings between designer and developer. No danger that the design is incomplete, or that part of it turns out to be unworkable.</p>
<p>It's not that we start writing code and design as we go along.</p>
<p><strong>We are always designing &ndash; we just write the code as we go along.</strong></p>
<p>Technorati Tags: <a href="http://technorati.com/tag/agile" rel="tag">agile</a>, <a href="http://technorati.com/tag/tdd" rel="tag"> tdd</a>, <a href="http://technorati.com/tag/bdd" rel="tag"> bdd</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/09/26/concurrent-design-and-development-a-better-spin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A step-by-step BDD example using RSpec</title>
		<link>http://www.kerrybuckley.org/2007/06/17/a-step-by-step-bdd-example-using-rspec/</link>
		<comments>http://www.kerrybuckley.org/2007/06/17/a-step-by-step-bdd-example-using-rspec/#comments</comments>
		<pubDate>Sun, 17 Jun 2007 10:39:34 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[BT]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/06/17/a-step-by-step-bdd-example-using-rspec/</guid>
		<description><![CDATA[We've now got a Ruby focus group at work, and one of the first things to be set up has been a weekly programming exercise [intranet link], in the style of Ruby Quiz. It's now week two, and the problem is slightly more complex than last week's gentle FizzBuzz introduction. Here's the specification: This time, [...]]]></description>
			<content:encoded><![CDATA[<p>We've now got a Ruby focus group at work, and one of the first things to be set up has been a <a href="http://professionalcommunities.intra.bt.com/confluence/display/RFG/Numbers+to+words">weekly programming exercise</a> [intranet link], in the style of <a href="http://www.rubyquiz.com/">Ruby Quiz</a>. It's now week two, and the problem is slightly more complex than last week's gentle <a href="http://en.wikipedia.org/wiki/Bizz_buzz">FizzBuzz</a> introduction. Here's the specification:</p>
<blockquote><p>
This time, the challenge is to come up with some Ruby code that converts a positive integer to its English language<br />
equivalent. For example:</p>
<p>1 => one</p>
<p>10 => ten</p>
<p>123 => one hundred and twenty three</p>
<p>10,456 => ten thousand four hundred and fifty six</p>
<p>1,234,123 => one million two hundred thirty four thousand one hundred and twenty three</p>
<p>The code should work from numbers 1 - 10,000,000,000 (ten billion) but if it works for bigger numbers its all good.</p>
<p>For an extra challenge, when the strings for the numbers for 1 - 10,000,000,000 are sorted alphabetically, which is the<br />
first odd number in the list?
</p></blockquote>
<p>I thought it might be interesting (to me, at least!) to record the process I go through to reach the solution, rather than just sharing the finished article. I'm using a <a href="http://behaviour-driven.org/">behaviour-driven</a> approach, although the process for writing a single method obviously doesn't touch on a lot of the wider aspects of BDD.</p>
<p>So here it is, warts and all (I'm writing this as I go along, so I have no idea how long this post is going to get, or whether I'll even arrive at a solution at all!)<br />
<span id="more-129"></span></p>
<p>First, let's describe the very simplest bit of behaviour: if I feed in 1, the output should be 'one'. The obvious approach is to add a <code>to_words</code> method to the <code>Integer</code> class. Obvious to those who have their Ruby heads on, that is &ndash; to those more used to languages like Java, it probably sounds like the ravings of a mentalist).</p>
<p>Let's create a specification file called <code>to_words_spec.rb</code> (I'm using <a href="http://rspec.rubyforge.org/">rspec</a>):</p>
<div class="igBar"><span id="lruby-41"><a href="#" onclick="javascript:showCodeTxt('ruby-41'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-41">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"1.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#996600;">"should be 'one'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006666;color:#800000;">1</span>.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == 'one'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>What happens when we run it?:</p>
<pre>
$ spec to_words_spec.rb
F

1)
NoMethodError in '1.to_words should be 'one''
undefined method `to_words' for 1:Fixnum
./to_words_spec.rb:3:

Finished in 0.009689 seconds

1 example, 1 failure
</pre>
<p>No surprise there. I'll create another file, <code>to_words.rb</code>, where I define the method (for now I'll leave it empy):</p>
<div class="igBar"><span id="lruby-42"><a href="#" onclick="javascript:showCodeTxt('ruby-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-42">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">Integer</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Require this at the top of <code>to_words_spec.rb</code>:</p>
<div class="igBar"><span id="lruby-43"><a href="#" onclick="javascript:showCodeTxt('ruby-43'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-43">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> 'to_words' </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And run the spec again:</p>
<pre>
F

1)
'1.to_words should be 'one'' FAILED
expected "one", got nil (using ==)
./to_words_spec.rb:5:

Finished in 0.009579 seconds

1 example, 1 failure
</pre>
<p>OK, so the method's there now, but we haven't got a return value. The easiest thing to do to make this spec pass is to just hardcode the return value:</p>
<div class="igBar"><span id="lruby-44"><a href="#" onclick="javascript:showCodeTxt('ruby-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-44">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; 'one'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.

Finished in 0.010926 seconds

1 example, 0 failures
</pre>
<p>The fact that we've hardcoded it means we need another example to describe the required behaviour for different inputs:</p>
<div class="igBar"><span id="lruby-45"><a href="#" onclick="javascript:showCodeTxt('ruby-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-45">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"2.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#996600;">"should be 'two'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006666;color:#800000;">2</span>.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == 'two'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.F

1)
'2.to_words should be 'two'' FAILED
expected "two", got "one" (using ==)
./to_words_spec.rb:11:

Finished in 0.019927 seconds

2 examples, 1 failure
</pre>
<p>Let's make the code a bit more intelligent. I think I'll use an array to hold the names of the digits, but for now I'll only populate it with enough data to pass the spec.</p>
<div class="igBar"><span id="lruby-46"><a href="#" onclick="javascript:showCodeTxt('ruby-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-46">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># we never use zero, but it keeps the indexes inline</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span>'zero', 'one', 'two'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..

Finished in 0.010498 seconds

2 examples, 0 failures
</pre>
<p>Now I could paste the same example in a few times and change the numbers, but that would be a bit ugly. This looks better, and I think it's still obvious what's going on:</p>
<div class="igBar"><span id="lruby-47"><a href="#" onclick="javascript:showCodeTxt('ruby-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-47">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># Single digits</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;color:#800000;">1</span>=&gt;'one', <span style="color:#006666;color:#800000;">2</span>=&gt;'two', <span style="color:#006666;color:#800000;">3</span>=&gt;'three', <span style="color:#006666;color:#800000;">4</span>=&gt;'four', <span style="color:#006666;color:#800000;">5</span>=&gt;'five', <span style="color:#006666;color:#800000;">6</span>=&gt;'six', <span style="color:#006666;color:#800000;">7</span>=&gt;'seven',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006666;color:#800000;">8</span>=&gt;'eight', <span style="color:#006666;color:#800000;">9</span>=&gt;'nine'<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number, word|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '#{word}'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == word</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
FF.F.FFFF

1)
'5.to_words should be 'five'' FAILED
expected "five", got nil (using ==)
./to_words_spec.rb:8:

&hellip;

7)
'4.to_words should be 'four'' FAILED
expected "four", got nil (using ==)
./to_words_spec.rb:8:

Finished in 0.021579 seconds

9 examples, 7 failures
</pre>
<p>If I add the missing numbers, it should pass.</p>
<div class="igBar"><span id="lruby-48"><a href="#" onclick="javascript:showCodeTxt('ruby-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-48">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># we never use zero, but it keeps the indexes inline</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span>'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If you add the -f s (short for --format specdoc) option, it's easy to see the examples that are being dynamically generated:</p>
<pre>
$ spec -f s to_words_spec.rb

5.to_words
- should be 'five'

6.to_words
- should be 'six'

1.to_words
- should be 'one'

7.to_words
- should be 'seven'

2.to_words
- should be 'two'

8.to_words
- should be 'eight'

3.to_words
- should be 'three'

9.to_words
- should be 'nine'

4.to_words
- should be 'four'

Finished in 0.020708 seconds

9 examples, 0 failures
</pre>
<p>Because I'm using a hash, the examples aren't run in numerical order. It's not ideal, but I don't think it's worth complicating the spec to fix it.</p>
<p>An interesting point from the previous failures though &ndash; what should it do if the number falls outside the range it can cope with? For the sake of argument, I'll make something up. To start with, the only numbers it can handle are one to nine: I'll change the example as I expand the range. I'll put 10,000,000,001 in now anyway.</p>
<div class="igBar"><span id="lruby-49"><a href="#" onclick="javascript:showCodeTxt('ruby-49'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-49">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># Examples of unhandled numbers</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>-<span style="color:#006666;color:#800000;">123</span>, -<span style="color:#006666;color:#800000;">1</span>, <span style="color:#006666;color:#800000;">0</span>, <span style="color:#006666;color:#800000;">10</span>, <span style="color:#006666;color:#800000;">10</span>_000_000_001<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '?'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == '?'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.........FFFFF

1)
'-123.to_words should be '?'' FAILED
expected "?", got nil (using ==)
./to_words_spec.rb:17:

2)
'-1.to_words should be '?'' FAILED
expected "?", got "nine" (using ==)
./to_words_spec.rb:17:

3)
'0.to_words should be '?'' FAILED
expected "?", got "zero" (using ==)
./to_words_spec.rb:17:

4)
'10.to_words should be '?'' FAILED
expected "?", got nil (using ==)
./to_words_spec.rb:17:

5)
RangeError in '10000000001.to_words should be '?''
bignum too big to convert into `long'
./to_words.rb:5:in `[]'
./to_words.rb:5:in `to_words'
./to_words_spec.rb:17:

Finished in 0.026465 seconds

14 examples, 5 failures
</pre>
<p>Hmm, a few unexpected things going on here. Let's fix the easy ones first by just returning a question mark if we don't find a value. Also, putting zero in that numbers array when none of the examples required it has come back to bite me. I'll change that to nil.</p>
<div class="igBar"><span id="lruby-50"><a href="#" onclick="javascript:showCodeTxt('ruby-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-50">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># we never use zero, but it keeps the indexes inline</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">or</span> '?'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..........F..F

1)
'-1.to_words should be '?'' FAILED
expected "?", got "nine" (using ==)
./to_words_spec.rb:17:

2)
RangeError in '10000000001.to_words should be '?''
bignum too big to convert into `long'
./to_words.rb:4:in `[]'
./to_words.rb:4:in `to_words'
./to_words_spec.rb:17:

Finished in 0.024623 seconds

14 examples, 2 failures
</pre>
<p>Still a couple of problems. The first one is obviously caused by the fact that in Ruby a negative index on an array means 'count back from the end', so I'll explicitly check for zero or negative numbers.</p>
<div class="igBar"><span id="lruby-51"><a href="#" onclick="javascript:showCodeTxt('ruby-51'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-51">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> &lt;= <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">or</span> '?'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.............F

1)
RangeError in '10000000001.to_words should be '?''
bignum too big to convert into `long'
./to_words.rb:5:in `[]'
./to_words.rb:5:in `to_words'
./to_words_spec.rb:17:

Finished in 0.024365 seconds

14 examples, 1 failure
</pre>
<p>You may have noticed I've been leaving that one until last. Now everything else works, it's time to bite the bullet and figure out exactly what's going on here. It obviously doesn't like doing <code>numbers[10000000001]</code>, but why is it converting it to a long? I didn't even think Ruby had a long type!</p>
<p>Time to <a href="http://corelib.rubyonrails.org/classes/Array.html#M000398">RTFM</a> (Incidentally, I always use <a href="http://www.gotapi.com/rubyrails">gotapi</a> for browsing and searching API docs). No mention of longs in the description, but a peek at the source shows that the underlying C code is doing things like <code>beg = NUM2LONG(argv[0]);</code>. Not to worry though &ndash; it's not like I'm going to be just creating an array of all possible values, so I'll just check the upper range explicitly too.</p>
<div class="igBar"><span id="lruby-52"><a href="#" onclick="javascript:showCodeTxt('ruby-52'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-52">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..............

Finished in 0.023524 seconds

14 examples, 0 failures
</pre>
<p>Right, now we can get back to the real task. The words for 10&ndash;19 are mostly irregular, so I'll just add them to the list for 1&ndash;9.</p>
<div class="igBar"><span id="lruby-53"><a href="#" onclick="javascript:showCodeTxt('ruby-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-53">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># 1-19</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;color:#800000;">1</span>=&gt;'one', <span style="color:#006666;color:#800000;">2</span>=&gt;'two', <span style="color:#006666;color:#800000;">3</span>=&gt;'three', <span style="color:#006666;color:#800000;">4</span>=&gt;'four', <span style="color:#006666;color:#800000;">5</span>=&gt;'five', <span style="color:#006666;color:#800000;">6</span>=&gt;'six', <span style="color:#006666;color:#800000;">7</span>=&gt;'seven',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006666;color:#800000;">8</span>=&gt;'eight', <span style="color:#006666;color:#800000;">9</span>=&gt;'nine', <span style="color:#006666;color:#800000;">10</span>=&gt;'ten', <span style="color:#006666;color:#800000;">11</span>=&gt;'eleven', <span style="color:#006666;color:#800000;">12</span>=&gt;'twelve', <span style="color:#006666;color:#800000;">13</span>=&gt;'thirteen',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006666;color:#800000;">14</span>=&gt;'fourteen', <span style="color:#006666;color:#800000;">15</span>=&gt;'fifteen', <span style="color:#006666;color:#800000;">16</span>=&gt;'sixteen', <span style="color:#006666;color:#800000;">17</span>=&gt;'seventeen',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006666;color:#800000;">18</span>=&gt;'eighteen', <span style="color:#006666;color:#800000;">19</span>=&gt;'nineteen'<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number, word|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '#{word}'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == word</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.....F.FF.F.F.F.F.F..F.F

1)
'16.to_words should be 'sixteen'' FAILED
expected "sixteen", got "?" (using ==)
./to_words_spec.rb:19:

&hellip;

10)
'10.to_words should be 'ten'' FAILED
expected "ten", got "?" (using ==)
./to_words_spec.rb:19:

Finished in 0.040177 seconds

24 examples, 10 failures
</pre>
<p>I'll just add them to the array in the code too (remembering to increase the allowable range):</p>
<div class="igBar"><span id="lruby-54"><a href="#" onclick="javascript:showCodeTxt('ruby-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-54">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">19</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
...F....................

1)
'10.to_words should be '?'' FAILED
expected "?", got "ten" (using ==)
./to_words_spec.rb:7:

Finished in 0.039858 seconds

24 examples, 1 failure
</pre>
<p>Oops, forgot to modify the examples for out-of-range behaviour.</p>
<div class="igBar"><span id="lruby-55"><a href="#" onclick="javascript:showCodeTxt('ruby-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-55">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># Examples of unhandled numbers</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>-<span style="color:#006666;color:#800000;">123</span>, -<span style="color:#006666;color:#800000;">1</span>, <span style="color:#006666;color:#800000;">0</span>, <span style="color:#006666;color:#800000;">20</span>, <span style="color:#006666;color:#800000;">10</span>_000_000_001<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '?'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == '?'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
# Examples of unhandled numbers
........................

Finished in 0.060643 seconds

24 examples, 0 failures
</pre>
<p>Now let's do 20, 30 etc. Once they're done, that should be it for the actual numbers, and we can start stringing them together.</p>
<div class="igBar"><span id="lruby-56"><a href="#" onclick="javascript:showCodeTxt('ruby-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-56">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># 20, 30 ... 90</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;color:#800000;">20</span>=&gt;'twenty', <span style="color:#006666;color:#800000;">30</span>=&gt;'thirty', <span style="color:#006666;color:#800000;">40</span>=&gt;'forty', <span style="color:#006666;color:#800000;">50</span>=&gt;'fifty', <span style="color:#006666;color:#800000;">60</span>=&gt;'sixty',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006666;color:#800000;">70</span>=&gt;'seventy', <span style="color:#006666;color:#800000;">80</span>=&gt;'eighty', <span style="color:#006666;color:#800000;">90</span>=&gt;'ninety'<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number, word|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '#{word}'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == word</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
........................FFFFFFFF

1)
'60.to_words should be 'sixty'' FAILED
expected "sixty", got "?" (using ==)
./to_words_spec.rb:29:

&hellip;

8)
'70.to_words should be 'seventy'' FAILED
expected "seventy", got "?" (using ==)
./to_words_spec.rb:29:

Finished in 0.045908 seconds

32 examples, 8 failures
</pre>
<p>There's going to be extra logic required for numbers over twenty (so far we're only looking at the round ones, and ignoring 21, 22 etc), so I'll split them out in the code.</p>
<div class="igBar"><span id="lruby-57"><a href="#" onclick="javascript:showCodeTxt('ruby-57'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-57">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">99</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; decades = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'seventy', 'eighty', 'ninety'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">19</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This time I remember to change the out-of-range example too:</p>
<div class="igBar"><span id="lruby-58"><a href="#" onclick="javascript:showCodeTxt('ruby-58'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-58">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>-<span style="color:#006666;color:#800000;">123</span>, -<span style="color:#006666;color:#800000;">1</span>, <span style="color:#006666;color:#800000;">0</span>, <span style="color:#006666;color:#800000;">100</span>, <span style="color:#006666;color:#800000;">10</span>_000_000_001<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '?'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == '?'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
................................

Finished in 0.053969 seconds

32 examples, 0 failures
</pre>
<p>Looking good. Now to start building up more interesting numbers. I'll word the examples a bit differently from now on, so that it's obvious what general behaviour they're actually describing. Let's start with two digit numbers.</p>
<div class="igBar"><span id="lruby-59"><a href="#" onclick="javascript:showCodeTxt('ruby-59'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-59">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A two-digit number above 20 that's not divisible by ten, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#996600;">"should be '&lt;decade for tens&gt;-&lt;units&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006666;color:#800000;">21</span>.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == 'twenty-one'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
................................F

1)
'A two-digit number above 20 that's not divisible by ten, in words should be '<decade for tens>-<units>'' FAILED
expected "twenty-one", got "twenty" (using ==)
./to_words_spec.rb:36:

Finished in 0.051832 seconds

33 examples, 1 failure
</pre>
<p>First of all, I'll take the naive approach of adding the hyphen and the units number every time.</p>
<div class="igBar"><span id="lruby-60"><a href="#" onclick="javascript:showCodeTxt('ruby-60'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-60">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> + '-' + numbers<span style="color:#006600; font-weight:bold;">&#91;</span>self%<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
........................FFFFFFFF.

1)
TypeError in '60.to_words should be 'sixty''
can't convert nil into String
./to_words.rb:13:in `+'
./to_words.rb:13:in `to_words'
./to_words_spec.rb:29:

&hellip;

8)
TypeError in '70.to_words should be 'seventy''
can't convert nil into String
./to_words.rb:13:in `+'
./to_words.rb:13:in `to_words'
./to_words_spec.rb:29:

Finished in 0.049627 seconds

33 examples, 8 failures
</pre>
<p>My new example passes, but as expected I've broken a load of others. This is good, because it shows that I'm building up a good test coverage. A slightly more sensible implementation:</p>
<div class="igBar"><span id="lruby-61"><a href="#" onclick="javascript:showCodeTxt('ruby-61'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-61">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">10</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : <span style="color:#006600; font-weight:bold;">&#40;</span>'-' + numbers<span style="color:#006600; font-weight:bold;">&#91;</span>self%<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.................................

Finished in 0.04624 seconds

33 examples, 0 failures
</pre>
<p>It's probably worth trying another couple of numbers too, including some boundary conditions. I'm not really sure what text to put in the specs now though, as they're just different examples of the same behaviour. I could create a new context and call it '<em>Another</em> two-digit number&hellip;', but I think I'll just give them both the same name. This isn't something I've done before, so I haven't decided yet whether it's a good thing or not. I'll factor out the duplication as I go.</p>
<div class="igBar"><span id="lruby-62"><a href="#" onclick="javascript:showCodeTxt('ruby-62'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-62">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A two-digit number above 20 that's not divisible by ten, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;color:#800000;">21</span>=&gt;'twenty-one', <span style="color:#006666;color:#800000;">42</span>=&gt;'forty-two', <span style="color:#006666;color:#800000;">69</span>=&gt;'sixty-nine',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006666;color:#800000;">99</span>=&gt;'ninety-nine'<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number, word|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;decade for tens&gt;-&lt;units&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == word</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
....................................

Finished in 0.048549 seconds

36 examples, 0 failures
</pre>
<p>Good, it still works. I could add more, but I think that's enough.</p>
<p>Next thing to attack is probably the round hundreds. I've decided there's no point in having to change the out-of-range example every time, so I've changed it to only try the really big number.</p>
<div class="igBar"><span id="lruby-63"><a href="#" onclick="javascript:showCodeTxt('ruby-63'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-63">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># Examples of unhandled numbers</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>-<span style="color:#006666;color:#800000;">123</span>, -<span style="color:#006666;color:#800000;">1</span>, <span style="color:#006666;color:#800000;">0</span>, <span style="color:#006666;color:#800000;">10</span>_000_000_001<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '?'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == '?'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9900CC;">describe</span> <span style="color:#996600;">"A three-digit number divisible by one hundred, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>, <span style="color:#006666;color:#800000;">5</span>, <span style="color:#006666;color:#800000;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |digit|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;hundreds&gt; hundred'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>digit*<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == digit.<span style="color:#9900CC;">to_words</span> + ' hundred'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note how I've used to_words itself in the matcher argument. I can get away with that because I've already tested its behaviour for the numbers I'm using.</p>
<pre>
...................................FFF

1)
'A three-digit number that's divisible by one hundred, in words should be '<hundreds> hundred'' FAILED
expected "one hundred", got "?" (using ==)
./to_words_spec.rb:46:

2)
'A three-digit number that's divisible by one hundred, in words should be '<hundreds> hundred'' FAILED
expected "five hundred", got "?" (using ==)
./to_words_spec.rb:46:

3)
'A three-digit number that's divisible by one hundred, in words should be '<hundreds> hundred'' FAILED
expected "nine hundred", got "?" (using ==)
./to_words_spec.rb:46:

Finished in 0.049282 seconds

38 examples, 3 failures
</pre>
<p>Should be easy enough&hellip;</p>
<div class="igBar"><span id="lruby-64"><a href="#" onclick="javascript:showCodeTxt('ruby-64'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-64">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">10</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; decades = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'seventy', 'eighty', 'ninety'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">19</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">10</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : <span style="color:#006600; font-weight:bold;">&#40;</span>'-' + numbers<span style="color:#006600; font-weight:bold;">&#91;</span>self%<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">100</span>...<span style="color:#006666;color:#800000;">999</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#93;</span> + ' hundred'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
......................................

Finished in 0.048804 seconds

38 examples, 0 failures
</pre>
<p>Let's fill in the gaps between the hundreds.</p>
<div class="igBar"><span id="lruby-65"><a href="#" onclick="javascript:showCodeTxt('ruby-65'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-65">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A three-digit number that's not divisible by one hundred, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">101</span>, <span style="color:#006666;color:#800000;">150</span>, <span style="color:#006666;color:#800000;">666</span>, <span style="color:#006666;color:#800000;">999</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;hundreds&gt; hundred and &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' hundred <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
......................................FFFF

1)
'A three-digit number that's not divisible by one hundred, in words should be '<hundreds> hundred and <remainder>'' FAILED
expected "one hundred and one", got "one hundred" (using ==)
./to_words_spec.rb:54:

2)
'A three-digit number that's not divisible by one hundred, in words should be '<hundreds> hundred and <remainder>'' FAILED
expected "one hundred and fifty", got "one hundred" (using ==)
./to_words_spec.rb:54:

3)
'A three-digit number that's not divisible by one hundred, in words should be '<hundreds> hundred and <remainder>'' FAILED
expected "six hundred and sixty-six", got "six hundred" (using ==)
./to_words_spec.rb:54:

4)
'A three-digit number that's not divisible by one hundred, in words should be '<hundreds> hundred and <remainder>'' FAILED
expected "nine hundred and ninety-nine", got nil (using ==)
./to_words_spec.rb:54:

Finished in 0.056888 seconds

42 examples, 4 failures
</pre>
<div class="igBar"><span id="lruby-66"><a href="#" onclick="javascript:showCodeTxt('ruby-66'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-66">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">100</span>...<span style="color:#006666;color:#800000;">999</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#93;</span> + ' hundred' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : ' <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.........................................F

1)
'A three-digit number that's not divisible by one hundred, in words should be '<hundreds> hundred and <remainder>'' FAILED
expected "nine hundred and ninety-nine", got nil (using ==)
./to_words_spec.rb:54:

Finished in 0.050711 seconds

42 examples, 1 failure
</pre>
<p>That's odd. Oh, I see &ndash; there's an extra dot in the range (100...999 instead of 100..99), which means it excludes the last number. Let's fix that and run it again.</p>
<div class="igBar"><span id="lruby-67"><a href="#" onclick="javascript:showCodeTxt('ruby-67'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-67">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">100</span>..<span style="color:#006666;color:#800000;">999</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#93;</span> + ' hundred' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : ' <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..........................................

Finished in 0.054811 seconds

42 examples, 0 failures
</pre>
<p>That's better.</p>
<p>From now on, it ought to be plain sailing. Each group of three digits needs to get converted on its own, then have the appropriate multiplier ('thousand', 'million' or 'billion') appended (and maybe a comma). Let's start with the thousands.</p>
<div class="igBar"><span id="lruby-68"><a href="#" onclick="javascript:showCodeTxt('ruby-68'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-68">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A four, five or six-digit number that's divisible by one thousand, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_000, <span style="color:#006666;color:#800000;">23</span>_000, <span style="color:#006666;color:#800000;">456</span>_000, <span style="color:#006666;color:#800000;">999</span>_000<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;thousands&gt; thousand'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..........................................FFFF

1)
'A four, five or six-digit number that's divisible by one thousand, in words should be '
<thousands> thousand'' FAILED
expected "one thousand", got nil (using ==)
./to_words_spec.rb:62:

&hellip;

4)
'A four, five or six-digit number that's divisible by one thousand, in words should be '
<thousands> thousand'' FAILED
expected "nine hundred and ninety-nine thousand", got nil (using ==)
./to_words_spec.rb:62:

Finished in 0.072061 seconds

46 examples, 4 failures
</pre>
<div class="igBar"><span id="lruby-69"><a href="#" onclick="javascript:showCodeTxt('ruby-69'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-69">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">19</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">10</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : <span style="color:#006600; font-weight:bold;">&#40;</span>'-' + numbers<span style="color:#006600; font-weight:bold;">&#91;</span>self%<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">100</span>..<span style="color:#006666;color:#800000;">999</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#93;</span> + ' hundred' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : ' <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000..<span style="color:#006666;color:#800000;">999</span>_999</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..............................................

Finished in 0.053842 seconds

46 examples, 0 failures
</pre>
<div class="igBar"><span id="lruby-70"><a href="#" onclick="javascript:showCodeTxt('ruby-70'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-70">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A four, five or six-digit number that's not divisible by one thousand, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_234, <span style="color:#006666;color:#800000;">23</span>_456, <span style="color:#006666;color:#800000;">345</span>_678, <span style="color:#006666;color:#800000;">999</span>_999<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;number&gt; thousand, &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand, ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..............................................FFFF

1)
'A four, five or six-digit number that's not divisible by one thousand, in words should be '<number> thousand, <remainder>'' FAILED
expected "one thousand, two hundred and thirty-four", got "one thousand" (using ==)
./to_words_spec.rb:70:

&hellip;

4)
'A four, five or six-digit number that's not divisible by one thousand, in words should be '<number> thousand, <remainder>'' FAILED
expected "nine hundred and ninety-nine thousand, nine hundred and ninety-nine", got "nine hundred and ninety-nine thousand" (using ==)
./to_words_spec.rb:70:

Finished in 0.05905 seconds

50 examples, 4 failures
</pre>
<div class="igBar"><span id="lruby-71"><a href="#" onclick="javascript:showCodeTxt('ruby-71'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-71">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000..<span style="color:#006666;color:#800000;">999</span>_999</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1000</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : ', ' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..................................................

Finished in 0.056001 seconds

50 examples, 0 failures
</pre>
<p>Flying along! There's a special case we need to cover before we move onto the millions though &ndash; where there are no hundreds, so the comma becomes an 'and' (like 'two thousand and seven').</p>
<div class="igBar"><span id="lruby-72"><a href="#" onclick="javascript:showCodeTxt('ruby-72'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-72">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A four, five or six-digit number that's not divisible by one thousand but has no hundreds, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_023, <span style="color:#006666;color:#800000;">23</span>_001, <span style="color:#006666;color:#800000;">345</span>_099<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;number&gt; thousand and &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
..................................................FFF

1)
'A four, five or six-digit number that's not divisible by one thousand but has no hundreds, in words should be '<number> thousand and <remainder>'' FAILED
expected "one thousand and twenty-three", got "one thousand, twenty-three" (using ==)
./to_words_spec.rb:78:

2)
'A four, five or six-digit number that's not divisible by one thousand but has no hundreds, in words should be '<number> thousand and <remainder>'' FAILED
expected "twenty-three thousand and one", got "twenty-three thousand, one" (using ==)
./to_words_spec.rb:78:

3)
'A four, five or six-digit number that's not divisible by one thousand but has no hundreds, in words should be '<number> thousand and <remainder>'' FAILED
expected "three hundred and forty-five thousand and ninety-nine", got "three hundred and forty-five thousand, ninety-nine" (using ==)
./to_words_spec.rb:78:

Finished in 0.060685 seconds

53 examples, 3 failures
</pre>
<div class="igBar"><span id="lruby-73"><a href="#" onclick="javascript:showCodeTxt('ruby-73'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-73">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000..<span style="color:#006666;color:#800000;">999</span>_999</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand' +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> self%<span style="color:#006666;color:#800000;">1000</span> == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1000</span> &lt;<span style="color:#006666;color:#800000;">100</span> ? ' <span style="color:#9966CC; font-weight:bold;">and</span> ' : ', '<span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.....................................................

Finished in 0.057426 seconds

53 examples, 0 failures
</pre>
<p>The millions should be pretty much the same, so I'll do all the specs at once.</p>
<div class="igBar"><span id="lruby-74"><a href="#" onclick="javascript:showCodeTxt('ruby-74'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-74">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A seven, eight or nine-digit number that's divisible by one million, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_000_000, <span style="color:#006666;color:#800000;">34</span>_000_000, <span style="color:#006666;color:#800000;">567</span>_000_000, <span style="color:#006666;color:#800000;">999</span>_000_000<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;millions&gt; million'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' million'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A seven, eight or nine-digit number that's not divisible by one million, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_234_567, <span style="color:#006666;color:#800000;">34</span>_567_890, <span style="color:#006666;color:#800000;">567</span>_890_123, <span style="color:#006666;color:#800000;">999</span>_999_999<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;number&gt; million, &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' million, ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A seven, eight or nine-digit number that's not divisible by one million but has zeroes "</span> +</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#996600;">"from hundreds of thousands down to hundreds, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_000_023, <span style="color:#006666;color:#800000;">23</span>_000_001, <span style="color:#006666;color:#800000;">345</span>_000_099<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;number&gt; million and &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' million <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
.....................................................FFFFFFFFFFF

1)
'A seven, eight or nine-digit number that's divisible by one million, in words should be '<millions> million'' FAILED
expected "one thousand million", got nil (using ==)
./to_words_spec.rb:86:

&hellip;

11)
'A seven, eight or nine-digit number that's not divisible by one million but has zeroes from hundreds of thousands down to hundreds, in words should be '<number> million and <remainder>'' FAILED
expected "three hundred and forty-five million and ninety-nine", got nil (using ==)
./to_words_spec.rb:103:

Finished in 0.074188 seconds

64 examples, 11 failures
</pre>
<div class="igBar"><span id="lruby-75"><a href="#" onclick="javascript:showCodeTxt('ruby-75'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-75">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000_000..<span style="color:#006666;color:#800000;">999</span>_999_999</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand' +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> self%<span style="color:#006666;color:#800000;">1</span>_000_000 == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1</span>_000_000 &lt;<span style="color:#006666;color:#800000;">100</span> ? ' <span style="color:#9966CC; font-weight:bold;">and</span> ' : ', '<span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
................................................................

Finished in 0.066676 seconds

64 examples, 0 failures
</pre>
<p>Just the billions to go, and we're done! I might as well let it handle up to 999,999,999,999 &ndash; forcing it to stop at 10,000,000,000 would probably be harder.</p>
<div class="igBar"><span id="lruby-76"><a href="#" onclick="javascript:showCodeTxt('ruby-76'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-76">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># Examples of unhandled numbers</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>-<span style="color:#006666;color:#800000;">123</span>, -<span style="color:#006666;color:#800000;">1</span>, <span style="color:#006666;color:#800000;">0</span>, <span style="color:#006666;color:#800000;">1</span>_000_000_000_000<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"#{number}.to_words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '?'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == '?'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9900CC;">describe</span> <span style="color:#996600;">"A ten, eleven or twelve-digit number that's divisible by one billion, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_000_000_000, <span style="color:#006666;color:#800000;">34</span>_000_000_000, <span style="color:#006666;color:#800000;">567</span>_000_000_000, <span style="color:#006666;color:#800000;">999</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;billions&gt; billion'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' billion'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A ten, eleven or twelve-digit number that's not divisible by one billion, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_234_567_890, <span style="color:#006666;color:#800000;">34</span>_567_890_123, <span style="color:#006666;color:#800000;">567</span>_890_123_456, <span style="color:#006666;color:#800000;">999</span>_999_999_999<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;number&gt; billion, &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' billion, ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"A ten, eleven or twelve-digit number that's not divisible by one billion but has zeroes "</span> +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#996600;">"from hundreds of millions down to hundreds, in words"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>_000_000_023, <span style="color:#006666;color:#800000;">23</span>_000_000_001, <span style="color:#006666;color:#800000;">345</span>_000_000_099<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |number|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; it <span style="color:#996600;">"should be '&lt;number&gt; billion and &lt;remainder&gt;'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; number.<span style="color:#9900CC;">to_words</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>number/<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' billion <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>number%<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
................................................................FFFFFFFFFFF

1)
'A ten, eleven or twelve-digit number that's divisible by one billion, in words should be '<billions> billion'' FAILED
expected "one billion", got nil (using ==)
./to_words_spec.rb:111:

&ndash;

11)
'A ten, eleven or twelve-digit number that's not divisible by one billion but has zeroes from hundreds of millions down to hundreds, in words should be '<number> billion and <remainder>'' FAILED
expected "three hundred and forty-five billion and ninety-nine", got "?" (using ==)
./to_words_spec.rb:128:

Finished in 0.105984 seconds

75 examples, 11 failures
</pre>
<p>The complete working code:</p>
<div class="igBar"><span id="lruby-77"><a href="#" onclick="javascript:showCodeTxt('ruby-77'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-77">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">Integer</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">999</span>_999_999_999<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; decades = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'seventy', 'eighty', 'ninety'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">19</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">10</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : <span style="color:#006600; font-weight:bold;">&#40;</span>'-' + numbers<span style="color:#006600; font-weight:bold;">&#91;</span>self%<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">100</span>..<span style="color:#006666;color:#800000;">999</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#93;</span> + ' hundred' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : ' <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000..<span style="color:#006666;color:#800000;">999</span>_999</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' thousand' +</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> self%<span style="color:#006666;color:#800000;">1000</span> == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1000</span> &lt;<span style="color:#006666;color:#800000;">100</span> ? ' <span style="color:#9966CC; font-weight:bold;">and</span> ' : ', '<span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000_000..<span style="color:#006666;color:#800000;">999</span>_999_999</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' million' +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> self%<span style="color:#006666;color:#800000;">1</span>_000_000 == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1</span>_000_000 &lt;<span style="color:#006666;color:#800000;">100</span> ? ' <span style="color:#9966CC; font-weight:bold;">and</span> ' : ', '<span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000_000_000..<span style="color:#006666;color:#800000;">999</span>_999_999_999</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' billion' +</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> self%<span style="color:#006666;color:#800000;">1</span>_000_000_000 == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1</span>_000_000_000 &lt;<span style="color:#006666;color:#800000;">100</span> ? ' <span style="color:#9966CC; font-weight:bold;">and</span> ' : ', '<span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">1</span>_000_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And just to prove it:</p>
<pre>
...........................................................................

Finished in 0.099379 seconds

75 examples, 0 failures
</pre>
<p>I think it's interesting how the recursive calls back into to_words just kind of fell into place as I went along, without particularly thinking about it. I think if I'd tried to design that up-front it would have involved a lot of head-scratching, and it would have been harder to pick suitable test cases to prove it worked.</p>
<p>Just a little bit of cleaning up before I call it a day &ndash; those last three clauses in the case statement look a bit repetitive, so let's see if we can do a bit of refactoring.</p>
<div class="igBar"><span id="lruby-78"><a href="#" onclick="javascript:showCodeTxt('ruby-78'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-78">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">Integer</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> to_words</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> '?' <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">999</span>_999_999_999<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, 'one', 'two', 'three', 'four', 'five', 'six', 'seven',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; decades = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; 'seventy', 'eighty', 'ninety'<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0000FF; font-weight:bold;">self</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>..<span style="color:#006666;color:#800000;">19</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">20</span>..<span style="color:#006666;color:#800000;">99</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; decades<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">10</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : <span style="color:#006600; font-weight:bold;">&#40;</span>'-' + numbers<span style="color:#006600; font-weight:bold;">&#91;</span>self%<span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">100</span>..<span style="color:#006666;color:#800000;">999</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>/<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#93;</span> + ' hundred' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span> == <span style="color:#006666;color:#800000;">0</span> ? '' : ' <span style="color:#9966CC; font-weight:bold;">and</span> ' + <span style="color:#006600; font-weight:bold;">&#40;</span>self%<span style="color:#006666;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000..<span style="color:#006666;color:#800000;">999</span>_999</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; words_for_big_numbers <span style="color:#006666;color:#800000;">1</span>_000, 'thousand'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000_000..<span style="color:#006666;color:#800000;">999</span>_999_999</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; words_for_big_numbers <span style="color:#006666;color:#800000;">1</span>_000_000, 'million'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;color:#800000;">1</span>_000_000_000..<span style="color:#006666;color:#800000;">999</span>_999_999_999</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; words_for_big_numbers <span style="color:#006666;color:#800000;">1</span>_000_000_000, 'billion'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; private</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> words_for_big_numbers multiplier_value, multiplier_name</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>/multiplier_value<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span> + ' ' + multiplier_name +</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> self%multiplier_value == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ''</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>self%multiplier_value &lt;<span style="color:#006666;color:#800000;">100</span> ? ' <span style="color:#9966CC; font-weight:bold;">and</span> ' : ', '<span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>self%multiplier_value<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Run the specs one last time, to make sure the refactoring didn't break anything:</p>
<pre>
...........................................................................

Finished in 0.099923 seconds

75 examples, 0 failures
</pre>
<p>Finished!</p>
<p>On the off-chance that anyone actually read this far, I hope it was at least mildly interesting. I didn't expect to end up with <em>quite</em> such a long post when I started!</p>
<h4>Postscript: the 'extra credit' question</h4>
<p>This isn't part of the main purpose of the post, because I'm not going to test-drive it, but I thought I might as well include the final part of the problem too:</p>
<blockquote><p>
For an extra challenge, when the strings for the numbers for 1 - 10,000,000,000 are sorted alphabetically, which is the<br />
first odd number in the list?
</p></blockquote>
<p>I'm not sure whether this will complete in a reasonable time, but I'll try a brute force approach &ndash; just loop through all the odd numbers and keep track of the one that's earliest, alphabetically speaking. I'm going to start with a much smaller range (up to a million), and put a progress indicator of sorts in so I can see whether it's getting anywhere at all.</p>
<div class="igBar"><span id="lruby-79"><a href="#" onclick="javascript:showCodeTxt('ruby-79'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-79">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> 'to_words'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">winner = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span>...<span style="color:#006666;color:#800000;">500</span>_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|n| n * <span style="color:#006666;color:#800000;">2</span> - <span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span>'zzz'<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |earliest, current|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">"#{current/10_000}% complete"</span> <span style="color:#9966CC; font-weight:bold;">if</span> current%<span style="color:#006666;color:#800000;">10</span>_000 == <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; word = current.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; word &lt;earliest ? word : earliest</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">puts</span> winner </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
Kerrys-G5:~/Dev/RFG kerry$ irb

irb(main):001:0> require 'benchmark'
=> true
irb(main):002:0> puts Benchmark.measure { load 'extra_credit.rb' }
0% complete
1% complete
...
99% complete
eight hundred and eight thousand and eighty-five
115.470000   1.060000 116.530000 (119.292097)
</pre>
<p>So, two minutes to run the first million &ndash; I make that about a fortnight to do ten billion. Time to be a bit more creative.</p>
<p>Thinking about it, eight is obviously the earliest number in the alphabet, and five the earliest <em>odd</em> number, so whatever the answer is, it's going to end in a five, with all the other digits being either zero or eight.</p>
<div class="igBar"><span id="lruby-80"><a href="#" onclick="javascript:showCodeTxt('ruby-80'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-80">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> 'to_words'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># find all the nine-digit numbers made up of only zeros and eights</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">possibilities = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">0</span>, <span style="color:#006666;color:#800000;">8</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006666;color:#800000;">8</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; new_possibilities = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; possibilities.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |possibility|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; new_possibilities &lt;&lt;possibility * <span style="color:#006666;color:#800000;">10</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; new_possibilities &lt;&lt;possibility * <span style="color:#006666;color:#800000;">10</span> + <span style="color:#006666;color:#800000;">8</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; possibilities += new_possibilities</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># add a five to all of them</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">possibilities.<span style="color:#9900CC;">collect</span>! <span style="color:#006600; font-weight:bold;">&#123;</span>|p| <span style="color:#CC0066; font-weight:bold;">p</span> * <span style="color:#006666;color:#800000;">10</span> + <span style="color:#006666;color:#800000;">5</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">winner = possibilities.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span>'zzz'<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |earliest, current|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; word = current.<span style="color:#9900CC;">to_words</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">"Unexpected number: #{current}"</span> <span style="color:#9966CC; font-weight:bold;">if</span> word.<span style="color:#9900CC;">eql</span>? '?'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; word &lt;earliest ? word : earliest</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">puts</span> winner </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<pre>
irb(main):003:0> puts Benchmark.measure { load 'extra_credit.rb' }
eight billion and eighty-five
  3.110000   0.030000   3.140000 (  3.149860)
</pre>
<p>So, if my logic makes sense, the answer is 8,000,000,085. And three seconds is a bit kinder to my CPU than a fortnight!</p>
<h4>Final files</h4>
<ul>
<li><a href="/wp-content/uploads/2007/06/to_words.rb">to_words.rb</li>
<li><a href="/wp-content/uploads/2007/06/to_words_spec.rb">to_words_spec.rb</li>
<li><a href="/wp-content/uploads/2007/06/extra_credit.rb">extra_credit.rb</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/06/17/a-step-by-step-bdd-example-using-rspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Software/geek symbiosis</title>
		<link>http://www.kerrybuckley.org/2007/06/09/softwaregeek-symbiosis/</link>
		<comments>http://www.kerrybuckley.org/2007/06/09/softwaregeek-symbiosis/#comments</comments>
		<pubDate>Sat, 09 Jun 2007 15:54:21 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[BT]]></category>
		<category><![CDATA[Enterprise]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/06/09/softwaregeek-symbiosis/</guid>
		<description><![CDATA[Yesterday I was lucky enough to get the chance to attend a talk/workshop on TiddlyWiki, presented by its creator Jeremy Ruston, with quality supporting acts from JP and Doc Searls. Jeremy has just joined BT as Head of Open Source Innovation, which is pretty cool in itself. One of the comments he made that particularly [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I was lucky enough to get the chance to attend a talk/workshop on <a href="http://tiddlywiki.com/">TiddlyWiki</a>, presented by its creator <a href="http://jermolene.wordpress.com/about/">Jeremy Ruston</a>, with quality supporting acts from <a href="http://www.confusedofcalcutta.com/">JP</a> and <a href="http://www.searls.com/dochome.html">Doc Searls</a>. Jeremy has just joined BT as Head of Open Source Innovation, which is pretty cool in itself.</p>
<p>One of the comments he made that particularly struck me was to the effect that a piece of software is a living thing, but is parasitic, requiring a 'geek host' to live on. Much of the value of software isn't what it <em>does</em>, but what it can <em>potentially</em> do. Once you separate the software from its geek, it starts to die, because it can no longer adapt to changes in its environment.</p>
<p>I think this is something that we're losing in the world of corporate IT.<span id="more-125"></span> In the old world the systems the business used tended to be huge, developed in-house, and come with a whole team of geeks attached. Unfortunately in those days we also had heavyweight waterfall processes which made it hard to react to new opportunities for the software to create business value.</p>
<p>These days the big systems tend to be assembled around so-called COTS (commercial, off-the-shelf) products from external vendors. In the real world, of course, COTS products rarely work 'off-the-shelf', so there's usually still plenty of code to be written. Since this is not considered 'core business', it gets farmed out to whichever outsourcing company puts in the cheapest bid.</p>
<p>The systems still have large teams attached, but somehow we lost all the geeks. The software's new hosts are programme managers, project managers, delivery managers, architects, solution designers and who-knows-what else. Actually interacting with the code is considered almost menial &ndash; something that can be done by anyone with the appropriate technical skills, regardless of whether they have any knowledge of the specific application or its business context.</p>
<p>It's much harder to unlock the potential in a piece of software when the people the business talks to aren't the ones who understand how the code works. It's even harder when <em>no-one</em> understands how the code works.</p>
<p>On a completely different note, it's embarrassing how low the turnout seems to be for events like yesterday's, and the talk by Martin Fowler and Dan North last year. Maybe people are just busy or don't hear about them, but I can't help thinking it's partly down to the fact that we just don't have many geeks left. On the plus side, at least it meant that those of us who <em>were</em> there got a decent chance to chat to Jeremy and Doc afterwards.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/06/09/softwaregeek-symbiosis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Blast from the past</title>
		<link>http://www.kerrybuckley.org/2007/06/09/blast-from-the-past/</link>
		<comments>http://www.kerrybuckley.org/2007/06/09/blast-from-the-past/#comments</comments>
		<pubDate>Sat, 09 Jun 2007 14:07:51 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[BT]]></category>
		<category><![CDATA[General nonsense]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/06/09/blast-from-the-past/</guid>
		<description><![CDATA[The things you find in server room cupboards!]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/kerrybuckley/sets/72157600330658548/">The things you find in server room cupboards!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/06/09/blast-from-the-past/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BT Agile Awards final</title>
		<link>http://www.kerrybuckley.org/2007/05/18/bt-agile-awards-final/</link>
		<comments>http://www.kerrybuckley.org/2007/05/18/bt-agile-awards-final/#comments</comments>
		<pubDate>Fri, 18 May 2007 10:16:04 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[BT]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/05/18/bt-agile-awards-final/</guid>
		<description><![CDATA[Wednesday night saw the end-of-year BT Agile Awards dinner, with all the recipients of the various quarterly awards gathering at the Royal Horseguards Hotel to eat, drink, and find out who'd won the overall team and individual awards for the year. It was a good night, made even better by the fact that we won [...]]]></description>
			<content:encoded><![CDATA[<p><a href='/2007/05/18/bt-agile-awards-final/the-csam-n-team-receiving-our-trophy/' rel='attachment wp-att-116' title='The CSAM-n team receiving our trophy'><img style="float:left;margin-right:10px;" src='/wp-content/uploads/2007/05/agile_award.thumbnail.jpg' alt='The CSAM-n team receiving our trophy' /></a></p>
<p>Wednesday night saw the end-of-year BT Agile Awards dinner, with all the recipients of the various quarterly awards gathering at the Royal Horseguards Hotel to eat, drink, and find out who'd won the overall team and individual awards for the year. It was a good night, made even better by the fact that we won the team award! Our prize is that we're off to the <a href="http://agile2007.org/">Agile 2007</a> conference in Washington DC in August.</p>
<p>Congratulations also to Gregg Wyburn for winning the individual award, and to our current colleagues from the .NET SDK team, who <a href="http://blog.iclutton.com/2007/05/my-agile-awards-moment.html">picked up their quarter three award</a> for best application of the BT agile values.</p>
<p><a href="http://flickr.com/photos/kerrybuckley/sets/72157600224161726/">More photos from the night on Flickr</a> (see also <a href="http://flickr.com/photos/tags/btagileawards/">photos tagged with btagileawards</a> &ndash; hopefully some more people will post and tag photos, but BT's webfilter blocking Flickr doesn't help).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/05/18/bt-agile-awards-final/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JohnMac and psd: the real story!</title>
		<link>http://www.kerrybuckley.org/2007/05/15/johnmac-and-psd-the-real-story/</link>
		<comments>http://www.kerrybuckley.org/2007/05/15/johnmac-and-psd-the-real-story/#comments</comments>
		<pubDate>Tue, 15 May 2007 11:59:07 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[General nonsense]]></category>
		<category><![CDATA[Web21C]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/05/15/johnmac-and-psd-the-real-story/</guid>
		<description><![CDATA[According to Twitter, anyway: :-)]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/JohnMac/friends">According to Twitter</a>, anyway:</p>
<p><img src='/wp-content/uploads/2007/05/johns_friend.JPG' alt='John’s one friend' /></p>
<p>:-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/05/15/johnmac-and-psd-the-real-story/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BT’s Java SDK part two: making phone calls</title>
		<link>http://www.kerrybuckley.org/2007/02/18/bt%e2%80%99s-java-sdk-part-two-making-phone-calls/</link>
		<comments>http://www.kerrybuckley.org/2007/02/18/bt%e2%80%99s-java-sdk-part-two-making-phone-calls/#comments</comments>
		<pubDate>Sun, 18 Feb 2007 12:02:30 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[BT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Web21C]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/02/18/bt%e2%80%99s-java-sdk-part-two-making-phone-calls/</guid>
		<description><![CDATA[Yesterday we saw how easy it is to send text messages using the SDK, so now let's try making a phone call. Imagine you're implementing a customer support system where people can submit a problem report along with their phone number, and you want to have the appropriate person ring them to help resolve their [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday we saw how easy it is to <a href="/2007/02/17/bts-java-sdk/">send text messages using the SDK</a>, so now let's try making a phone call.</p>
<p><span id="more-90"></span></p>
<p>Imagine you're implementing a customer support system where people can submit a problem report along with their phone number, and you want to have the appropriate person ring them to help resolve their fault. Your system does whatever magic is required to find who that appropriate person is, then uses the Web21C SDK to set up the call:</p>
<div class="igBar"><span id="ljava-82"><a href="#" onclick="javascript:showCodeTxt('java-82'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-82">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package com.<span style="color: #006600;">kerrybuckley</span>.<span style="color: #006600;">sdk</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import com.bt.sdk.thirdpartycall.ThirdPartyCall;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThirdPartyCallDemo <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> ThirdPartyCallDemo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">makeCall</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"tel:+447700900123"</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">"tel:+441214960391"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> makeCall<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> engineerPhoneNo, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> customerPhoneNo<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ThirdPartyCall tpc = <span style="color: #000000; font-weight: bold;">new</span> ThirdPartyCall<span style="color: #66cc66;">&#40;</span>customerPhoneNo,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engineerPhoneNo<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; tpc.<span style="color: #006600;">startCall</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The <code>ThirdPartyCall</code> constructor on line 12 takes two arguments, <em>callee</em> and <em>caller</em>. When the call is connected using <code>startCall()</code>, the caller's phone (the engineer, in our example) rings first. Once they pick up, the callee's phone starts ringing, and the call between the two is connected as though it had been made directly (although neither party sees the other's caller ID).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/02/18/bt%e2%80%99s-java-sdk-part-two-making-phone-calls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.966 seconds -->

