<?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>Khelll&#039;s Blog &#187; JRuby</title>
	<atom:link href="http://khelll.com/blog/category/jruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://khelll.com/blog</link>
	<description>Cool Web Development...</description>
	<lastBuildDate>Thu, 13 Oct 2011 03:48:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Google App Engine, JRuby, Sinatra and some fun!</title>
		<link>http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/</link>
		<comments>http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 14:05:57 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[GAE]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>
		<category><![CDATA[App Engine]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=671</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/' addthis:title='Google App Engine, JRuby, Sinatra and some fun! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Yesterday I was experimenting with Google App Engine for a little project that I was working on. I literally started from ground zero and could do my thing after a long night. I&#8217;m blogging about it to share you the &#8230; <a href="http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/' addthis:title='Google App Engine, JRuby, Sinatra and some fun!' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/' addthis:title='Google App Engine, JRuby, Sinatra and some fun! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>Yesterday I was experimenting with <a href="http://code.google.com/appengine/">Google App Engine</a> for a little project that I was working on. I literally started from ground zero and could do my thing after a long night. I&#8217;m blogging about it to share you the idea and save you some time googling solutions. So, here is the thing in brief: I wanted to <strong>parse</strong> some feed on <strong>periodical basis</strong> and <strong>send an email</strong> with new entries. </p>
<p>Looks like a 10 minutes with <a href="http://nokogiri.org/">Nokogiri</a> and cron jobs. Actually that&#8217;s true except of the fact that I need to pay for a VPS so that I can run the script with various gems (since I needed to do some work on the feeds before sending, but that&#8217;s for another topic) and for using cron jobs.  Thus, I decided to try something new this time and I went with GAE since it has <a href="http://code.google.com/appengine/docs/java/memcache/">memecached</a> that I can use as an LRU cache, also it has <a href="http://code.google.com/appengine/docs/java/config/cron.html">cronjobs</a> and finally it has a <a href="http://code.google.com/appengine/docs/java/mail/overview.html">mail</a> system. I&#8217;m using <a href="http://jruby.org/">JRuby</a> and <a href="http://www.sinatrarb.com/">Sinatra</a> for this project.</p>
<p>Here are the steps:</p>
<p>Install JRuby, I&#8217;m using <a href="http://rvm.beginrescueend.com/">RVM</a> on my machine:</p>

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

<p>Install needed gems, those versions are the ones which worked for me:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> jbundle
gem <span style="color: #c20cb9; font-weight: bold;">install</span> appengine-sdk <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;1.4.0&quot;</span>
gem <span style="color: #c20cb9; font-weight: bold;">install</span> google-appengine <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;0.0.19&quot;</span></pre></div></div>

<p>Create a simple app:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">appcfg.rb generate_app notifier
<span style="color: #7a0874; font-weight: bold;">cd</span> notifier</pre></div></div>

<p>Now let&#8217;s create the following files <em>(Please note that the following code can be further enhanced)</em>:</p>
<p><strong>Gemfile</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Critical default settings:</span>
disable_system_gems
disable_rubygems
bundle_path <span style="color:#996600;">&quot;.gems/bundler_gems&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># List gems to bundle here:</span>
gem <span style="color:#996600;">&quot;appengine-apis&quot;</span>
gem <span style="color:#996600;">&quot;appengine-rack&quot;</span>
gem <span style="color:#996600;">&quot;sinatra&quot;</span>
gem <span style="color:#996600;">&quot;sinatra-reloader&quot;</span>
gem <span style="color:#996600;">&quot;jruby-rack&quot;</span>, <span style="color:#996600;">&quot;1.0.4&quot;</span>
gem <span style="color:#996600;">&quot;nokogiri&quot;</span>,<span style="color:#996600;">&quot;1.5.0.beta.3&quot;</span></pre></div></div>

<p><strong>config.ru</strong></p>
<p><em>(fill the sender/receiver emails and the feed URL)</em></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span> 
&nbsp;
set <span style="color:#ff3333; font-weight:bold;">:sender_email</span>, <span style="color:#996600;">'SENDER_EMAIL'</span>
set <span style="color:#ff3333; font-weight:bold;">:receiver_email</span>, <span style="color:#996600;">'RCEIVER_EMAIL'</span>
set <span style="color:#ff3333; font-weight:bold;">:feed_url</span>, <span style="color:#996600;">'FEED_URL'</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'app'</span>
run <span style="color:#6666ff; font-weight:bold;">Sinatra::Application</span></pre></div></div>

<p><strong>app.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest/sha1'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'appengine-apis/memcache'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'appengine-apis/logger'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'appengine-apis/mail'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'appengine-apis/urlfetch'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra/reloader'</span> <span style="color:#9966CC; font-weight:bold;">if</span> development?
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'nokogiri'</span>
&nbsp;
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Fetcher 
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>sender_email,receiver_email,feed_url<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@sender_email</span> = sender_email
    <span style="color:#0066ff; font-weight:bold;">@receiver_email</span> = receiver_email
    <span style="color:#0066ff; font-weight:bold;">@feed_url</span> = feed_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> act
    send_email<span style="color:#006600; font-weight:bold;">&#40;</span>create_message<span style="color:#006600; font-weight:bold;">&#40;</span>get_results<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#996600;">&quot;done&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private 
	<span style="color:#9966CC; font-weight:bold;">def</span> memcache
    <span style="color:#0066ff; font-weight:bold;">@memcached</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#6666ff; font-weight:bold;">AppEngine::Memcache</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:namespace</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;cache&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># get the new results only</span>
	<span style="color:#9966CC; font-weight:bold;">def</span> filter_results<span style="color:#006600; font-weight:bold;">&#40;</span>results<span style="color:#006600; font-weight:bold;">&#41;</span>
	  res = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
	  results.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>hashed_guid,info<span style="color:#006600; font-weight:bold;">|</span>
	    <span style="color:#9966CC; font-weight:bold;">if</span> memcache.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>hashed_guid<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
	      memcache.<span style="color:#9900CC;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span>hashed_guid, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
	      res <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> info
	    <span style="color:#9966CC; font-weight:bold;">end</span>
	  <span style="color:#9966CC; font-weight:bold;">end</span>
	  res
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># parse the feed</span>
	<span style="color:#9966CC; font-weight:bold;">def</span> get_results
	  results = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
	  doc = <span style="color:#6666ff; font-weight:bold;">Nokogiri::XML</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">AppEngine::URLFetch</span>.<span style="color:#9900CC;">fetch</span><span style="color:#006600; font-weight:bold;">&#40;</span>@feed_url<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">body</span><span style="color:#006600; font-weight:bold;">&#41;</span>
	  doc.<span style="color:#9900CC;">css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'item'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span>
	    title = item.<span style="color:#9900CC;">at_css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'title'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">content</span>
	    link = item.<span style="color:#9900CC;">at_css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'link'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">content</span>
	    description = item.<span style="color:#9900CC;">at_css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'description'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">content</span>
	    guid = item.<span style="color:#9900CC;">at_css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'guid'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">content</span>
	    results<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>guid<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> title,:description <span style="color:#006600; font-weight:bold;">=&gt;</span> description, <span style="color:#ff3333; font-weight:bold;">:link</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> link<span style="color:#006600; font-weight:bold;">&#125;</span>
	  <span style="color:#9966CC; font-weight:bold;">end</span>
	  filter_results<span style="color:#006600; font-weight:bold;">&#40;</span>results<span style="color:#006600; font-weight:bold;">&#41;</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># create email message</span>
	<span style="color:#9966CC; font-weight:bold;">def</span> create_message<span style="color:#006600; font-weight:bold;">&#40;</span>results<span style="color:#006600; font-weight:bold;">&#41;</span>
	  template = <span style="color:#996600;">&quot;&quot;</span>
	  results.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>info<span style="color:#006600; font-weight:bold;">|</span>
	    template <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>DOC
&nbsp;
	      Title: <span style="color:#008000; font-style:italic;">#{info[:title]}</span>
	      Description: <span style="color:#008000; font-style:italic;">#{info[:description]}</span>
	      Link: <span style="color:#008000; font-style:italic;">#{info[:link]}</span>
	      <span style="color:#006600; font-weight:bold;">--------------------------------------------------------------------------------------------</span>
	    DOC
	  <span style="color:#9966CC; font-weight:bold;">end</span>
	  template
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> send_email<span style="color:#006600; font-weight:bold;">&#40;</span>message<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#6666ff; font-weight:bold;">AppEngine::Mail</span>::send<span style="color:#006600; font-weight:bold;">&#40;</span>@sender_email,@receiver_email,<span style="color:#996600;">&quot;New updates for the site&quot;</span>,message<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> message.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#996600;">&quot;Hello World!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
get <span style="color:#996600;">'/notify'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  Fetcher.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>settings.<span style="color:#9900CC;">sender_email</span>,settings.<span style="color:#9900CC;">receiver_email</span>,settings.<span style="color:#9900CC;">feed_url</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">act</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>cron.xml</strong></p>
<p><em>(You can set the period here, I&#8217;m using 15 minutes intervals)</em></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cronentries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cron<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/notify<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>fetch new data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;schedule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>every 15 minutes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/schedule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cron<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cronentries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now, create an application-id at <a href="http://appengine.google.com/start/createapp">appspot.com</a>. Then go to <em>Administration >> Permissions</em> and make sure the <em>sender/receiver emails</em> play some role in the app. Personally, I granted them the developer role.</p>
<p>Now, go back to the source and modify the application-id in WEB-INF/app.yaml</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">application your<span style="color:#006600; font-weight:bold;">-</span>app<span style="color:#006600; font-weight:bold;">-</span>id</pre></div></div>

<p>We should be ready now, start development server, watch the console and make sure no exceptions are there:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">dev_appserver.rb .</pre></div></div>

<p>If everything is OK, go to <a href="http://localhost:8080/notfiy">http://localhost:8080/notfiy</a> and you should see &#8220;Done&#8221;. </p>
<p>Let&#8217;s go live now:</p>
<p><em>(the first time you execute this, you will prompted to submit the GAE email/pass combination)</em></p>

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

<p>It should now be running at http://your-app-id.appspot.com, and in few minutes you should be receiving the first email. If not, go to app engine page then to <em>Main >> Logs</em>  and check what&#8217;s the problem. </p>
<p>I like this stack cause it&#8217;s Zero cent cost, Zero deployment time, flexible, and it gives me exactly the freedom I want in terms of needed gems. Give it a shot, you won&#8217;t regret it!</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/' addthis:title='Google App Engine, JRuby, Sinatra and some fun!' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/google-app-engine-jruby-sinatra-and-some-fun/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>The power of JRuby</title>
		<link>http://khelll.com/blog/ruby/the-power-of-jruby/</link>
		<comments>http://khelll.com/blog/ruby/the-power-of-jruby/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 08:08:09 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=280</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/the-power-of-jruby/' addthis:title='The power of JRuby '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>It&#8217;s true that I&#8217;m not the qualified guy to talk about Java&#8217;s power,as it has been 2 years since i last practiced it, but i feel like i have to communicate my thoughts to the people that didn&#8217;t give JRuby &#8230; <a href="http://khelll.com/blog/ruby/the-power-of-jruby/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/the-power-of-jruby/' addthis:title='The power of JRuby' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/the-power-of-jruby/' addthis:title='The power of JRuby '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>It&#8217;s true that I&#8217;m not the qualified guy to talk about Java&#8217;s power,as it has been 2 years since i last practiced it, but i feel like i have to communicate my thoughts to the people that didn&#8217;t give JRuby a trial yet, and why they should do so.<br />
If you never worked with Java before, then don&#8217;t panic, cause in these article i would list several reasons why JRuby is so powerful even if you don&#8217;t know anything about Java.<br />
Lemme start listing all the points that makes me feel the power of JRuby; the order here is irrelevant as i do believe that every point listed bellow has somehow the same rank that others might have:</p>
<p>1- Mixing the power of Java with the power of Ruby.<br />
2- A faster(if not the fastest) Ruby implementation.<br />
3- Native Threads.<br />
4- Support for Foreign Function Interface(FFI).<br />
5- Make use of the JVM (HotSpot).<br />
6- Have a great community and support.</p>
<h2>Mixing the power of Java with the power of Ruby</h2>
<p>There are thousands of Java packages that are ready for you to use, and according to <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE</a>, Java is continuing to be the most popular language, so why don&#8217;t you make use of the existing Java packages, and use them inside your code?<br />
For example, you can use the swing package(a very powerful GUI package) inside your ruby code, the following snippet of code is taken from the samples folder in the JRuby distribution:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Import Java packages</span>
<span style="color:#9966CC; font-weight:bold;">include</span> Java
&nbsp;
import javax.<span style="color:#9900CC;">swing</span>.<span style="color:#9900CC;">JFrame</span>
&nbsp;
frame = JFrame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Hello Swing&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
button = javax.<span style="color:#9900CC;">swing</span>.<span style="color:#9900CC;">JButton</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Klick Me!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
button.<span style="color:#9900CC;">add_action_listener</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>evt<span style="color:#006600; font-weight:bold;">|</span>
  javax.<span style="color:#9900CC;">swing</span>.<span style="color:#9900CC;">JOptionPane</span>.<span style="color:#9900CC;">showMessageDialog</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;</span>EOS<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&lt;</span>html<span style="color:#006600; font-weight:bold;">&gt;</span>Hello from <span style="color:#006600; font-weight:bold;">&lt;</span>b<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>u<span style="color:#006600; font-weight:bold;">&gt;</span>JRuby<span style="color:#006600; font-weight:bold;">&lt;/</span>u<span style="color:#006600; font-weight:bold;">&gt;&lt;/</span>b<span style="color:#006600; font-weight:bold;">&gt;</span>.<span style="color:#006600; font-weight:bold;">&lt;</span>br<span style="color:#006600; font-weight:bold;">&gt;</span>
Button <span style="color:#996600;">'#{evt.getActionCommand()}'</span> clicked.
<span style="color:#9900CC;">EOS</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Add the button to the frame</span>
frame.<span style="color:#9900CC;">get_content_pane</span>.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>button<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Show frame</span>
frame.<span style="color:#9900CC;">set_default_close_operation</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">JFrame::EXIT_ON_CLOSE</span><span style="color:#006600; font-weight:bold;">&#41;</span>
frame.<span style="color:#9900CC;">pack</span>
frame.<span style="color:#9900CC;">visible</span> = <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

<p>You can also reference ruby code inside Java code, examples could be found <a href="http://wiki.JRuby.org/wiki/Direct_JRuby_Embedding">here</a>.</p>
<h2>A faster(if not the fastest) Ruby implementation</h2>
<p>JRuby team consider it a bug if they are slower on something than <a href="http://en.wikipedia.org/wiki/Ruby_MRI">MRI</a>, thus they keep enhancing the performance of JRuby.<br />
According to Antonio Cangiano, the guy behind the great ruby shootout <a href="http://antoniocangiano.com/2008/12/09/the-great-ruby-shootout-december-2008/">here</a> and <a href="http://antoniocangiano.com/2008/12/10/reflections-on-the-ruby-shootout/">here</a>:</p>
<blockquote><p>Ruby 1.9 and JRuby are very close, respectively 2.5 and 1.9 faster than Ruby 1.8.7 (from source) on these benchmarks.</p></blockquote>
<p>Surprisingly JRuby was slower than ruby 1.8.6 last year, but now it&#8217;s competeing 1.9 in terms of speed.<br />
<a href="Antonio Cangiano, Software Engineer">Charles Nutter</a> the JRuby team leader commented on that saying:</p>
<blockquote><p>I&#8217;m looking forward to seeing another update once JRuby finishes 1.9 support, since these JRuby numbers don&#8217;t reflect execution and library optimizations 1.9 provides. I expect that JRuby in 1.9 mode will perform much closer to Ruby 1.9.</p>
<p>Until then, it&#8217;s good to hear we&#8217;re the fastest 1.8 implementation. Thanks again!</p></blockquote>
<h2>Native Threads</h2>
<p>The current ruby interpreter which is called MRI supports <a href="http://en.wikipedia.org/wiki/Green_threads">green threads</a>, as opposed to JRuby which supports native threads.<br />
Green threads are managed by VM, not by the operating system, and thus, by using them you lose a significant power called &#8220;<a href="http://en.wikipedia.org/wiki/Concurrency_(computer_science)">concurrency</a>&#8221; which is available to multi core processors, that&#8217;s because the VM will continue to use one core to serve the whole threads, as opposed to native threads which will use the full power of multi core processor, by being assigned to the available cores.<br />
In general an application might be IO-bounded(lots of IO operations, like logging and database related ones) or CPU-bounded(lots of computation), and while green threads are useful for the IO-bounded applications, they are not useful for the CPU-bounded ones.<br />
A CPU-bonded applications consumes the CPU, and thus you can increase the speed of your program by having multi core processor, but thus you need native threaded applications, to make use of the available cores.<br />
JRuby comes to rescue as it supports native threads, and thus you have full concurrency when you have multi core processors.</p>
<h2>Support for Foreign Function Interface(FFI)</h2>
<p>According to wikipedia a <a href="http://en.wikipedia.org/wiki/Foreign_function_interface">Foreign Function Interface</a> (or FFI) is :</p>
<blockquote><p>A mechanism by which a program written in one programming language can call routines or make use of services written in another. </p></blockquote>
<p>Why would a one need that? well many gems that we use like <a href="http://rmagick.rubyforge.org/">Rmagic</a>,<a href="http://hpricot.com/hpricot_rdoc154/index.html">Hpricot</a> and <a href="http://ruby-prof.rubyforge.org/">ruby-prof</a> uses C extensions, either because of performance or because of the ready existing libs related matters.<br />
Well that was causing problems for JRuby users as they couldn&#8217;t run those gems that have C extensions, and also for gem developers who had to maintain 2 versions of such gems to let them work for both MRI and JRuby.<br />
That was a kind of disaster till Charles <a href="http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html">announced</a> that FFI is available for ruby.<br />
So now when you need to have C code inside ruby, then it&#8217;s highly recommended to use the <a href="http://kenai.com/projects/ruby-ffi">FFI gem</a> to save yourself having dual code, and also to make your code work on different ruby implementations.</p>
<h2>Make use of the JVM (HotSpot)</h2>
<p>Since Sun has <a href="http://Java.sun.com/Javase/technologies/hotspot/">announced</a> it&#8217;s open source strategy, and lots of optimization work was done for the <a href="http://en.wikipedia.org/wiki/HotSpot">JVM(HotSpot)</a>, the reason behind these optimizations was the fact that many languages is adopting Java to be the hosting language for them, and thus having JVM as an open source technology helped to optimize it to better serve these languages. Some of these languages are: <a href="http://jruby.codehaus.org/">JRuby</a>, <a href="http://groovy.codehaus.org/">Groovy</a>, <a href="http://www.scala-lang.org/">Scala</a> and <a href="http://clojure.org/">Clojure</a>.<br />
So you might be wondering &#8220;How this will affect me?&#8221;, actually it will affect you indirectly cause once you have a better JVM, it means you have a faster JRuby implementation, and a better management for your memory, as the JVM is considered to have the best garbage collector.<br />
Also you might be able one day to work with other languages(Clojure for example) directly in your ruby code.</p>
<h2>Have a great community and support</h2>
<p>JRuby has a very nice community, and the guys behind it are very helpful and open to different opinions, just log to #JRuby channel on the FreeNode on IRC, or subscribe to <a href="mailto:user@JRuby.codehaus.org">mailing list</a> and send your questions, and you will get the minimum support to let you go.<br />
What i like about this community is their love to the Ruby community, they like to help any rubiest, they even worked with Merb(merb 2 is the core of Rails 3) guys to provide a better performance for merb users.<br />
The current active JRuby contributers are: <a href="http://blog.headius.com/">Charles Nutter</a>, <a href="http://www.bloglines.com/blog/ThomasEEnebo">Tom Enebo</a>, <a href="http://wmeissner.blogspot.com">Wayne Meissner</a>, Marcin Mielzynski and <a href="http://blog.nicksieger.com/">Nick Sieger</a>, and here some of the past contributers: <a href="http://olabini.com/blog/">Ola Bini</a> and <a href="http://blog.emptyway.com/">Vladimir Sizikov</a></p>
<p>I hope that i could deliver some of the good points about JRuby, and would like really to have your comments, and what might be also missing from those points. You can find bellow a list of references.</p>
<h3>References</h3>
<p>*<a href="http://rubyconf2008.confreaks.com/jruby-what-why-howtry-it-now.html">JRuby: What, Why, How&#8230;Try It Now</a> by Charles Nutter and Tom Enebo</p>
<p>*<a href="http://www.infoq.com/presentations/nutter-jruby-jvm-lang-summit">JRuby: The Pain of Bringing an Off-Platform Dynamic Language to the JVM </a> by Charles Nutter</p>
<p>*<a href="http://blog.headius.com/">Charles Nutter blog</a></p>
<p><strong>Update 1</strong><br />
The snippet of code was updated as a respond to Collin&#8217;s <a href="http://www.khelll.com/blog/ruby/the-power-of-jruby/comment-page-1/#comment-173">note</a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/the-power-of-jruby/' addthis:title='The power of JRuby' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/the-power-of-jruby/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

