<?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>Ahabman &#187; ruby</title>
	<atom:link href="http://ahabman.com/blog/category/code/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://ahabman.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 27 Nov 2011 19:52:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Command finished notification with Ruby</title>
		<link>http://ahabman.com/blog/2010/09/command-finished-notification-with-ruby/</link>
		<comments>http://ahabman.com/blog/2010/09/command-finished-notification-with-ruby/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 23:02:07 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=125</guid>
		<description><![CDATA[I wanted a visual notification in Ubuntu when long running commands finished in Terminal. My solution, run the command and pipe it into notify. $ a-long-running-command &#124; notify notify.rb Make it executable add an alias to it in ~/.bashrc Requires: ruby, rubygems, and libnotify. Similar solutions: &#8220;alert&#8221; alias (I&#8217;ll be switching to this in Maverick), [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted a visual notification in Ubuntu when long running commands finished in Terminal. My solution, run the command and pipe it into notify.</p>
<p>$ a-long-running-command | notify</p>
<p><a href="http://www.ahabman.com/blog/wp-content/uploads/2010/09/notify.png"><img title="notify" src="http://www.ahabman.com/blog/wp-content/uploads/2010/09/notify-300x188.png" alt="" width="300" height="188" /></a></p>
<p>notify.rb</p>
<pre class="brush: ruby; title: ; notranslate">#!/usr/bin/env ruby
require 'rubygems'
require 'libnotify'
Libnotify.show :summary =&gt; &quot;Command finished.&quot;, :body =&gt; &quot;#{ARGF.read}&quot;</pre>
<p>Make it executable</p>
<pre class="brush: bash; title: ; notranslate">chmod u+x ~/scripts/notify.rb</pre>
<p>add an alias to it in ~/.bashrc</p>
<pre class="brush: bash; title: ; notranslate">alias notify=&quot;~/scripts/notify.rb&quot;</pre>
<p>Requires: <a href="http://www.ruby-lang.org/en/"><br />
ruby</a>, <a href="http://rubygems.org/">rubygems</a>, and <a href="http://github.com/vargolo/ruby-libnotify">libnotify</a>.</p>
<p>Similar solutions:<br />
<a href="http://blog.dustinkirkland.com/2010/07/dear-command-line-please-ping-me-when.html">&#8220;alert&#8221; alias</a> (I&#8217;ll be switching to this in Maverick), <a href="http://blogs.divisibleprime.com/ronin/articles/2008/03/10/command-line-gnome-notification"><br />
a python version</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2010/09/command-finished-notification-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Duration</title>
		<link>http://ahabman.com/blog/2009/06/ruby-duration/</link>
		<comments>http://ahabman.com/blog/2009/06/ruby-duration/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 13:02:57 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=39</guid>
		<description><![CDATA[Download ruby duration lib Duration.new recieves a string describing a duration of time, and converts it into: seconds, and a standard readable format. Or, you can pass Duration.new an integer representing seconds, and receive the same standard readable format. Usage: Passing a String Passing an Fixnum (of seconds) &#160; Example valid input strings: &#8220;2 weeks&#8221;, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ahabman.com/blog/wp-content/uploads/2009/06/duration.zip">Download ruby duration lib</a></p>
<p>Duration.new recieves a string describing a duration of time, and converts it into: seconds, and a standard readable format.<br />
Or, you can pass Duration.new an integer representing seconds, and receive the same standard readable format.</p>
<p>Usage:</p>
<p>Passing a String</p>
<pre class="brush: ruby; title: ; notranslate">
duration = Duration.new(&quot;2weeks 8 hr 30m&quot;)
duration.seconds  # =&gt; 1240200
duration.readable  # =&gt; &quot;2 weeks, 8 hours and 30 mins&quot;
</pre>
<p>Passing an Fixnum (of seconds)</p>
<pre class="brush: ruby; title: ; notranslate">
duration = Duration.new(1240200)
duration.readable  # =&gt; &quot;2 weeks, 8 hours and 30 mins&quot;
</pre>
<p>&nbsp;</p>
<p>Example valid input strings:</p>
<p>&#8220;2 weeks&#8221;, &#8220;2wks&#8221;, &#8220;2w&#8221;,<br />
&#8220;8h&#8221;, &#8220;8 hrs&#8221;,&#8221;8 hours&#8221;,<br />
&#8220;30m&#8221;, &#8220;0:30&#8243;, &#8220;30 minutes&#8221;, &#8220;30min&#8221;,<br />
&#8220;2 weeks, 8 hours and 30 minutes&#8221;, &#8220;2w 8h 30m&#8221;, &#8220;2w 8:30&#8243;,<br />
&#8220;4 hours 30 minutes&#8221;, &#8220;4 hours and 30m&#8221;, &#8220;4h 30min&#8221;, &#8220;4:30&#8243;, &#8220;4.5&#8243;, &#8220;4.50&#8243;, &#8220;4h, 30 min&#8221;, &#8220;4.5 hours&#8221;, &#8220;4.50h&#8221;</p>
<p>&nbsp;</p>
<p><a href="http://ahabman.com">ahabman</a> added functionality, extended and mashed-up this <a href="http://stackoverflow.com/questions/657309/how-to-parse-days-hours-minutes-seconds-in-ruby">stack overflow article</a> and <a href="http://www.postal-code.com/binarycode/2007/04/04/english-friendly-timespan/">english friendly timespan</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2009/06/ruby-duration/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ruby csv to structured hash</title>
		<link>http://ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/</link>
		<comments>http://ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 20:29:02 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=31</guid>
		<description><![CDATA[I had a csv like this: object color flavor shape apple red sweet round banana yellow sweet long lemon yellow sour round and I wanted a ruby hash structured like this: So I wrote this, which does the job:]]></description>
			<content:encoded><![CDATA[<p>I had a csv like this:</p>
<table style="height: 84px;" border="1" width="166">
<tbody>
<tr>
<th>object</th>
<th>color</th>
<th>flavor</th>
<th>shape</th>
</tr>
<tr>
<td>apple</td>
<td>red</td>
<td>sweet</td>
<td>round</td>
</tr>
<tr>
<td>banana</td>
<td>yellow</td>
<td>sweet</td>
<td>long</td>
</tr>
<tr>
<td>lemon</td>
<td>yellow</td>
<td>sour</td>
<td>round</td>
</tr>
</tbody>
</table>
<p>and I wanted a ruby hash structured like this:</p>
<pre class="brush: ruby; title: ; notranslate">
{
'apple'=&gt; { 'color'=&gt;'red', 'flavor'=&gt;'sweet', 'shape'=&gt;'round'},
'banana'=&gt; { 'color'=&gt;'yellow', 'flavor'=&gt;'sweet', 'shape'=&gt;'long'},
'lemon'=&gt; { 'color'=&gt;'yellow', 'flavor'=&gt;'sour', 'shape'=&gt;'round'}
}
</pre>
<p>So I wrote this, which does the job:</p>
<pre class="brush: ruby; title: ; notranslate">
require &quot;faster_csv&quot;

def csv_to_structured_hash
	arr_of_arrs = FasterCSV.read( 'your.csv' )
	stuff = {}
	header = arr_of_arrs.shift
		arr_of_arrs.each_with_index do |row, i|
		thing = { row[0] =&gt; {} }
			header.each_with_index do |col, header_index|
			thing[ row[0] ][ header[header_index] ] = row[ header_index]
			end
		stuff.update( thing )
		end
	return stuff
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby conversion module</title>
		<link>http://ahabman.com/blog/2008/07/ruby-conversion-module/</link>
		<comments>http://ahabman.com/blog/2008/07/ruby-conversion-module/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 12:52:11 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=26</guid>
		<description><![CDATA[This is a ruby module useful for conversions dealing with length, weight, torque.]]></description>
			<content:encoded><![CDATA[<p>This is a <a href="http://www.ahabman.com/blog/wp-content/uploads/2008/07/convert.rb">ruby module useful for conversions</a> dealing with length, weight, torque.</p>
<pre class="brush: ruby; title: ; notranslate">
 module Convert 

   def Convert.number_with_precision(number, precision=2)
     &quot;%01.#{precision}f&quot; % number
   rescue
     number
   end 

   def Convert.mm_to_in(mm, precision=2)
     number_with_precision(mm * 0.03937, precision)
   end 

   def Convert.in_to_mm(inches, precision=2)
     number_with_precision(inches / 0.03937 , precision)
   end 

   def Convert.feet_to_meters(f, precision=2)
     number_with_precision( f * 0.3048, precision)
   end 

   def Convert.meters_to_inches(m, precision=2)
     number_with_precision( m * 39.37, precision)
   end 

   def Convert.meters_to_feet(m, precision=2)
     number_with_precision( m * 3.281, precision)
   end 

   def Convert.kg_to_lbs(kg, precision=2)
     number_with_precision( kg * 2.2   , precision)
   end 

   def Convert.lbs_to_kg(lbs, precision=2)
     number_with_precision( lbs / 2.2   , precision)
   end 

   def Convert.nm_to_inch_pounds(nm, precision=1)
     number_with_precision( nm * 8.850   , precision)
   end 

   def Convert.inch_pounds_to_nm(inlb, precision=1)
     number_with_precision( inlb / 8.850   , precision)
   end 

 end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/07/ruby-conversion-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

