<?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>kahfei</title>
	<atom:link href="http://www.kahfei.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.kahfei.com</link>
	<description>freshly brewed ideas</description>
	<lastBuildDate>Thu, 29 Jul 2010 15:15:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>remote_user in rails application</title>
		<link>http://www.kahfei.com/?p=153</link>
		<comments>http://www.kahfei.com/?p=153#comments</comments>
		<pubDate>Thu, 29 Jul 2010 15:15:55 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=153</guid>
		<description><![CDATA[I am developing a little application within instantrails, suppose to work in an intranet environment. The application let user book a part number, by filling some information. For better user experience, I plan to capture the username at the other end that make the booking, instead of creating another layer of login feature and require [...]]]></description>
			<content:encoded><![CDATA[<p>I am developing a little application within instantrails, suppose to work in an intranet environment.</p>
<p>The application let user book a part number, by filling some information. For better user experience, I plan to capture the username at the other end that make the booking, instead of creating another layer of login feature and require the user to login again. There is no complex authentication needed, just capture the username and store it together with other information. </p>
<p>ENV['user'] will give you the username that has the rails application running, which is not what we want.<br />
ENV['remote_user'] return nothing at all.<br />
I read through quite <a href="http://www.ruby-forum.com/topic/83067">a</a> <a href="http://old.nabble.com/Forcing-a-proxied-host-to-generate-REMOTE_USER-to2911573.html#a2914465">few</a> <a href="http://kbullock.ringworld.org/2010/06/05/apache-rails-and-remote_user/">discussions</a> on the net, it seems like apache didn&#8217;t actually pass the remote_user value to mongrel, hence rails application couldn&#8217;t call out remote_user. To fix that, we will need to modify on the apache configuration file, httpd.conf, and .htaccess. Admittedly, I really know close to nothing about the setup of these files, and have a hard time following the suggested steps, as to where to add the suggested line of codes, which file to modify, etc, etc. </p>
<p>So I figure, if we forgo apache in the setup, will it be easier to fetch the remote_user value directly from mongrel? or from thin if it is the web server being used. </p>
<p>But still, I just couldn&#8217;t get it working.<br />
Is there any gem or plugin need to be installed before this remote_user tingy can be pass to rails application?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=153</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>thin on windows</title>
		<link>http://www.kahfei.com/?p=149</link>
		<comments>http://www.kahfei.com/?p=149#comments</comments>
		<pubDate>Wed, 28 Jul 2010 21:27:45 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=149</guid>
		<description><![CDATA[Read about thin for a while (faster than webrick, faster than mongrel), but didn&#8217;t really get the chance to try it out, until recently. As I am looking for moving my little rails application out of instantrails to a proper production environment, decided to give thin a spin, too bad Passenger isn&#8217;t supporting Windows platform, [...]]]></description>
			<content:encoded><![CDATA[<p>Read about <a href="http://code.macournoyer.com/thin/">thin</a> for a while (faster than webrick, faster than mongrel), but didn&#8217;t really get the chance to try it out, until recently.</p>
<p>As I am looking for moving my little rails application out of instantrails to a proper production environment, decided to give thin a spin, too bad <a href="http://www.modrails.com/">Passenger</a> isn&#8217;t supporting Windows platform, heard a lot of good thing about it too.</p>
<p>Installing thin server on Windows isn&#8217;t quite straightforward though. Thin actually consist of mongrel parser, EventMachine, and rack, but the latest EventMachine gem is not win32 binary release, that pose a lot of problem for the installation. </p>
<p>There are a few workarounds suggested on the net,<br />
from <a href="http://www.winstonyw.com/2008/03/23/ruby-thin-server-on-windows/">Winston</a></p>
<li>install rack with the usual gem install</li>
<li>install EventMachine gem 0.8.1 as local gem</li>
<li>install thin gem install thin &#8211;ignore-dependencie</li>
<p>But this didn&#8217;t quite workout for me, some suggested to install <a href="http://www.mingw.org/">MinGW</a> and <a href="http://www.mingw.org/wiki/MSYS">MSYS</a>, then the normal path of gem install thin should just work. Tried that, but fail as well. </p>
<p>Then found out that EventMachine has a binary release at 0.12.6, and with that it works right away for me <img src='http://www.kahfei.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> , here are the steps that did it for me,</p>
<li>gem install rack</li>
<li>gem install eventmachine-0.12.6-x86-mswin32-60.gem (<a href="http://rubyforge.org/frs/download.php/52982/eventmachine-0.12.6-x86-mswin32-60.gem">download</a> gem to local, and install locally)</li>
<li>gem install thin</li>
<p>Only after a full circle of searching around the net, that I found out these steps are actually mentioned in the comments by Marty McGee in winston&#8217;s original post. Duh. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=149</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>bubble sort</title>
		<link>http://www.kahfei.com/?p=117</link>
		<comments>http://www.kahfei.com/?p=117#comments</comments>
		<pubDate>Mon, 26 Jul 2010 13:57:49 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=117</guid>
		<description><![CDATA[Understanding sorting algorithm by just reading text or still illustration takes a lot of mind gymnastic. Apparently, there are lot of videos online, trying to explain the concept. But not all explain it as good though, some with really boring animation, some with crappy background music, and some even both, . Here are two that [...]]]></description>
			<content:encoded><![CDATA[<p>Understanding sorting algorithm by just reading text or still illustration takes a lot of mind gymnastic. Apparently, there are lot of videos online, trying to explain the concept. But not all explain it as good though, some with really boring animation, some with crappy background music, and some even both, <img src='http://www.kahfei.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  . Here are two that I find did a great job in explaining bubble sort,</p>
<p>This one from <a href="http://codegearguru.com/">codegearguru</a>. Instead of creating graphic, or cutting out paper to represent data in the sorting process, just use the poker cards! That seems obvious now, but I didn&#8217;t think about it when I try to illustrate sorting to myself initially. Ha. Think there is a series of video created for different type of sorting algorithm as well, so definitely a great tool to understand sorting algorithm. Bonus near the end of the video, another technique called shaker sort or cocktail sort is also explained, which is basically a bidirectional bubble sort which should speed up the sorting speed.<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/UnK5ueUgc88&amp;hl=en_US&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/UnK5ueUgc88&amp;hl=en_US&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>Another one from <a href="http://vimeo.com/user385283">Miles Hauskaz</a>, just plain simple and succinct explanation and a video nicely done as well.<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="225" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=4315697&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="400" height="225" src="http://vimeo.com/moogaloop.swf?clip_id=4315697&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a href="http://vimeo.com/4315697">Sorting Algorithms &#8211; Bubble Sort</a> from <a href="http://vimeo.com/user385283">Miles Hauskaz</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>So, this is my implementation of bubble sort, admittedly not the best that you can find, but that will do for me now, until I have time to come back and do some refactoring.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">arr = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>g a c b j e e<span style="color:#006600; font-weight:bold;">&#125;</span>
i = arr.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
count = <span style="color:#006666;">0</span>
n = arr.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">def</span> swap<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span> 
  arr = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> m <span style="color:#006600; font-weight:bold;">&gt;</span> n
    arr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> n
	arr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> m
  <span style="color:#9966CC; font-weight:bold;">else</span>
    arr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> m
	arr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> n
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span> n <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">while</span> count <span style="color:#006600; font-weight:bold;">&lt;</span> i
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;comparing #{arr[count]} to #{arr[count+1]}&quot;</span>
    a = swap<span style="color:#006600; font-weight:bold;">&#40;</span>arr<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">&#93;</span>,arr<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    arr<span style="color:#006600; font-weight:bold;">&#91;</span>count..<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = a
    count <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  n <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
  i <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
  count = <span style="color:#006666;">0</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">p</span> arr</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=117</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>map and each</title>
		<link>http://www.kahfei.com/?p=131</link>
		<comments>http://www.kahfei.com/?p=131#comments</comments>
		<pubDate>Sun, 25 Jul 2010 13:42:06 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=131</guid>
		<description><![CDATA[It is a bit difficult for me to understand the difference between map and each, until I found out this in irb accidentally, irb&#40;main&#41;:001:0&#62; name = %w&#123;guido knuth adrian dhh pg&#125; =&#62; &#91;&#34;guido&#34;, &#34;knuth&#34;, &#34;adrian&#34;, &#34;dhh&#34;, &#34;pg&#34;&#93; irb&#40;main&#41;:002:0&#62; name.each &#123;&#124;n&#124; n.upcase&#125; =&#62; &#91;&#34;guido&#34;, &#34;knuth&#34;, &#34;adrian&#34;, &#34;dhh&#34;, &#34;pg&#34;&#93; irb&#40;main&#41;:003:0&#62; name.map &#123;&#124;n&#124; n.upcase&#125; =&#62; &#91;&#34;GUIDO&#34;, &#34;KNUTH&#34;, &#34;ADRIAN&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>It is a bit difficult for me to understand the difference between map and each, until I found out this in irb accidentally,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> name = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>guido knuth adrian dhh pg<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;guido&quot;</span>, <span style="color:#996600;">&quot;knuth&quot;</span>, <span style="color:#996600;">&quot;adrian&quot;</span>, <span style="color:#996600;">&quot;dhh&quot;</span>, <span style="color:#996600;">&quot;pg&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> name.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n.<span style="color:#9900CC;">upcase</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;guido&quot;</span>, <span style="color:#996600;">&quot;knuth&quot;</span>, <span style="color:#996600;">&quot;adrian&quot;</span>, <span style="color:#996600;">&quot;dhh&quot;</span>, <span style="color:#996600;">&quot;pg&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:003:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> name.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n.<span style="color:#9900CC;">upcase</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;GUIDO&quot;</span>, <span style="color:#996600;">&quot;KNUTH&quot;</span>, <span style="color:#996600;">&quot;ADRIAN&quot;</span>, <span style="color:#996600;">&quot;DHH&quot;</span>, <span style="color:#996600;">&quot;PG&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>map and each are suppose to do almost the same thing, but why <em>each</em> didn&#8217;t return the correct result in uppercase like what <em>map</em> did? </p>
<p>The different is <em>each</em> yield each element in the collection to the code block, in this case perform the upcase method, but return the receiver, which is the original array, hence an array of lower case names.</p>
<p>Whereas, <em>map</em> return a new array, consists of elements already went through the code block, hence an array of upper case names.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:007:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> name.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> n.<span style="color:#9900CC;">upcase</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#996600;">&quot;GUIDO&quot;</span>
<span style="color:#996600;">&quot;KNUTH&quot;</span>
<span style="color:#996600;">&quot;ADRIAN&quot;</span>
<span style="color:#996600;">&quot;DHH&quot;</span>
<span style="color:#996600;">&quot;PG&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;guido&quot;</span>, <span style="color:#996600;">&quot;knuth&quot;</span>, <span style="color:#996600;">&quot;adrian&quot;</span>, <span style="color:#996600;">&quot;dhh&quot;</span>, <span style="color:#996600;">&quot;pg&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:006:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> name.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> n.<span style="color:#9900CC;">upcase</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#996600;">&quot;GUIDO&quot;</span>
<span style="color:#996600;">&quot;KNUTH&quot;</span>
<span style="color:#996600;">&quot;ADRIAN&quot;</span>
<span style="color:#996600;">&quot;DHH&quot;</span>
<span style="color:#996600;">&quot;PG&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>So here you can see with p, both <em>map</em> and <em>each</em> output the uppercase names, but look more carefully, the return value of <em>each</em>, as expected, is the original array. <em>map</em> however, return an array of nils! </p>
<p>Why is that?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=131</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>A much better sort</title>
		<link>http://www.kahfei.com/?p=123</link>
		<comments>http://www.kahfei.com/?p=123#comments</comments>
		<pubDate>Wed, 21 Jul 2010 15:28:49 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=123</guid>
		<description><![CDATA[Okay, much better only in a relative term, comparing to my earlier sorting which didn&#8217;t take care of duplicate elements. Two major changes here. I do away with the temporary variable for holding on the smallest number result from each round of the comparison, instead elements are now passed over directly to the sorted array. [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, much better only in a relative term, comparing to my earlier sorting which didn&#8217;t take care of duplicate elements.<br />
Two major changes here.</p>
<ul>
<li>I do away with the temporary variable for holding on the smallest number result from each round of the comparison, instead elements are now passed over directly to the sorted array.</li>
<li>Those elements that get passed to the sorted array, will be deleted from the original array, but one items at one round of comparison, which mean if there are duplicated elements, only one of the instance will be removed. </li>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> hackers.<span style="color:#9900CC;">delete_at</span><span style="color:#006600; font-weight:bold;">&#40;</span>hackers.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span>sorted<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It look a lot more wordy than a shorter</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">hackers.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>sorted<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>but this is deleting ONLY the first element that match the value, compare to the shorter version where it will remove all elements. So it preserve the other element for next round of comparison, and eventually make it to the sorted array.<br />
Complete code as below,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">hackers = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span> gosling matz dhh adrian guido matz knuth dhh adrian guido <span style="color:#006600; font-weight:bold;">&#125;</span>
i = hackers.<span style="color:#9900CC;">length</span>
count = <span style="color:#006666;">0</span>
sorted = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span> count <span style="color:#006600; font-weight:bold;">&lt;</span> i
  sorted<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">&#93;</span> = hackers.<span style="color:#9900CC;">inject</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;">&lt;</span> b ? a : b <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#CC0066; font-weight:bold;">p</span> sorted<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">&#93;</span>
  hackers.<span style="color:#9900CC;">delete_at</span><span style="color:#006600; font-weight:bold;">&#40;</span>hackers.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span>sorted<span style="color:#006600; font-weight:bold;">&#91;</span>count<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  count <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">p</span> sorted</pre></div></div>

<p>And the result&#8230;.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;adrian&quot;</span>, <span style="color:#996600;">&quot;adrian&quot;</span>, <span style="color:#996600;">&quot;dhh&quot;</span>, <span style="color:#996600;">&quot;dhh&quot;</span>, <span style="color:#996600;">&quot;gosling&quot;</span>, <span style="color:#996600;">&quot;guido&quot;</span>, <span style="color:#996600;">&quot;guido&quot;</span>, <span style="color:#996600;">&quot;knuth&quot;</span>, <span style="color:#996600;">&quot;matz&quot;</span>, <span style="color:#996600;">&quot;matz&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=123</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A slightly better sort</title>
		<link>http://www.kahfei.com/?p=112</link>
		<comments>http://www.kahfei.com/?p=112#comments</comments>
		<pubDate>Sat, 17 Jul 2010 06:32:32 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=112</guid>
		<description><![CDATA[Finally have the time to come back to the sorting exercise, here is a slightly better version of sort. It still doesn&#8217;t take care of duplicate elements, but compare to the first sorting version, this one is shorter, as the whole finding smallest element left in the array is now done by inject and a [...]]]></description>
			<content:encoded><![CDATA[<p>Finally have the time to come back to the sorting exercise, here is a <em>slightly</em> better version of sort.<br />
It still doesn&#8217;t take care of duplicate elements, but compare to the first <a href="http://www.kahfei.com/?p=78">sorting version</a>, this one is shorter, as the whole finding smallest element left in the array is now done by inject and a ternary operator, a whole 7 lines of code cut to just 1. And now it no longer sort animals, it sort a list of hackers <img src='http://www.kahfei.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">hackers = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>matz ddh adrian guido gosling knuth linus<span style="color:#006600; font-weight:bold;">&#125;</span>
i = hackers.<span style="color:#9900CC;">length</span> 
sorted = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">while</span> i <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
  small = hackers.<span style="color:#9900CC;">inject</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;">&lt;</span> b ? a : b<span style="color:#006600; font-weight:bold;">&#125;</span>
  sorted.<span style="color:#9900CC;">push</span> small 
  hackers.<span style="color:#9900CC;">delete</span> small
  i <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">p</span> sorted</pre></div></div>

<p>And the result of the sort is</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;adrian&quot;</span>, <span style="color:#996600;">&quot;ddh&quot;</span>, <span style="color:#996600;">&quot;gosling&quot;</span>, <span style="color:#996600;">&quot;guido&quot;</span>, <span style="color:#996600;">&quot;knuth&quot;</span>, <span style="color:#996600;">&quot;linus&quot;</span>, <span style="color:#996600;">&quot;matz&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>So the next iteration will be to take care of duplicated elements&#8230;stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=112</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>optional parameters in 1.8.7 and 1.9</title>
		<link>http://www.kahfei.com/?p=108</link>
		<comments>http://www.kahfei.com/?p=108#comments</comments>
		<pubDate>Wed, 14 Jul 2010 05:27:17 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=108</guid>
		<description><![CDATA[Apparently you can have optional parameter listed in the middle for your method in Ruby version 1.9 C:\&#62;pik switch 191 &#160; C:\&#62;irb irb&#40;main&#41;:001:0&#62; def talk&#40;a,*b,c&#41; irb&#40;main&#41;:002:1&#62; p a,b,c irb&#40;main&#41;:003:1&#62; end =&#62; nil irb&#40;main&#41;:004:0&#62; talk 'a','b','c' &#34;a&#34; &#91;&#34;b&#34;&#93; &#34;c&#34; =&#62; &#91;&#34;a&#34;, &#91;&#34;b&#34;&#93;, &#34;c&#34;&#93; but not Ruby version 1.8.7 C:\&#62;pik switch 187 C:\&#62;irb irb&#40;main&#41;:001:0&#62; def talk&#40;a,*b,c&#41; irb&#40;main&#41;:002:1&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Apparently you can have optional parameter listed in the middle for your method in Ruby version 1.9</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">C:\<span style="color:#006600; font-weight:bold;">&gt;</span>pik switch <span style="color:#006666;">191</span>
&nbsp;
C:\<span style="color:#006600; font-weight:bold;">&gt;</span>irb
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">def</span> talk<span style="color:#006600; font-weight:bold;">&#40;</span>a,<span style="color:#006600; font-weight:bold;">*</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&gt;</span>   <span style="color:#CC0066; font-weight:bold;">p</span> a,b,c
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:003:<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:004:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> talk <span style="color:#996600;">'a'</span>,<span style="color:#996600;">'b'</span>,<span style="color:#996600;">'c'</span>
<span style="color:#996600;">&quot;a&quot;</span>
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;b&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#996600;">&quot;c&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;b&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#996600;">&quot;c&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>but not Ruby version 1.8.7</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">C:\<span style="color:#006600; font-weight:bold;">&gt;</span>pik switch <span style="color:#006666;">187</span>
C:\<span style="color:#006600; font-weight:bold;">&gt;</span>irb
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">def</span> talk<span style="color:#006600; font-weight:bold;">&#40;</span>a,<span style="color:#006600; font-weight:bold;">*</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&gt;</span>   <span style="color:#CC0066; font-weight:bold;">p</span> a,b,c
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:003:<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC00FF; font-weight:bold;">SyntaxError</span>: compile error
<span style="color:#006600; font-weight:bold;">&#40;</span>irb<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">1</span>: syntax error, unexpected tIDENTIFIER, expecting tAMPER <span style="color:#9966CC; font-weight:bold;">or</span> <span style="color:#996600;">'&amp;'</span>
<span style="color:#9966CC; font-weight:bold;">def</span> talk<span style="color:#006600; font-weight:bold;">&#40;</span>a,<span style="color:#006600; font-weight:bold;">*</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>
               ^
        from <span style="color:#006600; font-weight:bold;">&#40;</span>irb<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">1</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:004:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> quit</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=108</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visualization of Sorting</title>
		<link>http://www.kahfei.com/?p=102</link>
		<comments>http://www.kahfei.com/?p=102#comments</comments>
		<pubDate>Tue, 13 Jul 2010 21:38:49 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=102</guid>
		<description><![CDATA[A beautiful visualization of sorting algorithm&#8230; via Hacker News]]></description>
			<content:encoded><![CDATA[<p>A beautiful <a href="http://sortvis.org/index.html">visualization of sorting algorithm</a>&#8230;<br />
<a href="http://www.kahfei.com/wp-content/uploads/2010/07/sort1.png"><img src="http://www.kahfei.com/wp-content/uploads/2010/07/sort1.png" alt="" title="sort" width="600" height="397" class="alignnone size-full wp-image-106" /></a></p>
<p>via <a href="http://news.ycombinator.com/item?id=1511332">Hacker News</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=102</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Summing up factorial</title>
		<link>http://www.kahfei.com/?p=95</link>
		<comments>http://www.kahfei.com/?p=95#comments</comments>
		<pubDate>Sat, 10 Jul 2010 00:12:16 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=95</guid>
		<description><![CDATA[Another way for factorial, extending the integer class, so the function will feel more natural, instead of fact 6 for example, you just call it by 6.fact. Also, added a function to sum up the factorial value, counting down. Example, for 6!, it will be 6! + 5! + 4! + 3! + 2! + [...]]]></description>
			<content:encoded><![CDATA[<p>Another way for factorial, extending the integer class, so the function will feel more natural, instead of fact 6 for example, you just call it by 6.fact.<br />
Also, added a function to sum up the factorial value, counting down. Example, for 6!, it will be 6! + 5! + 4! + 3! + 2! + 1!<br />
Here go,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Integer</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> fact
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> == <span style="color:#006666;">0</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      n = <span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">fact</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sum_fact
    <span style="color:#006600; font-weight:bold;">&#40;</span>1..<span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</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;">+</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">fact</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> 4.<span style="color:#9900CC;">fact</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> 4.<span style="color:#9900CC;">sum_fact</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=95</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>failing factorial</title>
		<link>http://www.kahfei.com/?p=89</link>
		<comments>http://www.kahfei.com/?p=89#comments</comments>
		<pubDate>Fri, 09 Jul 2010 10:51:18 +0000</pubDate>
		<dc:creator>kahfei</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kahfei.com/?p=89</guid>
		<description><![CDATA[Hours ago I got a programming test question about factorial, which I didn&#8217;t do really well, only completed with the help of the interviewer. Damn. The worst thing is that I have done this question before, and I know in the head I can use recursion for it, but I totally go blank on how [...]]]></description>
			<content:encoded><![CDATA[<p>Hours ago I got a programming test question about factorial, which I didn&#8217;t do really well, only completed with the help of the interviewer. Damn.<br />
The worst thing is that I have done this question before, and I know in the head I can use recursion for it, but I totally go blank on how to do it with recursion. Here go my chance&#8230;sigh&#8230;</p>
<p>Now, here is how factorial can be done.<br />
With Recursion.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> fact n
  <span style="color:#9966CC; font-weight:bold;">if</span> n == <span style="color:#006666;">0</span>
    <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    n = n <span style="color:#006600; font-weight:bold;">*</span> fact<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</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;">end</span></pre></div></div>

<p>That simple. </p>
<p>Factorial without recursion, using inject instead</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> fact n
  <span style="color:#9966CC; font-weight:bold;">if</span> n == <span style="color:#006666;">0</span>
    <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>1..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</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;">*</span>b <span style="color:#006600; font-weight:bold;">&#125;</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Isn&#8217;t that difficult, huh? What was I thinking?!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kahfei.com/?feed=rss2&amp;p=89</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
