<?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>RORLAMP Development</title>
	<atom:link href="http://railstech.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://railstech.com</link>
	<description>Go For Open Source</description>
	<lastBuildDate>Tue, 15 May 2012 05:06:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>RVM vs RBENV&#8230;</title>
		<link>http://railstech.com/2012/05/rvm-vs-rbenv/</link>
		<comments>http://railstech.com/2012/05/rvm-vs-rbenv/#comments</comments>
		<pubDate>Tue, 15 May 2012 05:06:02 +0000</pubDate>
		<dc:creator>Rushabh Hathi</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3.0]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=835</guid>
		<description><![CDATA[Just installed a new precise pangolin and thought of giving rbenv a try….
So collected this with a bit of googling….
rbenv is a new lightweight Ruby version management tool built by Sam Stephenson(of 37signals and Prototype.js fame).
The established leader in the Ruby version management scene is RVM but rbenv is an interesting alternative if you want [...]]]></description>
			<content:encoded><![CDATA[<p>Just installed a new precise pangolin and thought of giving <strong>rbenv</strong> a try….</p>
<p>So collected this with a bit of googling….</p>
<p><strong>rbenv</strong> is a new lightweight Ruby version management tool built by <strong>Sam Stephenson</strong>(of 37signals and Prototype.js fame).</p>
<p>The established leader in the Ruby version management scene is <strong>RVM</strong> but <strong>rbenv</strong> is an interesting alternative if you want or need something  significantly lighter with fewer features. Think of it as a bit like  Sinatra and Rails. It’s not about which is the best, it’s about which is  better for you and your current requirements.<br />
<strong> What’s  rbenv?</strong></p>
<p>Compared to RVM, rbenv is light. For example, it doesn’t include any  mechanism to install Ruby implementations like RVM does. Its sole job is  to manage multiple Ruby “environments” and it allows you to quickly  switch between Ruby implementations either on a local directory or  default ‘system-wide’ basis.</p>
<p>With rbenv, you install Ruby implementations manually or, if you  prefer a little help, you can try ruby-build, another project of Sam’s  that provides RVM esque recipes for installing seven popular Ruby  implementation and version combos.</p>
<p>rbenv primarily works by creating ‘shim’ files in ~/.rbenv/shims  which call up the correct version of files for each Ruby implementation  behind the scenes. This means ~/.rbenv/shims will be in your path and  there’s no threat of incompatibilities between libraries or systems like  Bundler and rbenv.</p>
<p>The key thing to be aware of, however, is that if you install a gem  that includes ‘binaries’ (or any generally available command line  scripts), you need to run rbenv rehash so that rbenv can create the  necessary shim files.<br />
<strong> INSTALLATION</strong></p>
<p>Firstly, it’s worth noting that by default rbenv is incompatible with  RVM because RVM overrides the gem command with a function. This means  to get the full rbenv experience you’ll need to do a rvm implode to wipe  away your RVM installation or, at the least, remove/comment out the RVM  loader line in your .bash_profile and/or .bashrc.</p>
<p>The installation instructions for rbenv are likely to change fast due  to its youth, so I suggest the README. However, rbenv has just been  made into a homebrew package on OS X, so if you’re a homebrew user , try:</p>
<blockquote><p>brew update brew install rbenv rbenv rehash</p></blockquote>
<p>And then add this line to your ~/.bash_profile or equivalent:</p>
<blockquote><p>eval &#8220;$(rbenv init -)&#8221;</p></blockquote>
<p>When you open a new shell now, you can run commands like rbenv and  rbenv version to see what’s going on. rbenv versions should return  nothing since you won’t have any rbenv-enabled Ruby installations yet,  so move on to the next step..<br />
Installing Implementations for rbenv</p>
<p>If you have ruby-build installed, getting, say, Ruby 1.9.2-p290 installed is easy:</p>
<blockquote><p>ruby-build 1.9.2-p290 $HOME/.rbenv/versions/1.9.2-p290</p></blockquote>
<p>If you prefer to download tarballs and do your own Ruby installs,  however, you just need to set the directory prefix at the ./configure  stage in most cases. For example:</p>
<blockquote><p>./configure &#8211;prefix=$HOME/.rbenv/versions/1.9.2-p290</p></blockquote>
<p>Once you’ve installed a new Ruby in this way, you need to run rbenv  rehash in order for rbenv to create the ‘shim’ binaries necessary to  make the correction version of Ruby available on the path at all times.<br />
<strong> The RVM Splashback</strong></p>
<p>In the interests of completeness, it’d be amiss to not mention the  minor drama that kicked off on Twitter and Hacker News about rbenv’s  release.rbenv made its way on to Hacker News where, surprisingly, many people  railed against RVM. This, coupled with a slightly antagonistic tone  taken by rbenv’s README (which has now been taken away), led RVM’s  maintainer Wayne E Seguin to vent some pressure on Twitter:</p>
<p><img title="wayne" src="http://www.rubyinside.com/wp-content/uploads/2011/08/wayne.png" alt="" width="524" height="208" /></p>
<p>Sam quickly clarified his position:</p>
<p><img title="Screen shot 2011-08-12 at 16.00.59" src="http://www.rubyinside.com/wp-content/uploads/2011/08/Screen-shot-2011-08-12-at-16.00.59.png" alt="" width="522" height="254" /></p>
<p>Nonetheless, Wayne took a little time off, and a campaign to  encourage donations to Wayne for his work on RVM was kicked off on  Twitter (by Ryan Bates, I believe). The campaign went well, taking RVM’s  donations from $7468 to $11370 (at time of writing), a jump of almost  $4000 in a few days.</p>
<p>Part of the complaint made in rbenv’s README was about RVM’s  “obnoxious” redefinition of the “cd” shell builtin. Essentially, RVM  would create a new function which called “cd” but also took into account  any .rvmrc files present in each directory so that it could change Ruby  version automatically. While there was some validity to this concern,  Ben Atkin took some time to write a blog post correcting some of the  misinformation on this point.</p>
<p>In the end, however, all seems to be well, and Wayne is already  making regular commits to the RVM project again just days later.  Hopefully the outpouring of support from the Ruby community for RVM over  the past couple of days has shown Wayne that RVM still has a  significant user base, most of who aren’t going anywhere new anytime  soon. If you want to help out, of course, you can still donate to the  RVM Pledgie.<br />
<strong> Conclusion</strong></p>
<p>If you’re happy with RVM, there’s not much in rbenv to really leap  to. It’s just a lighter and more modular way to achieve the basic  functionality that RVM provides while missing out on a lot more  (although you can use rbenv-gemset to get some basic gemset-like  features).</p>
<p>If, however, you want something “lighter” than RVM, rbenv is  certainly worth a look. Its approach feels cleaner and more transparent  at a certain level, and if this is of utmost importance to you, you may  prefer it.</p>
<p>I personally switched back to rvm coz I am getting openssl error which is tricky(ofcourse mybad!) and not able to get over it…</p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2012/05/rvm-vs-rbenv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>N-choose-R</title>
		<link>http://railstech.com/2012/01/n-choose-r/</link>
		<comments>http://railstech.com/2012/01/n-choose-r/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 14:51:35 +0000</pubDate>
		<dc:creator>nilesh</dc:creator>
				<category><![CDATA[c]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=347</guid>
		<description><![CDATA[
Simple Solution for creating Permutations

While working on a research project, i came across a simple task of creating sets of permutation for a given objects. What i thought simple task become tedious after goggling it. I didn&#8217;t find any simple solutions for creating a permutations. So i create his one for those who wants simple solution for creating permutations.

How [...]]]></description>
			<content:encoded><![CDATA[<p><strong><br />
<h3>Simple Solution for creating Permutations</h3>
<p></strong><br />
While working on a research project, i came across a simple task of creating sets of permutation for a given objects. What i thought simple task become tedious after goggling it. I didn&#8217;t find any simple solutions for creating a permutations. So i create his one for those who wants simple solution for creating permutations.</p>
<p><strong><br />
<h4>How to solve complicated problems of creating permutation.</h4>
<p></strong></p>
<ul>
<li>Creating permutation of object such as set of colors, basically the task is to find combination of colors (i.e. color addition problem)</li>
</ul>
<p>Step 1 : create array of pointers to color objects</p>
<p>colors = { red, green, blue, red, &#8230;}</p>
<p>Step 2 : create method which take color object pointers and return a combination for selected color objects</p>
<p>magenta = combinations(colors, indexes), where indexes contain index of red and blue indexes = {2, 3}</p>
<p>Step 3 : create indexes using Permutation methods.</p>
<ul>
<li>Creating unique distribution of samples across multiple threads (p-threads) , .e. Dividing the work across the threads such that none of the thread do the same work.</li>
</ul>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<pre class="brush: cpp; title: ;">

   int n;//  set of object
   int r; //  subset of chosen object

   \* for each threads do *\
   {
         pid = get_pthread_id()
         p = get_total_pthread()
         perm =  createPermutation(n , r)
         startindex = pid * (n / p)
         PermuteAfterNiteration (perm, startindex)

         /*
           use nextpermutation method which return unique index
           Performed required operations
         */

         freepermutation()
    }
</pre>
<p> <strong>Code : </strong></p>
<pre class="brush: cpp; title: ;">

#include &lt;stdio.h&gt;;
#include &lt;stdlib.h&gt;;
#include &lt;string.h&gt;;

struct Permutation { 

  /* To Store current Combinations */
	int *A_i ; 

   /* To Store previous Combinations */
  int *A_i_1 ; 

  /* Total Number of Objects ( i.e. N), Object to Choose (i.e. R) */
	int n, r ; 

  /* To Store total possible Combinations */
	long int noOfPermutationCompleted ;
}; 

/* Create N-Choose-R chooser */
struct Permutation *createPermutation ( int n , int r ){ 

	int i ;
	struct Permutation * toreturn = ( struct Permutation *) malloc( sizeof ( struct Permutation ));
	toreturn -&gt; n = n ;
	toreturn -&gt; r = r ;
	toreturn -&gt; noOfPermutationCompleted = 0 ;
	toreturn -&gt; A_i = malloc ( r * sizeof ( int ));
	toreturn -&gt; A_i_1 = malloc ( r * sizeof ( int ));
	for ( i = 0 ; i &lt; r ; i ++){
		toreturn -&gt; A_i [ i ] = 0 ;
		toreturn -&gt; A_i_1 [ i ] = 0 ;
	}
	return toreturn ;
} 

/* Return next array of object indexs */
int * nextPermutation ( struct Permutation * Per ){ 

	int j ;
	size_t bytes = ( sizeof ( int ) * Per -&gt; r - 1 ); 

	/* Per -&gt; A_i = Per -&gt; A_i_1; */
	memcpy ( Per -&gt; A_i_1 , Per -&gt; A_i , bytes ); 

	for ( j = ( Per -&gt; r - 1 ) ; j &gt;;= 0 ; j --){
		Per -&gt; A_i [ j ] += 1 ;
		if ( Per -&gt; A_i [ j ] &lt; Per -&gt; n )
			break ;
		else
			Per -&gt; A_i [ j ] = 0 ;
	} 

	/* Per -&gt; A_i = Per -&gt; A_i_1; */
	memcpy ( Per -&gt; A_i_1 , Per -&gt; A_i , bytes ); 

	Per -&gt; noOfPermutationCompleted += 1 ; 

	return Per -&gt; A_i ;
} 

/* It initialise N-Choose-R chooser to start after Nth Iteration */
void PermuteAfterNiteration ( struct Permutation * Per , long int Niteration ){ 

	 while ( Niteration != 0 ){
	 	nextPermutation ( Per );
	 	Niteration --;
	 }
} 

/* Return total Permutation, for N-choose-R chooser */
long int TotalPermutations ( struct Permutation * Per ){ 

	long int ret = 1 ;
	int i = 0 ; 

	for ( i = 0 ; i &lt; Per -&gt; r ; i ++)
		ret *= Per -&gt; n ; 

	return ret ;
} 

/* Free Memory */
void free_Permutation ( struct Permutation * Per ){ 

	free ( Per -&gt; A_i );
	free ( Per -&gt; A_i_1 );
	free ( Per );
}
/*
Demo Program

int main(){

	int j, *permute, N = 4, R = 2, Nth = 10;
	long int tot, i;

	struct Permutation *Per = createPermutation(N, R);

	tot = TotalPermutations(Per);

	printf(&quot;Total Permutation %ld \n\n&quot;, tot);
	printf(&quot;List all Permutations \n&quot;);
	for(i = 0 ; i &lt; tot ; i++){
		permute = nextPermutation(Per);
		printf(&quot;permutation %ld : &quot;,i + 1);
		for(j = 0 ; j &lt; R ; j++)
			printf(&quot;%d &quot;, permute[j]);
		printf(&quot;\n&quot;);
	}

	printf(&quot;\nAfer Using nth Iterator \n&quot;);
	PermuteAfterNiteration(Per, Nth);
  	printf(&quot;List all Permutations \n&quot;);
	for(i = 0 ; i &lt; tot - Nth ; i++){
		permute = nextPermutation(Per);
		printf(&quot;permutation %ld : &quot;,i + 1);
		for(j = 0 ; j &lt; R ; j++)
			printf(&quot;%d &quot;, permute[j]);
		printf(&quot;\n&quot;);
	}

	printf(&quot;Free Memory \n&quot;);
  	free_Permutation(Per);

	return 0;
}

Output :
  Total Permutation 16

  List all Permutations
  permutation 1 : 0 1
  permutation 2 : 0 2
  permutation 3 : 0 3
  permutation 4 : 1 0
  permutation 5 : 1 1
  permutation 6 : 1 2
  permutation 7 : 1 3
  permutation 8 : 2 0
  permutation 9 : 2 1
  permutation 10 : 2 2
  permutation 11 : 2 3
  permutation 12 : 3 0
  permutation 13 : 3 1
  permutation 14 : 3 2
  permutation 15 : 3 3
  permutation 16 : 0 0

  Afer Using nth Iterator
  List all Permutations
  permutation 1 : 2 3
  permutation 2 : 3 0
  permutation 3 : 3 1
  permutation 4 : 3 2
  permutation 5 : 3 3
  permutation 6 : 0 0

  Free Memory

*/ 
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2012/01/n-choose-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PubSub  Chat Using HTML5 Web Socket and em-websocket</title>
		<link>http://railstech.com/2011/12/pubsub-chat-using-html5-web-socket-and-em-websocket/</link>
		<comments>http://railstech.com/2011/12/pubsub-chat-using-html5-web-socket-and-em-websocket/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 02:10:29 +0000</pubDate>
		<dc:creator>Amar Daxini</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[eventmachine]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5-websocket]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[ruby chat]]></category>
		<category><![CDATA[socketio]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=774</guid>
		<description><![CDATA[It’s 5 in the morning and i am tired and its a possibility that i may be writing a long post, Last night i asked my self what is it that i have learned in the las t couple of months and out of nothingness i rememberd a VIDEO CHAT APPLICATION which my friend Viren [...]]]></description>
			<content:encoded><![CDATA[<p>It’s 5 in the morning and i am tired and its a possibility that i may be writing a long post, Last night i asked my self what is it that i have learned in the las t couple of months and out of nothingness i rememberd a <strong>VIDEO CHAT APPLICATION</strong> which my friend Viren and I worked on. I thought it might be useful for some of you.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 53px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">When we started, we didnt have enough knowledge where to begin with and had time constraints.We came across solutions like Red5,ErlyVideo.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 53px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now out problem was that out client had to be in flash or java applet, we we tried lots of things and the mother solutions of all problems was yet released, HTML5.</div>
<p>When we started, we didnt have enough knowledge where to begin with and had time constraints to meet. We came across solutions like Red5,ErlyVideo. Now our problem was that our client had to be in flash or java applet. So we tried working around many solutions. HTML5 was months away from its release.</p>
<p>we are looking into other HTML5 Features and we get awesome <strong>HTML5 web socket</strong> support so we thought let&#8217;s get some hands on it.</p>
<p>So we think let&#8217;s build<strong> pubsub chat application</strong>, after Googling we got couple of solution.but we want to build own i have choice between <a title="Nodejs -Railstech" href="http://nodejs.org/" target="_blank">nodejs</a> and <a title="EventMacine - EM - Railstech" href="http://rubyeventmachine.com/" target="_blank">EventMachine</a>.we have choose <strong>EM</strong> to quick start.</p>
<p>So we are ready,<br />
We go through <strong>HTML5 Web Socket AP</strong>I,and tutorial so basically <strong>it&#8217;s  a technique for two-way communication over one (TCP) socket, a type of PUSH technology.<br />
</strong> So it has mainly three events</p>
<ol>
<li><strong> onopen</strong>: When a socket has opened</li>
<li><strong> onmessage</strong>: When a message has been received</li>
<li><strong> onclose</strong>: When a socket has been closed</li>
</ol>
<p>More info regarding HTML5 Web Socket you can visit <a href="http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/">Nettuts</a>, <a href="http://html5demos.com/web-socket">HTML5Demo</a>.<br />
<strong> lya Grigorik</strong> has written awesome <a href="https://github.com/igrigorik/em-websocket">library</a>,and <a href="http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/">article</a> on it so i don&#8217;t go in much detail.</p>
<p>Steps we are following</p>
<ul>
<li>When <strong>User is connected</strong> <strong>onopen </strong>event is fired and  we create one <strong>default connection</strong> and add to pool of channels,it&#8217;s used for mainly <strong>notification</strong> purpose</li>
<p><span id="more-774"></span></p>
<li>When <strong>User A</strong> wants to chat with other <strong>User B</strong> we pass <strong>user id</strong> of both user and we create <strong>channel</strong> on that.<br />
So basically <strong>User A </strong>is <strong>subscribe</strong> to <strong>individual channel</strong> for simplicity i have taken user id for creating <strong>uniq channel</strong>.I can use session or other stuff currently i am not worry about security.</li>
<li> When <strong>User A</strong> start <strong>sending messages</strong> to <strong>User B,</strong>it fires <strong>onmessage </strong>event,it first populate into <strong><strong>EM Queue</strong></strong>,right now i don&#8217;t want to use any dependency,this can be done with easily with <strong>redis</strong> also.</li>
<li><strong> After </strong>sending messages <strong>User B</strong> will receive notification via <strong>default channel</strong> which is created when it&#8217;s connected.</li>
<li>Now <strong>User B i</strong>s wants to <strong>chat</strong> with <strong>User A</strong> it check whether channel is present or not if not it creates channel and <strong>subscribe</strong> onto it</li>
<li>If channel is present then whatever <strong>messages in en queue</strong> it will be <strong>pop out</strong> and display messages.</li>
<li>If suppose anyone <strong><strong>disconnect</strong></strong> from then <strong><strong>onclose</strong></strong> event is fire it checks whether <strong>default channel</strong> is present  then <strong>unsubscribe from channel</strong> and <strong>remove socket from channel</strong>,and <strong>decrease uid</strong> <strong>by one</strong></li>
<li> If any  <strong>channel </strong>is present then <strong>unsubscribe</strong> from channel and <strong>remove socket from channel</strong>,and decrease <strong>uid by one.</strong></li>
<li>Now user is <strong>again connected </strong>it will just <strong>subscribe</strong> channel.</li>
</ul>
<p>Now I haven&#8217;t written code for removing dead channel it will be done easily.Code or Idea is may or may not  fully applicable,there are various solutions are available and respective opensource library is also present.</p>
<p>Now same concept can be easily applicabale <a title="Redis PubSub - Railstech" href="http://redis.io/topics/pubsub" target="_blank">redis pubsub</a> also.</p>
<p>Code is present on <a title="Em WebSocket Chat Demo - Railstech" href="https://github.com/amardaxini/em-websocket-chat-demo" target="_blank">github</a>,although code is not so furnish but  you can checkout and have some fun</p>
<p>Please feel free to share and comment on it.</p>
<p>At last i am awake it&#8217;s around 6.30 o&#8217;clock <img src='http://railstech.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/12/pubsub-chat-using-html5-web-socket-and-em-websocket/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing daemon using daemon-kit</title>
		<link>http://railstech.com/2011/12/managing-daemon-using-daemonkit/</link>
		<comments>http://railstech.com/2011/12/managing-daemon-using-daemonkit/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 06:30:57 +0000</pubDate>
		<dc:creator>Kelvin Kunjukutty</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[bundler]]></category>
		<category><![CDATA[Daemon]]></category>
		<category><![CDATA[DaemonKit]]></category>
		<category><![CDATA[Ruby-daemon]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=584</guid>
		<description><![CDATA[Hey all,
In Previous article i have given short bio of ruby daemon using daemon kit.
In this post i am writing about how to manage daemon,how to start/stop a daemon from any directory.
We can start a daemon using following way.
Go to the directory and give the command.
./bin/mydaemon
or
./bin/mydaemon start
(You know the difference right; To run it in [...]]]></description>
			<content:encoded><![CDATA[<p>Hey all,<br />
In<a title="Understanding Daemon Kit - Railstech" href="http://railstech.com/2011/12/understanding-daemonkit/" target="_blank"> Previous article</a> i have given short bio of ruby daemon using daemon kit.<br />
In this post i am writing about how to manage daemon,how to start/stop a daemon from any directory.</p>
<p>We can <strong>start a daemon</strong> using following way.<br />
Go to the directory and give the command.</p>
<pre class="brush: ruby; title: ;">./bin/mydaemon</pre>
<p>or</p>
<pre class="brush: ruby; title: ;">./bin/mydaemon start</pre>
<p>(You know the difference right; To run it in foreground or background respectively.)</p>
<p>But consider this possibility, what if you didn&#8217;t want to go to the directory where the daemon is located and start it.</p>
<pre class="brush: ruby; title: ;">/home/mysystem/mydaemon/bin/mydaemon</pre>
<p>or</p>
<pre class="brush: ruby; title: ;">/home/mysystem/mydaemon/bin/mydaemon start</pre>
<p>Now you might be thinking why you will want to do that, let me say such conditions does arise (when you want <a title="Monitoring Daemon Using Monit - Railstech" href="http://mmonit.com/monit/" target="_blank">monit</a> to watch over it and restart it when it goes down.)</p>
<p>Usually this would work with ease if you are not using a bundler, however if you are using it then this would be something that you would get to see.</p>
<p><span id="more-584"></span></p>
<pre class="brush: ruby; title: ;">/usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/shared_helpers.rb:22:in `default_gemfile': Could not locate Gemfile (Bundler::GemfileNotFound)</pre>
<p>This happens because <strong>bundler looks</strong> for the<strong> Gemfile in the directory</strong> you are in and the Gemfile is not there.<br />
So what do you do to <strong>overcome</strong> it ?<br />
You tell the <strong>daemon</strong> where to <strong> look for the gemfile</strong><br />
and how do you do it?</p>
<p>In you daemon place the following piece of <strong>code in bin/mydaemon</strong> at the very <strong>beginning</strong>.</p>
<pre class="brush: ruby; title: ;">ENV['BUNDLE_GEMFILE'] = &quot;Path/to/your/daemon/Gemfile&quot;</pre>
<p>Now this is all fine and you have got your daemon working&#8230;. BUT what if others would also want to work with your daemon in there system.</p>
<p>This won&#8217;t work as the location given previously would not be the same as theirs.<br />
Don&#8217;t worry I have a solution for that too.</p>
<p>Instead of</p>
<pre class="brush: ruby; title: ;">ENV['BUNDLE_GEMFILE'] = &quot;Path/to/your/daemon/Gemfile&quot;</pre>
<p>put it as</p>
<pre class="brush: ruby; title: ;">ENV['BUNDLE_GEMFILE'] = File.join(File.dirname(__FILE__),'..','Gemfile')</pre>
<p>This  would help in anybody <strong>starting the daemon from any directory</strong> if required or starting the daemon by going to the directory and running it.</p>
<p>And thus you have your <strong>daemon running from your daemon root</strong> and other<strong> different location.</strong><br />
Hope this helps you at some point of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/12/managing-daemon-using-daemonkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Daemonkit</title>
		<link>http://railstech.com/2011/12/understanding-daemonkit/</link>
		<comments>http://railstech.com/2011/12/understanding-daemonkit/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 08:28:51 +0000</pubDate>
		<dc:creator>Kelvin Kunjukutty</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[DaemonKit]]></category>
		<category><![CDATA[Ruby-daemon]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=695</guid>
		<description><![CDATA[Hey all,
Many of us knows what daemons are in linux. These are processes or programs that run in background with little or no user intervention.
Now we can write daemons in ruby too. And for doing so we can use Daemon-Kit(https://github.com/kennethkalmer/daemon-kit).
Now the daemon can do the task continuously or do it at particular interval of time [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Hey all,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Many of us knows what daemons are in linux. These are processes or programs that run in background with little or no user intervention.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now we can write daemons in ruby too. And for doing so we can use Daemon-Kit(https://github.com/kennethkalmer/daemon-kit).</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now the daemon can do the task continuously or do it at particular interval of time or do it depending on a particular event.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Daemon-Kit provides a skeleton to write these different types of daemons with the help of differnt types of generators.(XMPP bot,AMQP client,Nanite agent,Cron-style,ruote remote participants). Of these XMPP and AMQP are event based and amqp is specifically based on queuing system, and Nanite agent gives a nanite structured daemon while Cron-style is used to write daemons that perform certain task at particular interval amount of time.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">First install the gem daemon-kit</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">gem install daemon-kit</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">For viewing the help of daemonkit you can give the command,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">daemon-kit -h</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now to build a simple daemon which would use a default generator you can use,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">daemon-kit mydaemon</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">For building a daemon with a generator you can use,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">daemon-kit mydaemon -i amqp&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;daemon-kit mydaemon -i cron</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now for starting the daemon first you go into the daemon directory and then give the command,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">cd mydaemon&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;./bin/mydaemon</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">which would start the daemon in foreground which is used for testing if everhting is working.The daemon would die as you close the terminal.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">To purely daemonize it you can use</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">./bin/mydaemon start</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">This would start the daemon in daemonized form and would continue to run in background till any untoward exception occurs.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">To stop the daemon running in background you can use the commnad</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">./bin/mydaemon stop</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now the basic tree structure of a daemon would be something similar to this.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;my_cron_daemon&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── bin&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── my_cron_daemon&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── config&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── arguments.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── boot.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── environment.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── environments&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   │   ├── development.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   │   ├── production.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   │   └── test.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── post-daemonize&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   │   └── readme&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── pre-daemonize&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│       ├── cron.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│       ├── readme&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│       └── safely.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── Gemfile&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── lib&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── my_cron_daemon.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── libexec&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── my_cron_daemon-daemon.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── log&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── Rakefile&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── README&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── script&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── console&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── destroy&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── generate&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── spec&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── my_cron_daemon_spec.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   ├── spec_helper.rb&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── spec.opts&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── tasks&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;│   └── rspec.rake&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;├── tmp&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;└── vendor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">The pre-daemonize folder consists of all the files that are required before the daemon starts. The cron.rb can be used to require all the gems that are needed for the daemon.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">The my_cron_daemon.rb in lib folder is automatically loaded when the daemon starts. Additonal ruby files can be created in the lib folder and can be required from the cron.rb in pre-daemonize for utilization.This can be done as,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">excluded_files = [File.join(DaemonKit.root, 'lib', &quot;my_cron_daemon.rb&quot;)]&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;(Dir.glob(File.join(DaemonKit.root, &quot;lib&quot;, &quot;*.rb&quot;)) - excluded_files.flatten).each{|f| require f}</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">This is done because my_cron_daemon.rb in lib is automatically loaded and requiring it multiple times might cause issues.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">The post-daemonize folder is used to place those files that needs to be loaded after the daemon starts.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Similarly a yml file(e.g config.yml) which holds all the configuration setting can be created in the config and loaded into the daemon using,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">CONFIG = DaemonKit::Config.load(&quot;config.yml&quot;)</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Here CONFIG can be used anywhere in the daemon later on to access the required configuration.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now you can also add ruby files that connect to a particular database and query data by putting those in lib. These files would have a structure similar to the ruby files in models of rails application.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">As for the database connection you can put these pieces of code in cron.rb in pre-daemonize which establishes the databse connection,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">ActiveRecord::Base.configurations = CONFIG[:database_configuration]&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ActiveRecord::Base.establish_connection(CONFIG[:database_configuration])</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">where &#8220;database_configuration&#8221; is placed in the config.yml</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now we can also have the daemon as a request-response serving program similar to a rack-server, This can be done by using eventmachine.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Daemons are a difficult to debug. Because they can just cause  your program to fail when a request comes in. Normally we write a code in a normal Ruby program and run it to test all the functionallity and then put it within the daemon. This helps in ensuring that your logic is proper. For further debugging I use a lot of puts statements when coding later switching to DaemonKit.logger.info or .debug to dump the statements to the log.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: ruby; title: ;">DaemonKit.logger.info &quot;I am the DaemonKit logger info&quot;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Here is a link to a basic daemon which is based on default,cron generator.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">https://github.com/kelvink/My-Basic-Daemon</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">https://github.com/kelvink/My-Cron-Daemon</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Also here is a link for the daemon to work as a rack server using evenmachine.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">https://github.com/kelvink/My-EM-Daemon</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Thus we can have a ruby daemon which perform specific task as required and when required.</div>
<p>Hey all,</p>
<p>Many of us knows what daemons are in linux. These are processes or programs that run in background with little or no user intervention.</p>
<p>Now we can write <strong>daemons in ruby</strong> too. And for doing so we can use <a title="Daemonkit " href="(https://github.com/kennethkalmer/daemon-kit" target="_blank">Daemon-Kit</a>.</p>
<p>Now the daemon can do the task continuously or do it at particular interval of time or do it depending on a particular event.</p>
<p>Daemon-Kit provides a skeleton to write these different types of daemons with the help of differnt types of generators.(<strong>XMPP</strong> bot,<strong>AMQP </strong>client,<strong>Nanite agent,Cron-styl</strong>e,ruote remote participants). Of these <a title="XMPP - Messaging Protocol" href="http://en.wikipedia.org/wiki/Extensible_Messaging_and_Presence_Protocol" target="_blank">XMPP</a> and <a title="AMQP - Advanced Message Queuing Protocol" href="http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol" target="_blank">AMQP</a> are event based and amqp is specifically based on queuing system, and Nanite agent gives a nanite structured daemon while Cron-style is used to write daemons that perform certain task at particular interval amount of time.</p>
<p>First install the <strong>gem daemon-kit</strong></p>
<p><strong><span id="more-695"></span></strong></p>
<pre class="brush: ruby; title: ;">gem install daemon-kit</pre>
<p>For viewing the help of daemonkit you can give the command,</p>
<pre class="brush: ruby; title: ;">daemon-kit -h</pre>
<p>Now to build a simple daemon which would use a <strong>default generator</strong> you can use,</p>
<pre class="brush: ruby; title: ;">daemon-kit mydaemon</pre>
<p>For building a daemon with a generator you can use,</p>
<pre class="brush: ruby; title: ;">daemon-kit mydaemon -i amqp

daemon-kit mydaemon -i cron</pre>
<p>Now for starting the daemon first you go into the daemon directory and then give the command,</p>
<pre class="brush: ruby; title: ;">cd mydaemon

./bin/mydaemon</pre>
<p>which would start the daemon in foreground which is used for testing if everything is working.The daemon would die as you close the terminal.</p>
<p>To purely <strong>daemonize</strong> it you can use</p>
<pre class="brush: ruby; title: ;">./bin/mydaemon start</pre>
<p>This would <strong>start the daemon</strong> in daemonized form and would continue to run in background till any untoward exception occurs.</p>
<p>To stop the <strong>daemon</strong> running in <strong>background</strong> you can use the command</p>
<pre class="brush: ruby; title: ;">./bin/mydaemon stop</pre>
<p>Now the<strong> basic tree structure of a daemon</strong> would be something similar to this.</p>
<pre class="brush: ruby; title: ;">

my_cron_daemon

├── bin

│   └── my_cron_daemon

├── config

│   ├── arguments.rb

│   ├── boot.rb

│   ├── environment.rb

│   ├── environments

│   │   ├── development.rb

│   │   ├── production.rb

│   │   └── test.rb

│   ├── post-daemonize

│   │   └── readme

│   └── pre-daemonize

│       ├── cron.rb

│       ├── readme

│       └── safely.rb

├── Gemfile

├── lib

│   └── my_cron_daemon.rb

├── libexec

│   └── my_cron_daemon-daemon.rb

├── log

├── Rakefile

├── README

├── script

│   ├── console

│   ├── destroy

│   └── generate

├── spec

│   ├── my_cron_daemon_spec.rb

│   ├── spec_helper.rb

│   └── spec.opts

├── tasks

│   └── rspec.rake

├── tmp

└── vendor
</pre>
<p>The pre-daemonize folder consists of all the files that are required before the daemon starts. The<strong> cron.rb</strong> can be used to <strong>require all the gems</strong> that are needed for the daemon.</p>
<p>The <strong>my_cron_daemon.rb</strong> in lib folder is automatically <strong>loaded</strong> when the<strong> daemon starts</strong>. Additonal ruby files can be created in the lib folder and can be required from the cron.rb in pre-daemonize for utilization.This can be done as,</p>
<pre class="brush: ruby; title: ;">excluded_files = [File.join(DaemonKit.root, 'lib', &quot;my_cron_daemon.rb&quot;)]

(Dir.glob(File.join(DaemonKit.root, &quot;lib&quot;, &quot;*.rb&quot;)) - excluded_files.flatten).each{|f| require f}</pre>
<p>This is done because my_cron_daemon.rb in lib is automatically loaded and requiring it multiple times might cause issues.</p>
<p>The post-daemonize folder is used to place those files that needs to be loaded after the daemon starts.</p>
<p>Similarly a yml file(e.g<strong> config.yml</strong>) which holds all the configuration setting can be created in the config and loaded into the daemon using,</p>
<pre class="brush: ruby; title: ;">CONFIG = DaemonKit::Config.load(&quot;config.yml&quot;)</pre>
<p>Here CONFIG can be used anywhere in the daemon later on to access the required configuration.</p>
<p>Now you can also add ruby files that connect to a particular database and query data by putting those in lib. These files would have a structure similar to the ruby files in models of rails application.</p>
<p>As for the database connection you can put these pieces of code in cron.rb in pre-daemonize which establishes the databse connection,</p>
<pre class="brush: ruby; title: ;">ActiveRecord::Base.configurations = CONFIG[:database_configuration]

ActiveRecord::Base.establish_connection(CONFIG[:database_configuration])</pre>
<p>where &#8220;database_configuration&#8221; is placed in the config.yml</p>
<p>Now we can also have the daemon as a request-response serving program similar to a rack-server, This can be done by using eventmachine.</p>
<p>Daemons are a difficult to debug. Because they can just cause  your program to fail when a request comes in. Normally we write a code in a normal Ruby program and run it to test all the functionallity and then put it within the daemon. This helps in ensuring that your logic is proper. For further debugging I use a lot of puts statements when coding later switching to DaemonKit.logger.info or .debug to dump the statements to the log.</p>
<pre class="brush: ruby; title: ;">DaemonKit.logger.info &quot;I am the DaemonKit logger info&quot;</pre>
<p>Here are links to  basic daemon which is based on default,<strong>cron</strong> generator and rack server using <strong>eventmachine</strong>.</p>
<p><a href="https://github.com/kelvink/My-Basic-Daemon" target="_blank">https://github.com/kelvink/My-Basic-Daemon</a>,</p>
<p><a href="https://github.com/kelvink/My-Cron-Daemon" target="_blank">https://github.com/kelvink/My-Cron-Daemon</a>,</p>
<p><a href="https://github.com/kelvink/My-EM-Daemon" target="_blank">https://github.com/kelvink/My-EM-Daemo</a></p>
<p>Thus we can have a ruby daemon which perform specific task as required and when required.</p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/12/understanding-daemonkit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>acts_as_flying_saucer on Rails 3.1 and Heroku</title>
		<link>http://railstech.com/2011/10/acts_as_flying_saucer-on-rails-3-1-and-heroku/</link>
		<comments>http://railstech.com/2011/10/acts_as_flying_saucer-on-rails-3-1-and-heroku/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 10:03:36 +0000</pubDate>
		<dc:creator>Amar Daxini</dc:creator>
				<category><![CDATA[Asset Pipeline]]></category>
		<category><![CDATA[Rails 3.1.0]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3.0]]></category>
		<category><![CDATA[acts_as_flying_saucer]]></category>
		<category><![CDATA[heroku]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=567</guid>
		<description><![CDATA[Just quick post how to use acts_as_flying_saucer with rails 3.1 and Heroku.
I was testing acts_as_flying_saucer with rails 3.1.It is working fine so far.
but when i have added external style sheet it was hanging on dev mode.
and later on i figure out it is external style sheet causing an issue.
So i precomplied css and then try [...]]]></description>
			<content:encoded><![CDATA[<p>Just quick post how to use <strong>acts_as_flying_saucer</strong> with <strong>rails 3.1</strong> and H<strong>eroku</strong>.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">I was testing acts_as_flying_saucer with rails 3.1.It is working fine so far.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">but when i have added external style sheet it was hanging on dev mode.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">and later on i figure out it is external style sheet causing an issue.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So i precomplied css and then try to generate pdf it is working.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now it is time to test on heroku.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">I am using cedar stack and ruby 1.9.2 after precomplied step pdf.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">By default pdf is generated on system /tmp directory.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In heroku we can set application tmp path to generate pdf i.e ./tmp</div>
<p>I was testing<strong> acts_as_flying_saucer</strong> with <strong>rails 3.1</strong>.It is working fine so far,<span style="direction: ltr; ">but when i have added external style sheet it was hanging on dev mode rails is hanging</span></p>
<p><span style="direction: ltr; "> </span><span style="direction: ltr; ">After litte bit of googling  i figure out it is external style sheet causing an issue because of <strong>asset pipeline and thread issue for serving static file</strong> mainly in dev mode.</span></p>
<p><span style="direction: ltr; "> </span><span style="direction: ltr; ">So i simply<strong> pre complied </strong>css or we can <strong>manually (actual full path) or manipulate asset path</strong> .after this PDF is generated properly.</span></p>
<p><span style="direction: ltr; ">Now it is time to test on <strong>Heroku</strong>.</span><span style="direction: ltr; ">I am using </span><strong>cedar stack</strong><span style="direction: ltr; "> and ruby 1.9.2 <strong>after pre complied </strong>step. </span></p>
<p><span style="direction: ltr; ">Now <strong>point tmp directory to application tmp directory</strong>.</span><span style="direction: ltr;"> </span></p>
<p><span style="direction: ltr; "> </span></p>
<pre class="brush: ruby; title: ;">ActsAsFlyingSaucer::Config.options = {:tmp_path =&gt; &quot;./tmp&quot;}</pre>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/10/acts_as_flying_saucer-on-rails-3-1-and-heroku/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Walk through of new features of acts_as_flying_saucer XHTML to PDF</title>
		<link>http://railstech.com/2011/10/walk-through-of-new-features-of-acts_as_flying_saucer-xhtml-to-pdf/</link>
		<comments>http://railstech.com/2011/10/walk-through-of-new-features-of-acts_as_flying_saucer-xhtml-to-pdf/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 09:24:37 +0000</pubDate>
		<dc:creator>Amar Daxini</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3.0]]></category>
		<category><![CDATA[acts_as_flying_saucer]]></category>
		<category><![CDATA[sinatra]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=550</guid>
		<description><![CDATA[Generate PDF using acts_as_flying_saucer.It also support for clean up html before generating pdf,pdf can be generated on remote and do further processing.]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">It&#8217;s about more than 6 months i haven&#8217;t written any post.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Why ?</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Recently join new organiztion,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Can&#8217;t get time because we are working almost more than 10 to 12 hr day with messy code.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So we are spending more time to refactoring and optimising code,and scaling application also.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now i started making my time to writing.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In my current organization we are generating various Pdf report.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">We are using prawn but now we are switching to  acts_as_flying_saucer.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So there are couple of challenges i have faced</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">1) XHTML code is not proper</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">2) Special character in html</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Solution</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Cleanup Html code</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">I am using tidy library clean up html code.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">By passing :clean=&gt;true it will cleanup html befor generating pdf.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">3) Send PDF as email</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Solution</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Generate PDF on server and do some processing like attached to mail.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">:send_attachment =&gt; true it will generate pdf and return path of pdf file then do some processing.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So couple of new feature is added to acts_as_flying_saucer</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">1) Clean up  html by passing :clean=&gt;true</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">2) Generate pdf on server (locally) by passing :send_attachment=&gt;true</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">3) acts_as_flying_saucer is also working with rails 3.1 (precomplie asset) and heroku.</div>
<p>It&#8217;s about more than 6 months i haven&#8217;t written any post <strong>Why ? </strong></p>
<p><strong>because </strong><strong>r</strong><span style="direction: ltr; ">ecently join new organization, and c</span><span style="direction: ltr; ">an&#8217;t get much more time , we are working almost more than 10 to 12 hr day with <strong>messy code</strong>.</span><span style="direction: ltr; ">So we are spending more time to  <strong>re</strong><strong>factoring and optimizing code,and scaling</strong> application.</span><span style="font-family: monospace; font-size: 12px; line-height: 22px; text-align: left; direction: ltr;"><span style="font-family: 'Lucida Grande', Verdana, 'Bitstream Vera Sans', Arial, sans-serif;"> <img src='http://railstech.com/wp-includes/images/smilies/icon_cry.gif' alt=':cry:' class='wp-smiley' /> </span></span></p>
<p><span style="direction: ltr; ">In my current organization we are generating various <strong>PDF</strong> report.</span><span style="direction: ltr; ">We are using prawn but now we are switching to </span><a style="direction: ltr; " title="XHTML to PDF railstech" href="https://github.com/amardaxini/acts_as_flying_saucer" target="_blank">acts_as_flying_saucer</a><span style="direction: ltr; ">.</span></p>
<p>So there are couple of challenges i have faced</p>
<ol>
<li><span style="direction: ltr; "><strong>XHTML code is not proper</strong></span></li>
<li><span style="direction: ltr; "><strong> Special character in html</strong></span></li>
</ol>
<p><span style="direction: ltr; "> </span></p>
<p><span style="direction: ltr; "> <strong> Solution: </strong></span><strong><span style="direction: ltr; "> </span><span style="direction: ltr; ">Cleanup Html code </span></strong></p>
<p><span style="direction: ltr; "><span id="more-550"></span>I am using tidy library clean up html code.</span><span style="direction: ltr; "> </span><span style="direction: ltr; ">By passing <strong>:</strong></span><span style="background-color: #f8f8f8; font-family: helvetica, arial, freesans, clean, sans-serif; line-height: 20px; direction: ltr; "><strong>clean =&gt; true</strong></span><span style="direction: ltr; "> it will cleanup html before generating PDF.</span></p>
<p><span style="direction: ltr; "><span style="direction: ltr; "> 3.<strong> Send PDF as email or do some processing</strong></span></span><span style="direction: ltr; "> </span></p>
<p><span style="direction: ltr; "> <strong>Solution:</strong> </span><span style="direction: ltr; "><strong>Generate PDF on server </strong>and do some processing like attached to mail.</span></p>
<p><span style="font-family: helvetica, arial, freesans, clean, sans-serif; line-height: 20px; background-color: #f8f8f8; "><strong>:send_to_client =&gt;false</strong></span> It will generate pdf and return path of pdf file then do some processing.</p>
<p>4. <strong>Generate PDF</strong> from <strong>String,File,URL</strong></p>
<p><span style="font-family: helvetica, arial, freesans, clean, sans-serif; line-height: 20px; background-color: #f8f8f8; "><strong>:url &#8211; URl/String/File</strong> Path </span><span style="font-family: helvetica, arial, freesans, clean, sans-serif;"><span style="line-height: 20px;">Now by passing <strong>URL</strong> or <strong>String</strong> or <strong>File Path</strong> <strong>PDF</strong> can be generated.</span></span></p>
<p><span style="direction: ltr; ">So couple of new feature is added to <strong>acts_as_flying_saucer</strong> is as follows</span></p>
<p>1)  Clean up  html by passing <span style="background-color: #f8f8f8; font-family: helvetica, arial, freesans, clean, sans-serif; line-height: 20px; direction: ltr; "><strong>:clean=&gt;true</strong></span></p>
<p>2)  Generate pdf on server (locally) by passing <strong><span style="background-color: #f8f8f8; font-family: helvetica, arial, freesans, clean, sans-serif; line-height: 20px; direction: ltr; ">:send_to_client=&gt; false</span><span style="background-color: #f8f8f8; font-family: helvetica, arial, freesans, clean, sans-serif; line-height: 20px; direction: ltr; "> </span></strong></p>
<p>3)  Fixed issue of non inline CSS issue.</p>
<p>4) Generate PDF from <strong>File,String,URL.</strong></p>
<p>5)  More info about acts_as_flying_saucer you can visit <strong><a title="XHTML to PDF acts_as_flying_saucer wiki" href="https://github.com/amardaxini/acts_as_flying_saucer/wiki" target="_blank">wiki</a></strong></p>
<p>6)  <strong>PDF</strong> can be generated from <strong>Rails,Sinatra,Ruby.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/10/walk-through-of-new-features-of-acts_as_flying_saucer-xhtml-to-pdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Pre-Processor In Rails 3.1</title>
		<link>http://railstech.com/2011/08/custom-pre-processor-in-rails-3-1/</link>
		<comments>http://railstech.com/2011/08/custom-pre-processor-in-rails-3-1/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 09:55:15 +0000</pubDate>
		<dc:creator>viren</dc:creator>
				<category><![CDATA[Asset Pipeline]]></category>
		<category><![CDATA[Mustache]]></category>
		<category><![CDATA[PreProcessor]]></category>
		<category><![CDATA[Rails 3.1.0]]></category>
		<category><![CDATA[Sprockets]]></category>
		<category><![CDATA[rails3.0]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Tilt]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=486</guid>
		<description><![CDATA[Well long break,Let me guess It over 3 month since I wrote my last post. I&#8217;ve been kind of  busy lately not manage to dedicate much time to thing that I do.
Rails 3.1.0 is been in picture for a quite a while a now and major feature that been included in Rails 3.1.0 is asset [...]]]></description>
			<content:encoded><![CDATA[<p>Well long break,Let me guess It over 3 month since I wrote my last post. I&#8217;ve been kind of  busy lately not manage to dedicate much time to thing that I do.</p>
<p>Rails 3.1.0 is been in picture for a quite a while a now and major feature that been included in Rails 3.1.0 is asset pipeline and pre-processor.</p>
<p>For those is who aren&#8217;t aware of Rails 3.1.0 asset pipeline and pre-processor there is a very good article on <a href="http://ryanbigg.com/guides/asset_pipeline.html">rails guide </a>and recently Ryan Bates introduce a <a href="http://railscasts.com/episodes/279-understanding-the-asset-pipeline?autoplay=true">screencast</a> on Rails 3.1.0 asset pipeline.</p>
<p>I&#8217;m not here to talk about  asset pipeline you can get it all if you just googled it.</p>
<p>So what I&#8217;m here upto then ? <img src='http://railstech.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Well ever since I was made aware of what <strong>asset pipeline  and pre-processor</strong> more importantly <strong>pre-processor</strong> ,it always trilled me of using a <strong>custom pre-processor</strong> .</p>
<p>As you all know that <a href="http://rubyonrails.org/">Rails 3.1.0.rc1</a> is bundle with <a href="http://jashkenas.github.com/coffee-script/">coffee-script</a> and <a href="http://sass-lang.com/">sass</a> support. In your asset folder you may find <strong>js</strong> and <strong>css</strong> file that look something like this &#8220;<strong>*.js.coffee</strong>&#8221; , &#8220;<strong>*.css.scss</strong>&#8220;. Now you can add a chain of pre-processor that just look like this &#8220;<strong>*.js.coffee.erb</strong>&#8221; , &#8220;<strong>*.css.scss.erb</strong>&#8221;</p>
<p>The post is all about chaining your <strong>own pre-processor</strong> just as above something like &#8220;<strong>*.js.coffee.mustache</strong>&#8221;</p>
<p><strong><a href="http://mustache.github.com/">Mustache</a></strong> (<a href="http://mustache.github.com/">Mustache</a> is taken just for example here you can use your own)  because I been in love with it ever since I explored it . So how to accomplish this .</p>
<p>Stay with me as I will show how ?</p>
<p>Firstly you need to be aware of <a href="https://github.com/sstephenson/sprockets">Sprockets</a> and <a href="https://github.com/rtomayko/tilt">Tilt</a> library</p>
<p>First the code</p>
<p><strong><span id="more-486"></span>config/initializers/mustache_template.rb</strong></p>
<pre class="brush: ruby; title: ;">
require &quot;mustache&quot;
require &quot;tilt&quot;

class MustacheTemplate &lt; Tilt::Template
  def prepare; end

  def evaluate(context, locals, &amp;block)
    @data = ::Mustache.render @data ,:mustache_variable =&gt; &quot;World&quot; ,:name =&gt; &quot;Viren Negi&quot;
  end

end

Rails.application.assets.register_preprocessor 'application/javascript', MustacheTemplate
</pre>
<p>That all it take to define a <strong>custom preprocessor</strong> Before I explain the above code let me show what exactly my preprocessed file look like .</p>
<p><strong>app/assets/javascripts/user.js.coffee.mustache<br />
</strong></p>
<pre class="brush: ruby; title: ;">
# Here is the custom mustache and coffee-script code

a =  &quot;Hello {{ mustache_variable }} from {{ name }}&quot;;

if a
  alert a
else
  alert &quot;Sorry LOL&quot;
  alert &quot;The co-processor is not working&quot;
</pre>
<p>well as you would have guess I received and alert prompt on UI stating <strong> &#8220;Hello World from Viren Negi&#8221;</strong></p>
<p>Well that quite neat <img src='http://railstech.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<p>Let me describe what happening under the hood.</p>
<p><strong>Rails.application.assets</strong> is <strong>Sprocket::Environment</strong> instance as you would guess <strong> register_preprocessor</strong> is <strong>Sprocket</strong> method to register the preproccessor .</p>
<p>a closer look at the <strong>Sprocket &#8221; register_preprocessor &#8220;</strong> method</p>
<p><strong>sprockets/lib/sprockets/processing.rb</strong></p>
<pre class="brush: ruby; title: ;">
 def register_preprocessor(mime_type, klass, &amp;block)
      expire_index!

      if block_given?
        name  = klass.to_s
        klass = Class.new(Processor) do
          @name      = name
          @processor = block
        end
      end

      @preprocessors[mime_type].push(klass)
    end
</pre>
<p>As you guess till now that the method except 2 argument and block<strong> (if given)</strong>.</p>
<p>For all those dump guys like me who still haven&#8217;t figure it out.<br />
What it basically does is &#8220;it push the class to mime_type&#8221; in our case (<strong>mime_type = &#8220;application/js&#8221; and klass = MustacheTemplate</strong>)</p>
<p>what it internally does when processing the template is some <strong> Tilt </strong>magic.</p>
<p>The ruby code</p>
<pre class="brush: ruby; title: ;">

def prepare ;   end

def evaluate(context,locals,&amp;block)

...

...

end
</pre>
<p>defined above are basically the <strong>Tilt</strong> method which is overridden in <strong>MustacheTemplate</strong> to process the template .</p>
<p>That it that how you add your own custom pre-processor .</p>
<p>Hope that it help you in same way or other.</p>
<p>Thanks</p>
<p>Viren Negi</p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/08/custom-pre-processor-in-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Passing arrays using Net-http in Ruby</title>
		<link>http://railstech.com/2011/08/passing-arrays-using-net-http-in-ruby/</link>
		<comments>http://railstech.com/2011/08/passing-arrays-using-net-http-in-ruby/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 06:04:55 +0000</pubDate>
		<dc:creator>Kelvin Kunjukutty</dc:creator>
				<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[net/http]]></category>
		<category><![CDATA[ruby-1.8.7]]></category>
		<category><![CDATA[ruby-1.9.2]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=459</guid>
		<description><![CDATA[Hi all,
We needed to use net/http of ruby for sending parameters between two modules.
We decided to use the get and the post request of http as follows:
 url = URI.parse(&#8221;http://SERVER:PORT&#8221;)
 http = Net::HTTP.new(url.host, url.port)
 request = Net::HTTP::Get.new(&#8221;/method_name?parameter=a&#8221;)
 response = http.request(request)
In the above get request the parameter &#8220;a&#8221; is taken to the &#8220;http://SERVER:PORT/method_name&#8221; and data related [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Hi all,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">We needed to use net/http of ruby for sending parameters between two modules.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">We decided to use the get and the post request of http as follows:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>url = URI.parse(&#8221;http://SERVER:PORT&#8221;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>http = Net::HTTP.new(url.host, url.port)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>request = Net::HTTP::Get.new(&#8221;/method_name?parameter=a&#8221;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>response = http.request(request)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In the above get request the parameter &#8220;a&#8221; is taken to the &#8220;http://SERVER:PORT/method_name&#8221; and data related to it is queried.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>url = URI.parse(&#8221;http://SERVER:PORT&#8221;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>http = Net::HTTP.new(url.host, url.port)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>request = Net::HTTP::Post.new(&#8221;/method_name&#8221;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>request.set_form_data({&#8221;parameter1&#8243; =&gt; &#8220;a&#8221;, &#8220;parameter2&#8243; =&gt; &#8220;b&#8221;, &#8220;parameter3&#8243; =&gt; &#8220;c&#8221; })</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>response = http.request(request)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In the above post request three parameters are taken parameter1, parameter2 and parameter3  with the value a,b and c respectively to the &#8220;http://SERVER:PORT/method_name&#8221; for posting.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">When you need to pass a array in the parameters for posting. you can specify it as:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">request.set_form_data({&#8221;parameter1&#8243; =&gt; &#8220;a&#8221;, &#8220;parameter2&#8243; =&gt; ["1", "2", "3"], &#8220;parameter3&#8243; =&gt; &#8220;c&#8221; })</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Here the parameter2 takes a array with value 1,2,3.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now the fun begins,</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">If you are using ruby 1.9.2 this works fine parameter2 is passed as a array.(parameter2 = ["1", "2", "3"])</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">However if you are uing ruby 1.8.7 this doesnot work, parameter2 appends all the values of the array to form  a string and passes it.(parameter2 = &#8220;123&#8243;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">You have to override the set_form_data method for it to work and pass parameter as a array.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">It can be done as below. You an place this in a file and let the file be included while loading the app.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">module Net</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>module HTTPHeader</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>def set_form_data(request, params, sep = &#8216;&amp;&#8217;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>request.body = params.map {|k,v|</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>if v.instance_of?(Array)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span> v.map {|e| &#8220;#{urlencode(k.to_s)}=#{urlencode(e.to_s)}&#8221;}.join(sep)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>else</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span> &#8220;#{urlencode(k.to_s)}=#{urlencode(v.to_s)}&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>end</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>}.join(sep)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>request.content_type = &#8216;application/x-www-form-urlencoded&#8217;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>end</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>alias form_data= set_form_data</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>def urlencode(str)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf(&#8217;%%%02x&#8217;, s[0]) }</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>end</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>end</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">end</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">And then use set_form data as follows:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">request.set_form_data(request, {&#8221;parameter1&#8243; =&gt; &#8220;a&#8221;, &#8220;parameter2&#8243; =&gt; ["1", "2", "3"], &#8220;parameter3&#8243; =&gt; &#8220;c&#8221; })</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Thus we can pass a array in parameters while use net/http of ruby 1.8.7.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">References : http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data</div>
<p>In one of our projects, we needed to use <strong>net/http</strong> of ruby for <strong>sending parameters</strong> between two  modules which resided on two different servers.</p>
<p>We decided to use the <strong>get</strong> and the <strong>post</strong> request of http as follows:</p>
<pre class="brush: ruby; title: ;">url = URI.parse(&quot;http://SERVER:PORT&quot;)

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Get.new(&quot;/method_name?parameter=a&quot;)

response = http.request(request) </pre>
<p>In the above get request the parameter &#8220;a&#8221; is send to the &#8220;<strong>http://SERVER:PORT/method_name</strong>&#8221; and data related to it is queried.</p>
<pre class="brush: ruby; title: ;">url = URI.parse(&quot;http://SERVER:PORT&quot;)

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new(&quot;/method_name&quot;)

request.set_form_data({&quot;parameter1&quot; =&gt; &quot;a&quot;, &quot;parameter2&quot; =&gt; &quot;b&quot;, &quot;parameter3&quot; =&gt; &quot;c&quot; })

response = http.request(request) </pre>
<p>In the above post request three parameters are taken <strong>parameter1, parameter2 and parameter3 </strong>with the value <strong>a,b and c</strong> respectively to the <strong>&#8220;http://SERVER:PORT/method_name</strong>&#8221; for posting.</p>
<p>When you need to <strong>pass a array</strong> in the parameters for posting. you can specify it as:</p>
<pre class="brush: ruby; title: ;">request.set_form_data({&quot;parameter1&quot; =&gt; &quot;a&quot;, &quot;parameter2&quot; =&gt; [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;], &quot;parameter3&quot; =&gt; &quot;c&quot; }) </pre>
<p>Here the <strong>parameter2 </strong>takes a array with value <strong>1,2,3</strong>.</p>
<p>Now the fun begins,</p>
<p><span id="more-459"></span></p>
<p>If you are using <strong>ruby 1.9.2</strong> this works fine <strong>parameter2</strong> is passed as a <strong>array.(parameter2 = ["1", "2", "3"])</strong></p>
<p>However if you are uing <strong>ruby 1.8.7</strong> this doesnot work, <strong>parameter2</strong> appends all the values of the array to form  a string and passes it<strong>.(parameter2 = &#8220;123&#8243;</strong>)</p>
<p>You have to <strong>override</strong> the <strong>set_form_data method</strong> for it to work and pass parameter as a array.</p>
<p>It can be done as below. You an place this in a file and let the file be included while loading the app.</p>
<pre class="brush: ruby; title: ;">module Net

module HTTPHeader

def set_form_data(request, params, sep = '&amp;')

request.body = params.map {|k,v|

if v.instance_of?(Array)

v.map {|e| &quot;#{urlencode(k.to_s)}=#{urlencode(e.to_s)}&quot;}.join(sep)

else

&quot;#{urlencode(k.to_s)}=#{urlencode(v.to_s)}&quot;

end

}.join(sep)

request.content_type = 'application/x-www-form-urlencoded'

end

alias form_data= set_form_data

def urlencode(str)

str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }

end

end

end </pre>
<p>And then <strong>use set_form data</strong> as follows:</p>
<pre class="brush: ruby; title: ;"> request.set_form_data(request, {&quot;parameter1&quot; =&gt; &quot;a&quot;, &quot;parameter2&quot; =&gt; [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;], &quot;parameter3&quot; =&gt; &quot;c&quot; }) </pre>
<p>Thus we can<strong> pass a array in parameters</strong> while use <strong>net/http of ruby 1.8.7</strong>.</p>
<p>If any other solutions please feel free to share.</p>
<p>References : <a href="http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data">http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data</a></p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/08/passing-arrays-using-net-http-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Word Search Using grep and AWK</title>
		<link>http://railstech.com/2011/06/word-search-using-grep-and-awk/</link>
		<comments>http://railstech.com/2011/06/word-search-using-grep-and-awk/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 12:23:43 +0000</pubDate>
		<dc:creator>Kelvin Kunjukutty</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[pattern search]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[word search]]></category>

		<guid isPermaLink="false">http://railstech.com/?p=421</guid>
		<description><![CDATA[In one of our project we needed to search for multiple words from a file and return the found words.
These files would be large and there might be large number of words to search for also.
Ruby has a good support for regexp, however it would be slow in doing these searches for patterns as compared [...]]]></description>
			<content:encoded><![CDATA[<p>In one of our project we needed to search for multiple words from a file and return the found words.<br />
These files would be large and there might be large number of words to search for also.</p>
<p>Ruby has a good support for regexp, however it would be slow in doing these searches for patterns as compared to unix commands and hence we decided to look for certain unix commands for doing these searches.</p>
<p>We went through &#8220;<strong>grep</strong>&#8220;, &#8220;<strong>awk</strong>&#8221; and &#8220;<strong>boyermoore searching algorithm in ruby</strong>&#8221; for doing this search.<br />
Each of these were used for searching patterns in a file.</p>
<p>grep and AWK were giving the results as the line(not he line number but the whole content of line) on which the matching pattern were present. Working on grep and awk further we found differnt ways of searching from files using these.<br />
<span id="more-421"></span><br />
<strong>Using grep:</strong></p>
<p>For searching a word from a file by grep we can say :</p>
<pre class="brush: ruby; title: ;">grep &quot;word&quot; filepath</pre>
<p>e.g</p>
<pre class="brush: ruby; title: ;">grep &quot;cool&quot; fun.txt</pre>
<p>For searching multiple words from a file we can use :</p>
<pre class="brush: ruby; title: ;">grep &quot;word1\|word2&quot; filepath</pre>
<p>e.g</p>
<pre class="brush: ruby; title: ;">grep &quot;cool\|hot&quot; fun.txt</pre>
<p>This would return any word which contains cool or hot in it.(i.e it would return even hotter as a match)</p>
<p>To make it a whole word compare we have to add a -w option to it.</p>
<pre class="brush: ruby; title: ;">grep -w &quot;word1\|word2&quot; filepath</pre>
<p>This would return only words which contains cool or hot in it.</p>
<p>Also we can take the words from the list in file and compare those and search for other file.</p>
<pre class="brush: ruby; title: ;">grep -f list.txt filepath</pre>
<p>e.g</p>
<pre class="brush: ruby; title: ;">grep -f list.txt file.txt</pre>
<p>Here list.txt contains the list of words(each word in a new line) and the file.txt is the file to be searched in.</p>
<p>Now these gave the line in which the words were present as the result.</p>
<p>Now we wanted which words matched and not the whole line, so we added a option &#8221; -o &#8221; to it.</p>
<p>So final grep command was :</p>
<pre class="brush: ruby; title: ;">grep -o -w &quot;word1\|word2&quot; filepath</pre>
<p><strong><br />
Using AWK:</strong></p>
<p>For searching a word from a file by awk we can say:</p>
<pre class="brush: ruby; title: ;">awk '/word/' filepath</pre>
<p>For searching the file with the whole word and not those that has word as a part of it, we can use  (This is similar to using -w with grep) :</p>
<pre class="brush: ruby; title: ;">awk '/\&lt;word\&gt;/' filepath</pre>
<p>For searching multiple words we can use :</p>
<pre class="brush: ruby; title: ;">awk '/\&lt;word1\&gt;|\&lt;word2\&gt;/' filepath</pre>
<p>Similarly here also the result wld give the whole line as result in which the word was present, so to give only the matched words the command was :</p>
<pre class="brush: ruby; title: ;">awk '{for(i=1;i&lt;=NF;i++){if($i~/\&lt;word1\&gt;|\&lt;word2\&gt;/){print $i}}}' filepath</pre>
<p>This would split each word within the file and compare it and give the matched words out as result.</p>
<p><strong>Using</strong> <strong>Boyermoore Searching Algorithm</strong><strong>:</strong></p>
<p>Also we has tried a searching algorithm written in ruby namely &#8220;boyermoore searching algorithm&#8221;</p>
<p>Here we had to require the library file required for it and call it as:</p>
<pre class="brush: ruby; title: ;">BoyerMoore.search(&quot;ANPANMAN&quot;, &quot;ANP&quot;)</pre>
<p>This would give the result as the position in which the letter has matched, which would give the result as 0 in this case.</p>
<p>Now by benchmarking these three types of searching methods we found that grep and awk are better.</p>
<p>Both of them were giving minor time difference.</p>
<p>So thus we know that grep and awk are one of the solution for searching patterns in a file.</p>
]]></content:encoded>
			<wfw:commentRss>http://railstech.com/2011/06/word-search-using-grep-and-awk/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

