<?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-units</title>
	<atom:link href="http://www.sciwerks.com/blog/category/ruby/ruby-units/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>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>
	</channel>
</rss>
