<?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>Honolulu Hacker &#187; Programming</title>
	<atom:link href="http://honoluluhacker.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://honoluluhacker.com</link>
	<description>Tech, Linux, Rails by Honululu-based Kevin English</description>
	<lastBuildDate>Sat, 24 Sep 2011 22:53:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Ruby on Rails Class in Hawaii, October 3rd</title>
		<link>http://honoluluhacker.com/2011/09/24/ruby-on-rails-class-in-hawaii-october-3rd/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ruby-on-rails-class-in-hawaii-october-3rd</link>
		<comments>http://honoluluhacker.com/2011/09/24/ruby-on-rails-class-in-hawaii-october-3rd/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 22:53:21 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1237</guid>
		<description><![CDATA[I&#8217;m teaching an Intro Ruby on Rails Class at University of Hawaii&#8217;s Pacific New Media. Hope you can make it, here&#8217;s the flyer. Ruby on Rails Class Flyer]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m teaching an Intro Ruby on Rails Class at University of Hawaii&#8217;s Pacific New Media. Hope you can make it, here&#8217;s the flyer.</p>
<p><a href='http://honoluluhacker.com/wp-content/uploads/2011/09/Ruby_on_Rails_Class_Announment-1.pdf'>Ruby on Rails Class Flyer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2011/09/24/ruby-on-rails-class-in-hawaii-october-3rd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Migrating my heroku/postgres project to Jruby</title>
		<link>http://honoluluhacker.com/2011/07/22/migrating-my-herokupostgres-project-to-jruby/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=migrating-my-herokupostgres-project-to-jruby</link>
		<comments>http://honoluluhacker.com/2011/07/22/migrating-my-herokupostgres-project-to-jruby/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 21:57:42 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1213</guid>
		<description><![CDATA[A little inspired by all the JRuby buzz, I thought I&#8217;d take an existing rails app that I have running on heroku and migrate it to Jruby. I wanted to test how easy/hard it would be: I made a branch of my project: git checkout -b jruby-test Install jruby via rvm rvm install jruby-1.6.2 I [...]]]></description>
			<content:encoded><![CDATA[<p>A little inspired by all the JRuby buzz, I thought I&#8217;d take an existing rails app that I have running on heroku and migrate it to Jruby. I wanted to test how easy/hard it would be:</p>
<ol>
<li> I made a branch of my project:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> checkout <span style="color: #660033;">-b</span> jruby-test</pre></div></div>

</li>
<li>Install jruby via rvm

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rvm <span style="color: #c20cb9; font-weight: bold;">install</span> jruby-1.6.2</pre></div></div>

</li>
<li>I editted my .rvmc to use Jruby instead of MRI, from:

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rvm use 1.9.2@proj</pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rvm use jruby<span style="color:#006600; font-weight:bold;">-</span>1.6.2@proj<span style="color:#006600; font-weight:bold;">-</span>jruby</pre></div></div>

</li>
<li> I updated my rmvc from :

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rvm use 1.9.2@proj</pre></div></div>

<p>    to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rvm use jruby<span style="color:#006600; font-weight:bold;">-</span>1.6.2@proj<span style="color:#006600; font-weight:bold;">-</span>jruby</pre></div></div>

</li>
<li>
  I edited my Gemfile. This is where I knew it would get tricky.</p>
<ol type="a">
<li>Removed:

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">gem <span style="color:#996600;">'pg'</span></pre></div></div>

</li>
<li>Added:

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">gem <span style="color:#996600;">'activerecord-jdbc-adapter'</span>
gem <span style="color:#996600;">'activerecord-jdbcpostgresql-adapter'</span>
gem <span style="color:#996600;">'jdbc-postgres'</span></pre></div></div>

</li>
<li>Found out hard way had to remove ruby-debug19. DUh, dude <img src='http://honoluluhacker.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </li>
<li> <a href="http://rubygems.org/gems/texticle">Texticle</a> gem does not work. This makes sense because it is plugin to postgres ActiveRecord driver. However, I use this for my minimalistic search functionality.
</li>
</ol>
<li>Ran &#8216;rake spec&#8217; on the project. My tests for the search feature failed as expected because of the missing Texticle dependency. I had one test that was failing on<br />
  Regular expression. The regular expression looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> last_item =~ <span style="color:#006600; font-weight:bold;">/</span>sector<span style="color:#006600; font-weight:bold;">|</span>leverage_point<span style="color:#006600; font-weight:bold;">/</span></pre></div></div>

<p>  When examined the code a bit, I found out that last_item was actually a symbol. I guess MRI automatically converts symbols to strings for Regular Expressions and JRuby does not. I changed the line to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> last_item.<span style="color:#9900CC;">to_s</span> =~ <span style="color:#006600; font-weight:bold;">/</span>sector<span style="color:#006600; font-weight:bold;">|</span>leverage_point<span style="color:#006600; font-weight:bold;">/</span></pre></div></div>

<p>  and it worked fine</p>
</li>
<li>Ran &#8216;rails s thin&#8217; and my app was up and running.</li>
</ol>
<p>Not bad for a JRuby amatuer. Although not having the Texticle gem is a show stopper on this project.<br />
It&#8217;s not that Texticle is the best solution for search but we have some code written in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2011/07/22/migrating-my-herokupostgres-project-to-jruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Common Accurev Command Line commands and their git/svn counter parts</title>
		<link>http://honoluluhacker.com/2011/07/12/common-accurev-command-line-commands-and-their-gitsvn-counter-parts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=common-accurev-command-line-commands-and-their-gitsvn-counter-parts</link>
		<comments>http://honoluluhacker.com/2011/07/12/common-accurev-command-line-commands-and-their-gitsvn-counter-parts/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 08:01:41 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1198</guid>
		<description><![CDATA[I&#8217;m using Accurev for a new project, thought I&#8217;d throw up a blog post of the commands that I use most commonly: Make a new workspace, similar to &#8216;git clone&#8217; or &#8216;svn co&#8217;: accurev mkws -w workspacename -b streamname_from_server -l local_directory Note: accurev workspaces are similiar to a cloned git repo, kind of but not [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using Accurev for a new project, thought I&#8217;d throw up a blog post of the commands that I use most commonly:</p>
<p>Make a new workspace, similar to &#8216;git clone&#8217; or &#8216;svn co&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev mkws <span style="color: #660033;">-w</span> workspacename <span style="color: #660033;">-b</span> streamname_from_server <span style="color: #660033;">-l</span> local_directory</pre></div></div>

<p>Note: accurev workspaces are similiar to a cloned git repo, kind of but not really&#8230;. Are you confused yet?</p>
<p>Update my workspace from the stream, similiarly to a &#8220;git pull&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev update</pre></div></div>

<p>Get status of my workspace, ie, see what&#8217;s changed. Same as &#8220;git status&#8221; or &#8220;svn st&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev <span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #660033;">-n</span></pre></div></div>

<p>With the &#8216;accurev stat -n&#8217; command, unlike git, the files shown will be in relation to your current working directory. So if you do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate
accurev <span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #660033;">-n</span></pre></div></div>

<p>You will only see files you have changed in your db/migrate folder. (Why are you changing your migrations???)</p>
<p>In accurev, file not checked into the work space are called &#8220;external&#8221; files, you see all these with this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev <span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #660033;">-x</span></pre></div></div>

<p>Add external file, same as &#8220;git add&#8221; or &#8220;svn add&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev add <span style="color: #660033;">-x</span> path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">file</span>
<span style="color: #666666; font-style: italic;"># or add a directory</span>
accurev add <span style="color: #660033;">-x</span> <span style="color: #660033;">-d</span> directory</pre></div></div>

<p>Revert a file, same as &#8220;git checkout&#8221; or &#8220;svn revert&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev purge path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">file</span></pre></div></div>

<p>Promote all kept files to the stream, kind like &#8220;git push&#8221; or &#8220;svn commit&#8221; but more like &#8220;git push&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev promote –c “JIRA-<span style="color: #000000;">666</span> comment” –k</pre></div></div>

<p>I&#8217;ll add more as my project progresses&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">accurev keep <span style="color: #660033;">-R</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;codenarc fixes&quot;</span> grails-app<span style="color: #000000; font-weight: bold;">/</span>domain</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2011/07/12/common-accurev-command-line-commands-and-their-gitsvn-counter-parts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Override behavior of &#8220;rake spec&#8221;  task</title>
		<link>http://honoluluhacker.com/2011/04/29/override-behavior-of-rake-spec-task/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=override-behavior-of-rake-spec-task</link>
		<comments>http://honoluluhacker.com/2011/04/29/override-behavior-of-rake-spec-task/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 01:04:05 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1183</guid>
		<description><![CDATA[Basically, I have a bunch of folders in my spec directory for a rails project: spec/acceptance spec/controllers spec/fixtures spec/helpers spec/javascripts spec/lib spec/mailers spec/models spec/support The spec files in my acceptance folder are my capybara/selenium tests and still in experimental phase. I don&#8217;t want them to run every time and definitely don&#8217;t want the CI server [...]]]></description>
			<content:encoded><![CDATA[<p>Basically, I have a bunch of folders in my spec directory for a rails project:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">spec<span style="color: #000000; font-weight: bold;">/</span>acceptance
spec<span style="color: #000000; font-weight: bold;">/</span>controllers
spec<span style="color: #000000; font-weight: bold;">/</span>fixtures
spec<span style="color: #000000; font-weight: bold;">/</span>helpers
spec<span style="color: #000000; font-weight: bold;">/</span>javascripts
spec<span style="color: #000000; font-weight: bold;">/</span>lib
spec<span style="color: #000000; font-weight: bold;">/</span>mailers
spec<span style="color: #000000; font-weight: bold;">/</span>models
spec<span style="color: #000000; font-weight: bold;">/</span>support</pre></div></div>

<p>The spec files in my acceptance folder are my capybara/selenium tests and still in experimental phase. I don&#8217;t want them to run every time and definitely don&#8217;t want the CI server running them. However, when I run &#8216;rake&#8217; or &#8216;rake spec&#8217;, it runs everything in my &#8216;spec&#8217; folder. I want it it to run the specs in every directory <em>except</em> acceptance.</p>
<p>I discovered there is not easy way to override a rake task.  Redefining the task appends to it which to me is a pretty intuitive default behavior. Apparently, dchemsky knows about this and has <a href="https://github.com/jimweirich/rake/issues/17">opened an issue with rake.</a> I was able to piece together a solution from an old <a href="http://blog.jayfields.com/2008/02/rake-task-overwriting.html">blog post by Jay Fields.</a></p>
<p>Feel free to comment if you have a better way to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rake'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rspec/core/rake_task'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Rake::Task</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> abandon
    <span style="color:#0066ff; font-weight:bold;">@actions</span>.<span style="color:#9900CC;">clear</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#6666ff; font-weight:bold;">Rake::Task</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spec</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">abandon</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">RSpec::Core::RakeTask</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:spec</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
  t.<span style="color:#9900CC;">pattern</span> = <span style="color:#996600;">&quot;spec/{models,views,controllers,helpers}/**/*_spec.rb&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
namespace <span style="color:#ff3333; font-weight:bold;">:spec</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#996600;">&quot;Run the code examples in spec/acceptance&quot;</span>
  <span style="color:#6666ff; font-weight:bold;">RSpec::Core::RakeTask</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:acceptance</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;db:test:prepare&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#9900CC;">pattern</span> = <span style="color:#996600;">&quot;spec/acceptance/**/*_spec.rb&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2011/04/29/override-behavior-of-rake-spec-task/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Rails 3 helpers and routes in the console or a rake task</title>
		<link>http://honoluluhacker.com/2011/02/05/using-rails-3-helpers-and-routes-in-the-console-or-a-rake-task/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-rails-3-helpers-and-routes-in-the-console-or-a-rake-task</link>
		<comments>http://honoluluhacker.com/2011/02/05/using-rails-3-helpers-and-routes-in-the-console-or-a-rake-task/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 21:45:49 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1176</guid>
		<description><![CDATA[To use a Rails 3 helper in the console If this is your helper: module ItemsHelper def custom_item_display&#40;item&#41; item.to_s end end And you have a route that looks like this: resources :items On the console, you would do this: ruby-1.9.2-p136 :001 &#62; item = &#34;hello world&#34; ruby-1.9.2-p136 :002 &#62; helper.extend ItemsHelper ruby-1.9.2-p136 :003 &#62; helper.custom_item_display&#40;item&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>To use a Rails 3 helper in the console</p>
<p>If this is your helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ItemsHelper
  <span style="color:#9966CC; font-weight:bold;">def</span> custom_item_display<span style="color:#006600; font-weight:bold;">&#40;</span>item<span style="color:#006600; font-weight:bold;">&#41;</span>
    item.<span style="color:#9900CC;">to_s</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And you have a route that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  resources <span style="color:#ff3333; font-weight:bold;">:items</span></pre></div></div>

<p> On the console, you would do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>p136 :001 <span style="color:#006600; font-weight:bold;">&gt;</span> item = <span style="color:#996600;">&quot;hello world&quot;</span>
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>p136 :002 <span style="color:#006600; font-weight:bold;">&gt;</span> helper.<span style="color:#9900CC;">extend</span> ItemsHelper
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>p136 :003 <span style="color:#006600; font-weight:bold;">&gt;</span> helper.<span style="color:#9900CC;">custom_item_display</span><span style="color:#006600; font-weight:bold;">&#40;</span>item<span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;hello world&quot;</span> 
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>p136 :004 <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActionController::UrlWriter</span>
 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span> 
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>p136 :005 <span style="color:#006600; font-weight:bold;">&gt;</span> items_path
 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/items&quot;</span></pre></div></div>

<p>In the rake task you do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">namespace <span style="color:#ff3333; font-weight:bold;">:example</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#996600;">&quot;Do example items&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:items</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    item = <span style="color:#996600;">&quot;Hello World&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">include</span> ItemsHelper
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ItemsHelper.custom_item_display(item) = #{custom_item_display(item)}&quot;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActionController::UrlWriter</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;items_path = #{items_path}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Hope that helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2011/02/05/using-rails-3-helpers-and-routes-in-the-console-or-a-rake-task/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using TinyTDS to connect to SQL Server with Ruby on Rails 3 (for Mac OSX)</title>
		<link>http://honoluluhacker.com/2011/01/13/using-tinytds-to-connect-to-sql-server-with-ruby-on-rails-3-for-mac-osx/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-tinytds-to-connect-to-sql-server-with-ruby-on-rails-3-for-mac-osx</link>
		<comments>http://honoluluhacker.com/2011/01/13/using-tinytds-to-connect-to-sql-server-with-ruby-on-rails-3-for-mac-osx/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 19:36:58 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[freetds]]></category>
		<category><![CDATA[mssql rails]]></category>
		<category><![CDATA[tinytds]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1157</guid>
		<description><![CDATA[Note: please see comments by Ken Collins (metaskills.net) at the bottom of this entry. He mentions a better way to do this without even installing FreeTDS. You will need FreeTDS to connect to MSSQL Server on Unix-based environments. You MUST use Homebrew to install FreeTDS on the Mac. MacPorts is evil and will not install [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note: please see comments by Ken Collins (<a href="http://metaskills.net/">metaskills.net</a>) at the bottom of this entry. He mentions a better way to do this without even installing FreeTDS.</strong></p>
<p>You will need <a href="http://www.freetds.org/">FreeTDS</a> to connect to MSSQL Server on Unix-based environments. You MUST use Homebrew to install FreeTDS on the Mac. MacPorts is evil and will not install FreeTDS correctly. Installation of Homebrew is covered excellently in the Homebrew installation docs:<br />
<a href="https://github.com/mxcl/homebrew/wiki/installation">https://github.com/mxcl/homebrew/wiki/installation</a></p>
<p>Edit the FreeTDS configuration for the FreeTDS library:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">brew edit freetds</pre></div></div>

<p>Replace from &#8220;def install&#8221;  to &#8220;end &#8221; with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> install 
  args = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;--prefix=#{prefix}&quot;</span>,
          <span style="color:#996600;">&quot;--with-tdsver=7.0&quot;</span>,
          <span style="color:#996600;">&quot;--enable-msdblib&quot;</span>,
          <span style="color:#996600;">&quot;--mandir=#{man}&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">&quot;./configure&quot;</span>, <span style="color:#006600; font-weight:bold;">*</span>args
  <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">'make'</span>
  <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">'make install'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Install FreeTDS:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">brew <span style="color: #c20cb9; font-weight: bold;">install</span> freetds</pre></div></div>

<p>Add an entry to the bottom of your freetds.conf for your MSSQL Server. This file is found at /usr/local/etc/freetds.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>my_sql_server<span style="color: #7a0874; font-weight: bold;">&#93;</span> 
host = <span style="color: #000000; font-weight: bold;">&lt;</span>hostip<span style="color: #000000; font-weight: bold;">&gt;</span>
port = <span style="color: #000000;">1433</span> 
tds version = <span style="color: #000000;">7.0</span></pre></div></div>

<p>Add the TinyTDS and activerecord-sqlserver-adapter gems to your Rails Gemfile.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  gem <span style="color:#996600;">'tiny_tds'</span>
  gem <span style="color:#996600;">'activerecord-sqlserver-adapter'</span>, :<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<p>Run bundler to install the gems:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">bundle <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Finally, add the connection to your <em>database.yml</em> file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">development:
  adapter: sqlserver
  mode: dblib
  dataserver: my_sql_server
  database: my_database_name
  username: my_username
  password: xxxxx
  timeout: <span style="color:#006666;">5000</span></pre></div></div>

<p>A reference for installing UnixODBC (which I don&#8217;t recommend), you can follow this tutorial:<br />
<a href="http://stuartc.posterous.com/mssql2005-on-rails-on-snow-leopard-the-easies">MSSQL2005 on Rails on Snow Leopard (the easiest way I know how.)</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2011/01/13/using-tinytds-to-connect-to-sql-server-with-ruby-on-rails-3-for-mac-osx/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Honolulu Hacker&#8217;s Guide to Lite Weight Backpacking</title>
		<link>http://honoluluhacker.com/2010/06/24/honolulu-hackers-guide-to-lite-weight-backpacking/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=honolulu-hackers-guide-to-lite-weight-backpacking</link>
		<comments>http://honoluluhacker.com/2010/06/24/honolulu-hackers-guide-to-lite-weight-backpacking/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 08:48:42 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=1143</guid>
		<description><![CDATA[Based on some backpacking experience over the years, I&#8217;ve come up with this list of ways to reduce the weight of your pack. Read and Learn. Toilet Paper: Anyone who has moved before knows one thing: Books are heavy. Guess what books are made of?&#8230;. PAPER! That&#8217;s right, the same thing that they use to [...]]]></description>
			<content:encoded><![CDATA[<p>Based on some backpacking experience over the years, I&#8217;ve come up with this list of ways to reduce the weight of your pack. Read and Learn.</p>
<ul>
<li>Toilet Paper: Anyone who has moved before knows one thing: Books are heavy. Guess what books are made of?&#8230;. PAPER! That&#8217;s right, the same thing that they use to make toilet paper. I suggest you lighten your toilet paper load. Next time you go to the bath room, make note of how much toilet paper you need. How many times do you wipe? If you average 3-4 wipes, can use only 2 squares per wipe and can estimate you will be going to the bathroom 2 times a day, do some math, figure out how much mountain money you will REALLY need. If you are a guy, congratulations, you can bring less TP. Don&#8217;t tell the girls this, they may ask you to carry their tent.
    </li>
<li>Deodorant: Seriously? All or some of your backpacking buddies are gonna smell worse than the slums of Manila. Why are you going out our way to smell good? Leave this item at home.</li>
<li>Camera: Skip it. Don&#8217;t worry, one of you buddies will bring a camera especially if you are with a big group. One of them may even carry a tripod, a monopod and so much other camera gear that he is going to need a hip replacement by the age of 35. Don&#8217;t be that guy. Plus, think about it: if you are taking all the picture, you won&#8217;t be in any of them. My wiser older brother once told me: a picture without a person is postcard. If you want pictures of the scenery, just go on flickr and do a search. Chance are pretty good someone else has been there. Make their image your wallpaper. When your co-workers comes, you can brag about what a great trip you had and how much cooler you are than them. Go ahead, gloat away. You&#8217;ve got the wallpaper to back it up.</li>
<li>Tent: So, you hiked all the way out into the woods so you can sleep INDOORS. Forget that. I have seen someone make a very sustainable shelter out of a rain tarp. You just need to be creative. Cavemen didn&#8217;t always make it back to the cave and they didn&#8217;t have REI either. What did they do? If the going gets really rough, you can sneak into your buddy&#8217;s tent.
<li> Water: I don&#8217;t think I need to ask this but what human do you know that will deny another person water?&#8230; and you can always count on others to carry water.
</ul>
<p>This only some of the great ideas that will be present in my new book on backpacking. Please e-mail to preorder it.</p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2010/06/24/honolulu-hackers-guide-to-lite-weight-backpacking/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Force download of fasta files and aln files with Apache</title>
		<link>http://honoluluhacker.com/2010/01/14/force-download-of-fasta-files-and-aln-files-with-apache/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=force-download-of-fasta-files-and-aln-files-with-apache</link>
		<comments>http://honoluluhacker.com/2010/01/14/force-download-of-fasta-files-and-aln-files-with-apache/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 20:11:19 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Bioinformatics]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=998</guid>
		<description><![CDATA[If you are serving fasta files or alignment files on your server, you may want to force users to download them instead of previewing them in the browser. My application would return the fasta files as Content-Type text/plain. I wanted to force it to application/x-fasta and force download. This is accomplished rather easily in Apache [...]]]></description>
			<content:encoded><![CDATA[<p>If you are serving fasta files or alignment files on your server, you may want to force users to download them instead of previewing them in the browser. My application would return the fasta files as Content-Type text/plain. I wanted to force it to application/x-fasta and force download. This is accomplished rather easily in Apache with the following directive:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">  &lt;<span style="color: #000000; font-weight:bold;">FilesMatch</span> <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>(?i:fasta)$&quot;</span>&gt;
    <span style="color: #00007f;">ForceType</span> application/x-fasta
    <span style="color: #00007f;">Header</span> set Content-Disposition attachment
  &lt;/<span style="color: #000000; font-weight:bold;">FilesMatch</span>&gt;
  &lt;<span style="color: #000000; font-weight:bold;">FilesMatch</span> <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>(?i:aln)$&quot;</span>&gt;
    <span style="color: #00007f;">ForceType</span> application/x-aln
    <span style="color: #00007f;">Header</span> set Content-Disposition attachment
  &lt;/<span style="color: #000000; font-weight:bold;">FilesMatch</span>&gt;</pre></div></div>

<p>You will have to enable the apache module &#8220;mod_header&#8221; for this to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2010/01/14/force-download-of-fasta-files-and-aln-files-with-apache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Ruby &amp; Hpricot to find lowest mortgage rate in Hawaii</title>
		<link>http://honoluluhacker.com/2009/12/14/using-ruby-hpricot-to-find-lowest-mortgage-rate-in-hawaii/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-ruby-hpricot-to-find-lowest-mortgage-rate-in-hawaii</link>
		<comments>http://honoluluhacker.com/2009/12/14/using-ruby-hpricot-to-find-lowest-mortgage-rate-in-hawaii/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 05:39:27 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=958</guid>
		<description><![CDATA[Each week the Honolulu Board of Realtors publishes a report of Hawaii Mortgage Rates. To find the lowest rate for your category is difficult. A non-programming solution would be to copy it into excel, delete all the rows that you need and then sort by the rate column. This takes too much time so I [...]]]></description>
			<content:encoded><![CDATA[<p>Each week the Honolulu Board of Realtors publishes a report of <a href="http://www.hicentral.com/MortgageRates.asp">Hawaii Mortgage Rates</a>. To find the lowest rate for your category is difficult. A non-programming solution would be to copy it into excel, delete all the rows that you need and then sort by the rate column. This takes too much time so I wrote a ruby script that parses this data. This is also a demonstration of how to use the ruby tool Hpricot, an HTML Parser. You will need to install the Hpricot gem for this to work</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'hpricot'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#term = '1-YR ARM'</span>
<span style="color:#008000; font-style:italic;">#term = '30-YR Fixed'</span>
term = <span style="color:#996600;">'15-YR Fixed'</span>
&nbsp;
doc = Hpricot.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://www.hicentral.com/MortgageRates.asp&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
&nbsp;
rates = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> 
lender_name = <span style="color:#996600;">&quot;&quot;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;table&quot;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;table&quot;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;tr&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span> row <span style="color:#006600; font-weight:bold;">|</span>
   arr =<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
   <span style="color:#006600; font-weight:bold;">&#40;</span>row<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;td&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span> cell<span style="color:#006600; font-weight:bold;">|</span>  
     arr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> cell.<span style="color:#9900CC;">inner_html</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
   <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#9966CC; font-weight:bold;">if</span> arr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">15</span><span style="color:#006600; font-weight:bold;">-</span>YR Fixed<span style="color:#006600; font-weight:bold;">/</span>
      lender_name = arr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      arr.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>lender_name<span style="color:#006600; font-weight:bold;">&#41;</span> 
      lender_name.<span style="color:#CC0066; font-weight:bold;">sub!</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'&lt;br /&gt;'</span>,<span style="color:#996600;">' - '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">unless</span> arr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{term}/</span>
   lender_data =<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> 
   lender_data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:lender_name</span><span style="color:#006600; font-weight:bold;">&#93;</span> = lender_name    
   lender_data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:term</span><span style="color:#006600; font-weight:bold;">&#93;</span> = arr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> 
   lender_data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:apr</span><span style="color:#006600; font-weight:bold;">&#93;</span> = arr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_f</span>
   rates <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> lender_data
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006666;">5</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span> rank <span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Losest Rate ##{rank+1}&quot;</span>
  row = rates.<span style="color:#9900CC;">min</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span> a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:apr</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> b<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:apr</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> 
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;  Name: #{row[:lender_name]}&quot;</span>  
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;  Term: #{row[:term]}&quot;</span> 
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;  APR: #{row[:apr]}&quot;</span>  
  rates.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>row<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Here&#8217;s what&#8217;s going on:</p>
<ul>
<li>On line 7 you will notice that I am interested in the 15-Year mortgage rate. You can change this value to get the report for the term you want. </li>
<li>On line 9, the program will download the latest rates from the hicentral.com website and parse the page returning an hpricot doc object.</li>
<li>From line 14 to 30, the program parses each line in the mortgage rate table. The logic is custom to this table. The table is unusual because the lender name is first cell only on the first line (15-YR Fixed) for each lender. To accommodate this, we match the line that has &#8220;15-YR Fixed&#8221; in second position and delete the lender name from the array (lines 19-23). We then assign the data to our summary data structure (lines 25-29)</li>
<li>Finally, we show the top 5 lowest mortgage rates (lines 32-38). To do this we use the Ruby <a href="http://ruby-doc.org/core/classes/Enumerable.html#M003134">max</a> method (line 34). We delete the current max element so it is not counted in the next loop iteration (line 38).
</ul>
<p>Thomas Lecklider has a great tutorial on how to use  <a href="http://hpricot.com/">Hpricot</a> called <a href="http://lecklider.com/posts/4-using-hpricot-to-traverse-and-parse-html">Using Hpricot to Traverse and Parse HTML.</a></p>
<p>Your commends are welcome. Give some refactoring advice if you like. I have wordpress plugin for pre tag so to write ruby code just do:</p>
<p>&lt;pre lang=&quot;ruby&quot;&gt;<br />
puts YEAH<br />
&lt;/pre&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2009/12/14/using-ruby-hpricot-to-find-lowest-mortgage-rate-in-hawaii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A SQL Server file &#8216;basename&#8217; function</title>
		<link>http://honoluluhacker.com/2009/11/24/a-sql-server-file-basename-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-sql-server-file-basename-function</link>
		<comments>http://honoluluhacker.com/2009/11/24/a-sql-server-file-basename-function/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 03:31:29 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=901</guid>
		<description><![CDATA[Given a file path: /var/www/html/index.html Returns: index.html Pretty common, here&#8217;s how you do it: Perl: use File::Basename; $fullname = &#34;/usr/local/src/perl-5.6.1.tar.gz&#34;; $file = basename&#40;$fullname&#41;; PHP: $path = &#34;/home/httpd/html/index.php&#34;; $file = basename&#40;$path&#41;; Ruby: path = &#34;/usr/lib/ruby/site_ruby/1.8/rubygems/version.rb&#34; File.basename path Python: import os.path path = &#34;/usr/local/bin/python&#34; os.path.basename&#40;path&#41; T-SQL (MsSQL Server): 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>Given a file path: /var/www/html/index.html<br />
Returns: index.html</p>
<p>Pretty common, here&#8217;s how you do it:</p>
<p><b>Perl:</b></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Basename</span><span style="color: #339933;">;</span> 
<span style="color: #0000ff;">$fullname</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;/usr/local/src/perl-5.6.1.tar.gz&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #0000ff;">$file</span> <span style="color: #339933;">=</span> basename<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$fullname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>PHP:</b></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/home/httpd/html/index.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Ruby:</b></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">path = <span style="color:#996600;">&quot;/usr/lib/ruby/site_ruby/1.8/rubygems/version.rb&quot;</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">basename</span> path</pre></div></div>

<p><b>Python:</b></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
path =  <span style="color: #483d8b;">&quot;/usr/local/bin/python&quot;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">basename</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span></pre></div></div>

<p><b>T-SQL (MsSQL Server):</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_file_basename<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #808080; font-style: italic;">-- Add the parameters for the function here</span>
	@file_path <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
    <span style="color: #993333; font-weight: bold;">DECLARE</span> @file_basename <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #993333; font-weight: bold;">IF</span> charindex<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>, @file_path) != 0 
	   set @file_basename= reverse(substring(reverse(@file_path), 1, charindex('</span>\<span style="color: #ff0000;">', reverse(@file_path))-1))
	else
	   set @file_basename=@file_path
&nbsp;
	return @file_basename
END</span></pre></td></tr></table></div>

<p>My brilliant co-worker figured this out. The magic is done on line 11:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">reverse<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>reverse<span style="color: #66cc66;">&#40;</span>@file_path<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> charindex<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>, reverse(@file_path))-1))</span></pre></div></div>

<p>It reverses the string, find the first occurance of the character &#8216;\&#8217;, takes the substring to that character and the reverses again. How elegant!</p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2009/11/24/a-sql-server-file-basename-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

