<?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>SciWerks.com &#187; Ruby</title>
	<atom:link href="http://www.sciwerks.com/blog/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sciwerks.com/blog</link>
	<description>Software for scientists, by scientists</description>
	<lastBuildDate>Fri, 24 Oct 2008 01:02:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using MacFUSE with Rubyforge</title>
		<link>http://www.sciwerks.com/blog/2007/01/25/using-macfuse-with-rubyforge/</link>
		<comments>http://www.sciwerks.com/blog/2007/01/25/using-macfuse-with-rubyforge/#comments</comments>
		<pubDate>Thu, 25 Jan 2007 05:18:43 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2007/01/25/using-macfuse-with-rubyforge/</guid>
		<description><![CDATA[I&#8217;ve always found it a PITA to administer html files for rubyforge projects, mostly because it requires a lot of command line work to move things around.  
Today I started using a new trick.  Google has recently released MacFUSE.  The neat thing here is that you can also download the sshfs file [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always found it a PITA to administer html files for rubyforge projects, mostly because it requires a lot of command line work to move things around.  </p>
<p>Today I started using a new trick.  Google has recently released <a href="http://googlemac.blogspot.com/2007/01/taming-mac-os-x-file-systems.html">MacFUSE</a>.  The neat thing here is that you can also <a href="http://code.google.com/p/macfuse/">download</a> the sshfs file system and use it to mount an SSH server as a network drive.</p>
<p>Needless to say, this works like a charm with Rubyforge.  Just fire up the sshfs.app file after installing the MacFUSE core and the sshfs.app bundle.</p>
<p>It will ask you for a server, type &#8216;rubyforge.org&#8217;.  Enter your rubyforge id as well, and for the directory, enter something like </p>
<div class="codesnip-container" >/var/www/gforge-projects/project name/</div>
<p>It will ask you for a password, and then like magic, your rubyforge filespace will be mounted like a drive.  Now you have no excuse for not putting some documentation or a spiffy web page up there.</p>
<p>Incidentally, once the connection is established, you can quit the &#8217;sshfs.app&#8217; program.  There are also instructions on the Google code web page that tell you how to use this from the command line.  The mounted rubyforge.org drive will also be accessible from the command line as &#8216;/Volumes/rubyforge.org </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1072568092760209";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2007/01/25/using-macfuse-with-rubyforge/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A quick tip for searching for rails related information</title>
		<link>http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/</link>
		<comments>http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/#comments</comments>
		<pubDate>Fri, 05 Jan 2007 23:36:24 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/</guid>
		<description><![CDATA[If you use Firefox, you can use this little trick.

go to Rails Google co-op
right click on the search box.
select &#8216;Add keyword for this search&#8217;
fill out the resulting form and add a keyword like &#8216;rails&#8217;
move focus to the address bar (CMD-L on a Mac)
Type &#8216;rails sciwerks&#8217; on the address bar

You can also do this with rails [...]]]></description>
			<content:encoded><![CDATA[<p>If you use Firefox, you can use this little trick.</p>
<ol>
<li>go to <a href="http://www.google.com/coop/cse?cx=017837420008955394371%3Aenopjbgisno" target=_blank>Rails Google co-op</a></li>
<li>right click on the search box.</li>
<li>select &#8216;Add keyword for this search&#8217;</li>
<li>fill out the resulting form and add a keyword like &#8216;rails&#8217;</li>
<li>move focus to the address bar (CMD-L on a Mac)</li>
<li>Type &#8216;rails sciwerks&#8217; on the address bar</li>
</ol>
<p>You can also do this with rails or ruby API methods.</p>
<p>You could also set up a quicksilver web search for this too.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-1072568092760209";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ruby-units 0.3.3</title>
		<link>http://www.sciwerks.com/blog/2006/10/06/ruby-units-033/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/06/ruby-units-033/#comments</comments>
		<pubDate>Fri, 06 Oct 2006 17:50:16 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[ruby-units]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/06/ruby-units-033/</guid>
		<description><![CDATA[The latest version of the ruby-units gem has just been released.
Installation
gem install ruby-units
or
gem update ruby-units
Features

Significantly improved speed
Will use the &#8216;Chronic&#8217; gem to interpret times/dates if loaded
some nice time helpers.
&#8216;1 day&#8217;.from &#8216;now&#8217;       #=>  Wed Oct 04 18:27:13 EDT 2006
   &#8216;1 day&#8217;.ago       [...]]]></description>
			<content:encoded><![CDATA[<p>The latest version of the ruby-units gem has just been released.</p>
<h2>Installation</h2>
<div class="codesnip-container" >gem install ruby-units<br />
or<br />
gem update ruby-units</div>
<h2>Features</h2>
<ol>
<li>Significantly improved speed</li>
<li>Will use the &#8216;Chronic&#8217; gem to interpret times/dates if loaded</li>
<li>some nice time helpers.
<div class="codesnip-container" >&#8216;1 day&#8217;.from &#8216;now&#8217;       #=>  Wed Oct 04 18:27:13 EDT 2006<br />
   &#8216;1 day&#8217;.ago               #=>  Mon Oct 02 18:38:56 EDT 2006<br />
   &#8216;7 days&#8217;.before &#8216;1/1/2007&#8242; #=> Mon Dec 25 00:00:00 EST 2006<br />
   &#8216;7 days&#8217;.after &#8216;12/25/2006&#8242; #=> Mon Jan 01 00:00:00 EST 2007<br />
   &#8216;days&#8217;.since &#8216;1/1/2006&#8242;   #=>  275.436 d<br />
   &#8216;days&#8217;.until &#8216;1/1/2007&#8242;   #=>  89.7683 d</div>
<p>    (works for sec, min, hours, days, weeks, fortnights, years, decades, centuries&#8230;.<br />
    but not months).</p>
<p>   and some others..<br />
   these helpers will automagically give you a DateTime object<br />
   if a Time object won&#8217;t work.  </p>
<div class="codesnip-container" >&#8216;years&#8217;.since &#8216;1/1/1900&#8242; #=> 106.756 y<br />
   &#8216;50 years&#8217;.from &#8216;now&#8217;    #=> 2056-10-02T18:46:55Z</div>
</li>
<li>units can now identify themselves (sort of..)
<div class="codesnip-container" >&#8216;60 mph&#8217;.unit.kind  #=> :speed<br />
   &#8216;9.8 m/s^2&#8242;.unit.kind #=> :acceleration</div>
</li>
<li>some new constructors
<div class="codesnip-container" >Unit(10,&#8217;m')  #=> 10 m</div>
</li>
<li>Now throws an exception if it donsn&#8217;t understand a unit (during creation)</li>
<li>Temperature handling improved
<div class="codesnip-container" >&#8220;0 tempC&#8221;.unit #=> 273.15 degK<br />
   &#8220;310.15 degK&#8221;.to(&#8217;tempC&#8217;) #=> 37 degC</div>
</li>
<li>Trig functions can accept angular units
<div class="codesnip-container" >Math.sin(&#8217;90 deg&#8217;.unit) #=> 1.0<br />
   Math.sin(&#8217;100 grad&#8217;.unit) #=> 1.0</div>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/06/ruby-units-033/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ruby-units</title>
		<link>http://www.sciwerks.com/blog/2006/09/19/ruby-units/</link>
		<comments>http://www.sciwerks.com/blog/2006/09/19/ruby-units/#comments</comments>
		<pubDate>Tue, 19 Sep 2006 19:23:46 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[ruby-units]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/09/19/ruby-units/</guid>
		<description><![CDATA[In scientific (and non-scientific) programming one frequently needs to be able to manipulate units such as &#8216;10 mm&#8217;, &#8216;6 gal&#8217;, or &#8216;3 weeks&#8217;.      The traditional way to handle units in programming is to simply save the scalar part of the unit (i.e., the 10 from &#8216;10 mm&#8217;), and then make [...]]]></description>
			<content:encoded><![CDATA[<p>In scientific (and non-scientific) programming one frequently needs to be able to manipulate units such as &#8216;10 mm&#8217;, &#8216;6 gal&#8217;, or &#8216;3 weeks&#8217;.      The traditional way to handle units in programming is to simply save the scalar part of the unit (i.e., the 10 from &#8216;10 mm&#8217;), and then make sure you programmatically convert the unit whenever necessary.  Needless to say, this is error prone and it constrains the user to only being able to enter known unit types.</p>
<p>Ruby has a few libraries (notably, <a href="http://facets.rubyforge.org">Facets</a>) available that handle unit conversions in one form or another.  However, most of these systems don&#8217;t quite meet my somewhat demanding and arbitrary requirements, so I set out to write a new one.  </p>
<h2>Installation</h2>
<p>You can get &#8216;ruby-units&#8217; using</p>
<div class="codesnip-container" >gem install ruby-units</div>
<h2>Usage</h2>
<p>To use this gem, you need to do the standard</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">require</span> &#8216;rubygems&#8217;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">require</span> &#8216;ruby-units&#8217; </div>
</li>
</ol>
</div>
</div>
<h3>Creating a Unit object</h3>
<p>Because I use a lot of units in my work, and I&#8217;m lazy, there are several different ways to create a new &#8216;Unit&#8217; object.</p>
<ol>
<li>unit = Unit.new(&#8221;1 mm&#8221;)</li>
<li>unit = Unit(&#8221;1 mm&#8221;)</li>
<li>unit = U(&#8221;1 mm&#8221;)</li>
<li>unit = &#8220;1 mm&#8221;.to_unit</li>
<li>unit = &#8220;1 mm&#8221;.unit </li>
<li>unit = &#8220;1 mm&#8221;.u</li>
<li>unit = &#8220;mm&#8221;.unit (yields &#8216;1 mm&#8217;)</li>
</ol>
<p>Of these, I use #5 the most since it is reasonably terse without becoming obscure.<br />
You can also create Unit objects from Numerics, Arrays, and Time objects.  Numerics just become unitless Unit objects, Arrays are interpreted as [scalar, numerator, denominator], and Time objects are converted into a number of seconds.</p>
<p>Unit objects can be serialized through YAML using &#8216;unit.to_yaml&#8217;, which facilitates them being stored in database columns using Rails&#8217; :serialize functionality.</p>
<p>Unit strings generally consist of text in the following format:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">R = <span class="st0">&quot;8.31451 kJ/mol*degK&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">a = <span class="st0">&quot;9.8 m/s^2&quot;</span>.<span class="me1">unit</span> </div>
</li>
</ol>
</div>
</div>
<p>Since Units are constructed from strings, it is easy to have a user specify them in input fields.  Because the format is fairly standard, there should not be too many problems with misinterpreted units.  </p>
<p>A couple of points:</p>
<ol>
<li> only one &#8216;/&#8217; per unit.  No &#8216;m/s/s&#8217;
<li> everything after the &#8216;/&#8217; is in the denominator
<li> you can use negative exponents (&#8217;9.8 m*s^-2&#8242;)
<li> use either a space or a &#8216;*&#8217; to separate units (&#8217;9.8 ms^-2&#8242; == &#8216;9.8 1/ms^2&#8242;)
<li> ruby-units understands all SI prefixes, and a few others as well
</ol>
<h3>Unit Compatibility</h3>
<p>Sometimes it is helpful to know if two units are &#8216;compatible&#8217;.  Compatible units can be converted into each other, so &#8216;feet&#8217; and &#8216;meters&#8217; are compatbile, but &#8216;feet&#8217; and &#8216;liters&#8217; are not.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">unit1= &#8216;<span class="nu0">1</span> feet&#8217;.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">unit2 = &#8216;<span class="nu0">1</span> meter&#8217;.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">unit3 = &#8216;<span class="nu0">1</span> liter&#8217;.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">unit1 =~ unit2&nbsp; <span class="co1">#=&gt;true</span></div>
</li>
<li class="li1">
<div class="de1">unit1 =~ unit3&nbsp; <span class="co1">#=&gt;false</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">unit1.<span class="me1">compatible</span>? unit2&nbsp; <span class="co1">#=&gt; true</span></div>
</li>
<li class="li1">
<div class="de1">unit1.<span class="me1">compatible_with</span>? unit3&nbsp; <span class="co1">#=&gt;false </span></div>
</li>
</ol>
</div>
</div>
<h3>Unit Conversion</h3>
<p>Units can be converted to each other so long as they are &#8216;compatible&#8217;.  Ruby-units will throw an exception if units need to be compatible, but aren&#8217;t, so be sure to trap those exceptions when malformed input is possible.</p>
<h4>Explicit Conversion</h4>
<p>Explicit unit conversion occurs when the user asks for it.  There are a couple of ways to force conversion to a particular unit.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">unit = <span class="st0">&quot;1 mm&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">unit &gt;&gt; <span class="st0">&quot;ft&quot;</span></div>
</li>
<li class="li1">
<div class="de1">unit.<span class="me1">to</span><span class="br0">&#40;</span><span class="st0">&quot;inches&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">unit2.<span class="me1">to</span><span class="br0">&#40;</span>unit1<span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp;<span class="co1"># converts unit2 to same units as unit1 </span></div>
</li>
</ol>
</div>
</div>
<h4>Implicit Conversion</h4>
<p>There are times when unit conversions are implied or assumed for simplicity.  For example, when adding two units, the gem will convert the second unit to the same base units as the first prior to doing the math.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">unit1 = <span class="st0">&quot;1 meter&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">unit2 = <span class="st0">&quot;50 cm&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">result = unit1 + unit2 <span class="co1">#=&gt; 1.5 m </span></div>
</li>
</ol>
</div>
</div>
<h3>Unit Math</h3>
<p>Unit objects come in really handy when doing complex math calculations:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">pr = <span class="st0">&quot;1 atm&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">n = <span class="st0">&quot;1 mol&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">R = <span class="st0">&quot;8.3144 J/mol*degK&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">T = <span class="st0">&quot;0 tempC&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">v = <span class="br0">&#40;</span>n*R*T<span class="br0">&#41;</span>/pr&nbsp; &nbsp;<span class="co1"># ideal gas law</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">p</span> v.<span class="me1">to_s</span><span class="br0">&#40;</span><span class="st0">&quot;l&quot;</span><span class="br0">&#41;</span>&nbsp; <span class="co1">#=&gt; 22.4 l </span></div>
</li>
</ol>
</div>
</div>
<p>You can add, multiply, subtract, and divide units as you can with any Numeric class.   You can also exponentiate or root them, although I restrict roots to integer values, since &#8216;m^0.5&#8242; doesn&#8217;t really make any sense.</p>
<p>As an added bonus, all Trig functions (sin, cos, tan, sinh, &#8230;) can accept units that are compatible with radians.  The values will be converted to radians before returning the result.  So you can do this..</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">Math.<span class="me1">sin</span><span class="br0">&#40;</span><span class="st0">&quot;90 deg&quot;</span>.<span class="me1">unit</span><span class="br0">&#41;</span> <span class="co1">#=&gt; 1.0 </span></div>
</li>
</ol>
</div>
</div>
<h3> Temperatures </h3>
<p>Note that we specified the temperature in this equation as a &#8216;tempC&#8217;.  Specifying a temperature will convert that unit into the equivalent number of degrees kelvin.  So in this case, &#8220;0 tempC&#8221;.unit => 273.15 degK.</p>
<p>This is necessary to make the calculations work out right.  Most times unit conversions are done by just adjusting the size of the units, but temperatures require you to also specify the zero point on the scale.</p>
<p>You can convert &#8216;degree&#8217; units (like &#8216;degC&#8217;, &#8216;degF&#8217;, &#8216;degR&#8217;, or &#8216;degK&#8217;) to a temperature this way&#8230;</p>
<div class="codesnip-container" >temp = &#8216;0 degC&#8217;.unit.to(&#8217;tempK&#8217;)</div>
<p>This really only makes sense when the value represents the degrees between absolute zero and the point of interest.  Only the user knows when that is the case, so use it carefully.</p>
<h3> Time and Date Math </h3>
<p>Ruby-units understands a wide range of time units, and can convert back and forth between them with ease.  Nothing new there.</p>
<p>However, ruby-units can add and subtract time units from Date, DateTime, and Time objects.  Throw in a few rails-esque helpers and you can write code like this&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">one_hour_from_now = <span class="st0">&quot;1 h&quot;</span>.<span class="me1">from_now</span></div>
</li>
<li class="li1">
<div class="de1">in_five_minutes = Time.<span class="kw1">in</span><span class="br0">&#40;</span><span class="st0">&quot;5 min&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">an_hour_ago = <span class="st0">&quot;1 h&quot;</span>.<span class="me1">ago</span></div>
</li>
<li class="li1">
<div class="de1">next_week = Time.<span class="me1">now</span> + <span class="st0">&quot;1 week&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">waiting_for = <span class="st0">&quot;min&quot;</span>.<span class="kw1">until</span><span class="br0">&#40;</span>one_hour_from_now<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">days_to_christmas = <span class="st0">&quot;days&quot;</span>.<span class="kw1">until</span><span class="br0">&#40;</span><span class="st0">&quot;12/25/06&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">age = <span class="st0">&quot;years&quot;</span>.<span class="me1">since</span><span class="br0">&#40;</span><span class="st0">&quot;4/24/69&quot;</span><span class="br0">&#41;</span> </div>
</li>
</ol>
</div>
</div>
<h3>Formatting Output</h3>
<p>You can format the output of a Unit object by using the &#8220;.to_s&#8221; function and string format specifiers&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">length = <span class="st0">&quot;1 mm&quot;</span>.<span class="me1">unit</span></div>
</li>
<li class="li1">
<div class="de1">length.<span class="me1">to_s</span><span class="br0">&#40;</span><span class="st0">&quot;%0.2f&quot;</span><span class="br0">&#41;</span>&nbsp; <span class="co1">#=&gt; &quot;1.00 mm&quot;</span></div>
</li>
<li class="li1">
<div class="de1">height = <span class="st0">&quot;6 ft 4 in&quot;</span>.<span class="me1">unit</span>.<span class="me1">to_s</span><span class="br0">&#40;</span>:ft<span class="br0">&#41;</span>&nbsp; <span class="co1">#=&gt; 6&#8242;4&quot;</span></div>
</li>
<li class="li1">
<div class="de1">weight = <span class="st0">&quot;8.25 lbs&quot;</span>.<span class="me1">unit</span>.<span class="me1">to_s</span><span class="br0">&#40;</span>:lbs<span class="br0">&#41;</span> <span class="co1">#=&gt; 8 lbs, 4 oz</span></div>
</li>
<li class="li1">
<div class="de1">time = <span class="st0">&quot;1.5 h&quot;</span>.<span class="me1">unit</span>.<span class="me1">to_s</span><span class="br0">&#40;</span><span class="st0">&quot;%H:%M&quot;</span><span class="br0">&#41;</span> <span class="co1">#=&gt; 1:30 </span></div>
</li>
</ol>
</div>
</div>
<h3>Miscellaneous Stuff</h3>
<p>You can create ranges of units, so long as they have scalar integers.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span> U<span class="br0">&#40;</span><span class="st0">&quot;1 mm&quot;</span><span class="br0">&#41;</span> .. <span class="me1">U</span><span class="br0">&#40;</span><span class="st0">&quot;5 mm&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>.<span class="me1">map</span>&nbsp; <span class="co1">#=&gt;[ 1 mm, 2 mm, 3 mm, 4 mm, 5mm] </span></div>
</li>
</ol>
</div>
</div>
<p>If a unit is &#8216;unitless?&#8217; then it can be converted back to other types of Numerics.</p>
<p>Note that &#8216;Unit&#8217; objects are pretty slow compared to more common ones like Integers and Floats, so if performance is a big issue, you might want to consider using these sparingly.</p>
<h2> Defining new units </h2>
<p>You can define custom units by adding a code block like this..</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Unit &lt; Numeric</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @@USER_DEFINITIONS = </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span>&#8216;&lt;inchworm&gt;&#8217; =&gt;&nbsp; <span class="br0">&#91;</span>%w<span class="br0">&#123;</span>inworm inchworm<span class="br0">&#125;</span>, <span class="nu0">0.0254</span>, :length, %w<span class="br0">&#123;</span>&lt;meter&gt;<span class="br0">&#125;</span> <span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;&#8217;&lt;cell&gt;&#8217; =&gt; <span class="br0">&#91;</span>%w<span class="br0">&#123;</span>cells cell<span class="br0">&#125;</span>, <span class="nu0">1</span>, :counting, %w<span class="br0">&#123;</span>&lt;each&gt;<span class="br0">&#125;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;&#8217;&lt;habenero&gt;&#8217;&nbsp; &nbsp;=&gt; <span class="br0">&#91;</span>%<span class="br0">&#123;</span>degH<span class="br0">&#125;</span>, <span class="nu0">100</span>, :temperature, %w<span class="br0">&#123;</span>&lt;celcius&gt;<span class="br0">&#125;</span><span class="br0">&#93;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; Unit.<span class="me1">setup</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>The format for the array is [%w{primary_name synonyms}, conversion_factor, unit_type, %w{<base-unit>}]</p>
<h2> Useful References </h2>
<p><a href="http://en.wikipedia.org/wiki/Units_of_measurement">Wikipedia: Units of Measurement</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/09/19/ruby-units/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rails Testing Strategies: Valid and Invalid Data</title>
		<link>http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-valid-and-invalid-data/</link>
		<comments>http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-valid-and-invalid-data/#comments</comments>
		<pubDate>Mon, 12 Jun 2006 17:36:52 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-valid-and-invalid-data/</guid>
		<description><![CDATA[Consider this test&#8230;




def test_create


&#160; @item = Item.new


&#160; assert_valid @item


end 




Can you tell what the expected result of this test is?  
It turns out that you can&#8217;t tell from reading this test if it will pass or fail without knowing if &#8216;Item.new&#8217; returns a valid or invalid object.  If &#8216;Item&#8217; has a &#8216;validates_presence_of&#8217; statement [...]]]></description>
			<content:encoded><![CDATA[<p>Consider this test&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_create</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @item = Item.<span class="me1">new</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_valid @item</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>Can you tell what the expected result of this test is?  </p>
<p>It turns out that you can&#8217;t tell from reading this test if it will pass or fail without knowing if &#8216;Item.new&#8217; returns a valid or invalid object.  If &#8216;Item&#8217; has a &#8216;validates_presence_of&#8217; statement in there, this new item will be invalid and the test will fail.  In addition, this test is &#8216;fragile&#8217;, subtle changes to the model definition can make the result of this test change.  This isn&#8217;t such a big deal if all you are doing is testing to see if an object is valid or not, but if you are writing a more involved test that depends on this assumption then you might be in trouble.</p>
<p>Currently the only way to ensure that your test data is valid is to add an assertion to each test making the appropriate test.  This isn&#8217;t very DRY.</p>
<p>One trick that I use to avoid repeating myself a lot in tests is to define a mock object like this..(putting this in a mock object keeps these methods out of production mode).</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># mocks/test/item.rb</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">require</span> &#8216;models/item&#8217;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Item</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">#this assumes that an empty object is invalid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">#modify this as needed</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">new_invalid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Item.<span class="me1">new</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">new_valid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; item = <span class="kw2">self</span>.<span class="me1">new_invalid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; item.<span class="me1">name</span> = <span class="st0">&quot;new valid item&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1"># add any additional statements </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1"># required to make the item valid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>Then in my tests I add&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_valid</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_valid Item.<span class="me1">new_valid</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_invalid</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert !Item.<span class="me1">new_invalid</span>.<span class="me1">valid</span>?, <span class="st0">&quot;Item not invalid&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_save_invalid</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @invalid_item = Item.<span class="me1">new_invalid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert !@invalid_item.<span class="me1">save</span>, <span class="st0">&quot;Saved invalid item&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>This way I am guaranteed to get a valid or invalid item when I need one.  in addition, if the definition of valid or invalid changes sufficiently the &#8216;test_valid&#8217; and &#8216;test_invalid&#8217; tests will fail, warning me that my assumptions in the rest of the tests are incorrect.</p>
<p>One problem with this approach is if your tests care about HOW the object is invalid.  In that case, you might need additional methods like &#8216;new_with_invalid_name&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-valid-and-invalid-data/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rails Testing Strategies: Positive and Negative Controls</title>
		<link>http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-positive-and-negative-controls/</link>
		<comments>http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-positive-and-negative-controls/#comments</comments>
		<pubDate>Mon, 12 Jun 2006 17:36:36 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-positive-and-negative-controls/</guid>
		<description><![CDATA[Testing your rails application can be a great way to make your application robust.  Proper testing aids in debugging, and can alert you when some minor change you made breaks something horribly.  Rails has some nice testing features &#8216;baked-in&#8217;, but the best advice I have seen so far regarding which actual tests to [...]]]></description>
			<content:encoded><![CDATA[<p>Testing your rails application can be a great way to make your application robust.  Proper testing aids in debugging, and can alert you when some minor change you made breaks something horribly.  Rails has some nice testing features &#8216;baked-in&#8217;, but the best advice I have seen so far regarding which actual tests to run is &#8230; &#8216;it depends&#8217;.  </p>
<p>Needless to say, that isn&#8217;t very practical advice.</p>
<p>Over the course of developing my own rails application, I have developed a list of &#8216;tests&#8217; that I like to run on my models and controllers. This article will discuss some of the strategic design of tests, but won&#8217;t cover how to implement them (well, not much anyway).</p>
<h2>Positive Controls</h2>
<p>Tests should be designed with both &#8216;positive&#8217; and &#8216;negative&#8217; controls in mind.  A positive control is a test or assertion that succeeds when you expect it to, and likewise a negative control fails when you expect it to.  For example, if you create and save an object using predefined good data, an appropriate positive control could be&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_create</div>
</li>
<li class="li1">
<div class="de1">&nbsp; num_items = Item.<span class="me1">count</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; @item = Item.<span class="me1">new</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert @item.<span class="me1">save</span>, <span class="st0">&quot;Item not saved&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_valid @item&nbsp; <span class="co1"># a positive control</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_equal num_items + <span class="nu0">1</span>, Item.<span class="me1">count</span>&nbsp; <span class="co1">#another positive control</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>In this case, we just check to make sure that the object is valid and that the number of them in the database has increased by one.  These types of tests don&#8217;t really tell you much.  The only time these types of tests fail is when something is seriously wrong with your database server, your ruby install, or your rails install.  The only other time this type of test will fail is if you change the definition of the &#8216;Item&#8217; model so that creating one using the &#8216;new&#8217; method results in an invalid object (more on that later).  The problem with positive controls is that when they pass, all they tell you is that nothing in the test caused an assertion to fail.  This could mean that everything is hunky-dory, or it could mean that your assertions are not working as expected.</p>
<p>Passing the test doesn&#8217;t mean much if the test itself can&#8217;t detect a failure.  Our test would pass just fine, even if the object model should really have a &#8216;validates_presence_of :name&#8217; specified.  It&#8217;s also easy to see that if we write a test like this&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_name</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @object = Object.<span class="me1">new</span><span class="br0">&#40;</span>:name=&gt;&#8217;<span class="kw3">test</span> name&#8217;<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; @object.<span class="me1">save</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_valid @object</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_equal @object.<span class="me1">name</span>, &#8216;<span class="kw3">test</span> name&#8217;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>This test will still pass, even if the validation is not specified. Still, it&#8217;s nice to know that things work as expected.</p>
<h2>Negative Controls</h2>
<p>Negative controls are really much more handy.  A Negative control is a test that is designed to detect when things fail.<br />
If we write another test for our &#8216;item&#8217;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Item &lt; ActiveRecord::Base</div>
</li>
<li class="li1">
<div class="de1">&nbsp; validates_presence_of :name</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_valid</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @item = Item.<span class="me1">new</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; @item.<span class="me1">name</span> = <span class="st0">&quot;test item&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert_valid @item</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> test_invalid</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @item = Item.<span class="me1">new</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assert !@item.<span class="me1">valid</span>?, <span class="st0">&quot;Object is not invalid&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>If &#8216;test_invalid&#8217; passes, then you can be confident that your &#8216;validates_presence_of&#8217; is working.  If it had been commented out during a bug fix and never replaced, this test would fail, while &#8216;test_valid&#8217; would still happily report a success.</p>
<p>The same approach should be used when testing model methods.  At least one test for each type of expected output, and one for each possible failure mode.  Then make sure that the negative controls are in place so that they will pass when the model is fed corrupt data.</p>
<p>The bottom line&#8230; tests should be written to pass when you pass valid data, but they should also be written to pass when you pass missing, corrupt, or extra data.  The failure modes are the most useful during development since they will point out places where bugs and security holes may creep into your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/06/12/rails-testing-strategies-positive-and-negative-controls/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Ruby Series (&#8221;A&#8221;..&#8221;Z&#8221;)</title>
		<link>http://www.sciwerks.com/blog/2006/05/28/ruby-series-az/</link>
		<comments>http://www.sciwerks.com/blog/2006/05/28/ruby-series-az/#comments</comments>
		<pubDate>Mon, 29 May 2006 01:42:53 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/05/28/ruby-series-az/</guid>
		<description><![CDATA[One of the nice features of Ruby is the &#8216;range&#8217; object that can represent a series of objects ranging from one object to another.  A numerical series like [1, 2, 3, 4, 5, 6] can be represented as (1..6).  This works well and is quite useful in many cases.  Any object that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the nice features of Ruby is the &#8216;range&#8217; object that can represent a series of objects ranging from one object to another.  A numerical series like [1, 2, 3, 4, 5, 6] can be represented as (1..6).  This works well and is quite useful in many cases.  Any object that implements a &#8216;<=>&#8216; operator and a &#8217;succ&#8217; method can be implemented as a range.</p>
<p>Ranges of letters don&#8217;t work as well.  </p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="st0">&quot;A&quot;</span>..<span class="st0">&quot;C&quot;</span><span class="br0">&#41;</span>.<span class="me1">map</span> <span class="co1">#=&gt; [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;]</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="st0">&quot;Z&quot;</span>..<span class="st0">&quot;AB&quot;</span><span class="br0">&#41;</span>.<span class="me1">map</span> <span class="co1">#=&gt; [] </span></div>
</li>
</ol>
</div>
</div>
<p>What?  Why doesn&#8217;t the second example work?  That&#8217;s a valid series, it should return [ "Z", "AA", "AB" ].<br />
The problem here seems to be that these strings don&#8217;t sort the way that &#8216;range&#8217; expects them to.  (Thanks to Robert Dober for pointing this out)</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="st0">&quot;Y&quot;</span> &lt;=&gt; <span class="st0">&quot;Z&quot;</span>&nbsp; <span class="co1">#=&gt; -1</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&quot;Z&quot;</span> &lt;=&gt; <span class="st0">&quot;AA&quot;</span> <span class="co1">#=&gt; 1 </span></div>
</li>
</ol>
</div>
</div>
<p>So one solution to this problem is to simply create a class that implements the correct semantics and sorts properly.<br />
Here&#8217;s one implementation.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> LetterSeries</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">include</span> Comparable</div>
</li>
<li class="li1">
<div class="de1">&nbsp; attr_accessor :value</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> initialize<span class="br0">&#40;</span>n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @value = n</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">from_i</span><span class="br0">&#40;</span>n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; b = <span class="st0">&quot;A&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span>n<span class="nu0">-1</span><span class="br0">&#41;</span>.<span class="me1">times</span> <span class="br0">&#123;</span>b.<span class="me1">succ</span>!<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; LetterSeries.<span class="me1">new</span><span class="br0">&#40;</span>b<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> to_i</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span><span class="nu0">0</span>&#8230;@value.<span class="me1">size</span><span class="br0">&#41;</span>.<span class="me1">map</span>.<span class="me1">inject</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>|sum,n| sum + <span class="br0">&#40;</span>@value<span class="br0">&#91;</span><span class="nu0">-1</span>-n<span class="br0">&#93;</span><span class="nu0">-64</span><span class="br0">&#41;</span> * <span class="nu0">26</span>**<span class="br0">&#40;</span>n<span class="br0">&#41;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> &lt;=&gt;<span class="br0">&#40;</span>other<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">to_i</span> &lt;=&gt; other.<span class="me1">to_i</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> succ</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; LetterSeries.<span class="me1">new</span><span class="br0">&#40;</span>@value.<span class="me1">succ</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> to_s</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @value</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> +<span class="br0">&#40;</span>n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; LetterSeries.<span class="me1">from_i</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">to_i</span> + n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> -<span class="br0">&#40;</span>n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; LetterSeries.<span class="me1">from_i</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">to_i</span> &#8211; n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>Now some tests&#8230;.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1">z = LetterSeries.<span class="me1">new</span><span class="br0">&#40;</span><span class="st0">&quot;Z&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">ac = LetterSeries.<span class="me1">new</span><span class="br0">&#40;</span><span class="st0">&quot;AC&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span>z..<span class="me1">ac</span><span class="br0">&#41;</span>.<span class="me1">map</span> <span class="br0">&#123;</span>|n| n.<span class="me1">to_s</span><span class="br0">&#125;</span> <span class="co1">#=&gt; [&quot;Z&quot;,&quot;AA&quot;,&quot;AB&quot;,&quot;AC&quot;]</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span>z..<span class="me1">z</span><span class="nu0">+3</span><span class="br0">&#41;</span>.<span class="me1">map</span> <span class="br0">&#123;</span>|n| n.<span class="me1">to_s</span>| <span class="co1">#=&gt; [&quot;Z&quot;,&quot;AA&quot;,&quot;AB&quot;,&quot;AC&quot;] </span></div>
</li>
</ol>
</div>
</div>
<p>Feel free to use this code if you find it handy.<br />
See <a href="http://www.ruby-forum.com/topic/67300?reply_to=83726#83612 ">http://www.ruby-forum.com/topic/67300?reply_to=83726#83612 </a>for the original discussion that inspired this code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/05/28/ruby-series-az/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
