<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>ahabman &#187; ruby</title>
	<atom:link href="http://www.ahabman.com/blog/category/code/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ahabman.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 05 Sep 2010 00:18:44 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- podcast_generator="podPress/8.8" - maintenance_release="8.8.4" -->
		<copyright>2007-2008 </copyright>
		<managingEditor>andy@ahabman.com (ahabman)</managingEditor>
		<webMaster>andy@ahabman.com (ahabman)</webMaster>
		<category>music</category>
		<ttl>1440</ttl>
		<itunes:keywords></itunes:keywords>
		<itunes:subtitle></itunes:subtitle>
		<itunes:summary></itunes:summary>
		<itunes:author>ahabman</itunes:author>
		<itunes:category text="Society &amp; Culture"/>
		<itunes:owner>
			<itunes:name>ahabman</itunes:name>
			<itunes:email>andy@ahabman.com</itunes:email>
		</itunes:owner>
		<itunes:block>No</itunes:block>
		<itunes:explicit>no</itunes:explicit>
		<itunes:image href="" />
		<image>
			<url></url>
			<title>ahabman</title>
			<link>http://www.ahabman.com/blog</link>
			<width>144</width>
			<height>144</height>
		</image>
		<item>
		<title>Command finished notification with Ruby</title>
		<link>http://www.ahabman.com/blog/2010/09/command-finished-notification-with-ruby/</link>
		<comments>http://www.ahabman.com/blog/2010/09/command-finished-notification-with-ruby/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 23:02:07 +0000</pubDate>
		<dc:creator>admin</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 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
#!/usr/bin/env ruby
require 'rubygems'
require 'libnotify'
Libnotify.show :summary =&#62; &#34;Command finished.&#34;, :body =&#62; &#34;#{ARGF.read}&#34;
Make it executable
chmod u+x ~/scripts/notify.rb
add an alias to it in ~/.bashrc
alias notify=&#34;~/scripts/notify.rb&#34;
Requires: 
ruby, rubygems, and libnotify.
Similar solutions:
&#8220;alert&#8221; alias (I&#8217;ll be switching [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted a visual notification 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;">#!/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;">chmod u+x ~/scripts/notify.rb</pre>
<p>add an alias to it in ~/.bashrc</p>
<pre class="brush: bash;">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://www.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://www.ahabman.com/blog/2009/06/ruby-duration/</link>
		<comments>http://www.ahabman.com/blog/2009/06/ruby-duration/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 13:02:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=39</guid>
		<description><![CDATA[[download id="1" format="1"]
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

duration = Duration.new(&#34;2weeks 8 hr 30m&#34;)
duration.seconds  # =&#62; 1240200
duration.readable  # =&#62; &#34;2 weeks, 8 hours [...]]]></description>
			<content:encoded><![CDATA[<p>[download id="1" format="1"]</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;">
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;">
duration = Duration.new(1240200)
duration.readable  # =&gt; &quot;2 weeks, 8 hours and 30 mins&quot;
</pre>
<p>Example valid input strings<br />
&#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>http://ahabman.com added functionallity, extended and mashed-up  http://stackoverflow.com/questions/657309/how-to-parse-days-hours-minutes-seconds-in-ruby  and  http://www.postal-code.com/binarycode/2007/04/04/english-friendly-timespan/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.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://www.ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/</link>
		<comments>http://www.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:

{
'apple'=&#62; { 'color'=&#62;'red',  'flavor'=&#62;'sweet', 'shape'=&#62;'round'},
'banana'=&#62; {  'color'=&#62;'yellow',  'flavor'=&#62;'sweet', 'shape'=&#62;'long'},
'lemon'=&#62; {   'color'=&#62;'yellow',  'flavor'=&#62;'sour', 'shape'=&#62;'round'}
}

So I wrote this, which does the job:

require &#34;faster_csv&#34;

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 &#124;row, i&#124;
		thing = { row[0] =&#62; {} }
			header.each_with_index do &#124;col, header_index&#124;
			thing[ [...]]]></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;">
{
'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;">
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://www.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://www.ahabman.com/blog/2008/07/ruby-conversion-module/</link>
		<comments>http://www.ahabman.com/blog/2008/07/ruby-conversion-module/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 12:52:11 +0000</pubDate>
		<dc:creator>andy</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.

 module Convert 

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

   def Convert.mm_to_in(mm, precision=2)
     number_with_precision(mm * 0.03937, precision)
   [...]]]></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;">
 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://www.ahabman.com/blog/2008/07/ruby-conversion-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
