<?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>JPz&#039;log &#187; Ruby</title>
	<atom:link href="http://jpz-log.info/tags/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://jpz-log.info</link>
	<description>Coin Coin and Plop da Plop</description>
	<lastBuildDate>Thu, 29 Jul 2010 06:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Script de compilation LaTeX en Ruby</title>
		<link>http://jpz-log.info/archives/2008/01/28/script-de-compilation-latex-en-ruby/</link>
		<comments>http://jpz-log.info/archives/2008/01/28/script-de-compilation-latex-en-ruby/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 21:09:42 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[French]]></category>
		<category><![CDATA[Geeking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jpz-log.info/archives/2008/01/28/script-de-compilation-latex-en-ruby/</guid>
		<description><![CDATA[Mon manuscrit de thÃ¨se sera rÃ©digÃ© en LaTeX (le premier qui me parle de Word se fera frapper avec violence ) et histoire d'automatiser les choses, j'ai Ã©crit un script de compilation en Ruby. J'aurai pu utiliser un Makefile, un des scripts existants (il y en a un trÃ¨s bien en Perl dont j'ai oubliÃ© [...]]]></description>
			<content:encoded><![CDATA[<p>Mon manuscrit de thÃ¨se sera rÃ©digÃ© en LaTeX (le premier qui me parle de Word se fera frapper avec violence <img src='http://jpz-log.info/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) et histoire d'automatiser les choses, j'ai Ã©crit un script de compilation en Ruby.</p>
<p>J'aurai pu utiliser un Makefile, un des scripts existants (il y en a un trÃ¨s bien en Perl dont j'ai oubliÃ© le nom), du Python, du Groovy... mais ce script lÃ  me va trÃ¨s bien !</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'optparse'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fileutils'</span>
&nbsp;
MAIN_FILE = <span style="color:#996600;">&quot;phd-thesis&quot;</span>
PDFLATEX  = <span style="color:#996600;">&quot;pdflatex #{MAIN_FILE}.tex&quot;</span>
BIBTEX    = <span style="color:#996600;">&quot;bibtex #{MAIN_FILE}.aux&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> build_pdf
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC0066; font-weight:bold;">system</span> PDFLATEX
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC0066; font-weight:bold;">system</span> BIBTEX
  callcc <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>stop_build<span style="color:#006600; font-weight:bold;">|</span>
    3.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      status = <span style="color:#CC0066; font-weight:bold;">system</span> PDFLATEX
      stop_build.<span style="color:#9900CC;">call</span> <span style="color:#9966CC; font-weight:bold;">unless</span> status
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> single_compilation
  <span style="color:#CC0066; font-weight:bold;">system</span> PDFLATEX
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> clean_artifacts
  extensions = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>blg log pdf aux bbl lof lot out toc tps<span style="color:#006600; font-weight:bold;">&#93;</span>
  files = extensions.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>ext<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{MAIN_FILE}.#{ext}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#6666ff; font-weight:bold;">FileUtils::Verbose</span>::rm files, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> open_pdf<span style="color:#006600; font-weight:bold;">&#40;</span>how<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">&quot;#{how} #{MAIN_FILE}.pdf&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> run
  options = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:command</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:pdf</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
  OptionParser.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>opts<span style="color:#006600; font-weight:bold;">|</span>
    opts.<span style="color:#9900CC;">banner</span> = <span style="color:#996600;">&quot;Usage: build.rb [options]&quot;</span>
&nbsp;
    opts.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;--pdf&quot;</span>, <span style="color:#996600;">&quot;Build the PDF output (default)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>pdf<span style="color:#006600; font-weight:bold;">|</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:command</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#ff3333; font-weight:bold;">:pdf</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    opts.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;--oneshot&quot;</span>, <span style="color:#996600;">&quot;Single LaTeX compilation&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>oneshot<span style="color:#006600; font-weight:bold;">|</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:command</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#ff3333; font-weight:bold;">:oneshot</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    opts.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;--clean&quot;</span>, <span style="color:#996600;">&quot;Clean the build artifacts&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>clean<span style="color:#006600; font-weight:bold;">|</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:command</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#ff3333; font-weight:bold;">:clean</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    opts.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;--open-pdf [HOW]&quot;</span>, <span style="color:#996600;">&quot;Open the generated PDF with HOW)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>how<span style="color:#006600; font-weight:bold;">|</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:openpdf</span><span style="color:#006600; font-weight:bold;">&#93;</span> = how
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    opts.<span style="color:#9900CC;">on_tail</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;-h&quot;</span>, <span style="color:#996600;">&quot;--help&quot;</span>, <span style="color:#996600;">&quot;Show this message&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> opts
      <span style="color:#CC0066; font-weight:bold;">exit</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">parse</span>!
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">case</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:command</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:pdf</span>
    build_pdf
    open_pdf<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:openpdf</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:openpdf</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:oneshot</span>
    single_compilation
    open_pdf<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:openpdf</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:openpdf</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:clean</span>
    clean_artifacts
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>  
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
run</pre></div></div>

<p>Je ne garanti pas que le code soit parfait, alors n'hÃ©sitez pas Ã  Ã©mettre des critiques constructives !</p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2008/01/28/script-de-compilation-latex-en-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Rails-style Ruby meta-programming</title>
		<link>http://jpz-log.info/archives/2007/10/15/rails-style-ruby-meta-programming/</link>
		<comments>http://jpz-log.info/archives/2007/10/15/rails-style-ruby-meta-programming/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 20:10:19 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Geeking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jpz-log.info/archives/2007/10/15/rails-style-ruby-meta-programming/</guid>
		<description><![CDATA[I am giving a Ruby lecture tomorrow (some students happen to be quite lucky in fact). I wanted to present an example of a Ruby On Rails style meta programming technique. For example you can have a find_by_name or find_by_email method on your ActiveRecord objects. Those methods are in fact generated on the fly using [...]]]></description>
			<content:encoded><![CDATA[<p>I am giving a Ruby lecture tomorrow (some students happen to be quite lucky in fact). I wanted to present an example of a <a href="http://www.rubyonrails.org/">Ruby On Rails</a> style meta programming technique. For example you can have a <code>find_by_name</code> or <code>find_by_email</code> method on your <code>ActiveRecord</code> objects. Those methods are in fact generated on the fly using a technique which looks like what follows. This has probably been explained a million times somewhere else on the web, but I don't care <img src='http://jpz-log.info/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>So let's start with a very basic contacts book class which looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ContactBook
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@contacts</span> = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> add_contact<span style="color:#006600; font-weight:bold;">&#40;</span>name, email<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@contacts</span><span style="color:#006600; font-weight:bold;">&#91;</span>name<span style="color:#006600; font-weight:bold;">&#93;</span> = email
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> contact_named<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@contacts</span><span style="color:#006600; font-weight:bold;">&#91;</span>name<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
book = ContactBook.<span style="color:#9900CC;">new</span>
book.<span style="color:#9900CC;">add_contact</span> <span style="color:#996600;">&quot;Pierre&quot;</span>, <span style="color:#996600;">&quot;pierre@zzland.fr&quot;</span>
book.<span style="color:#9900CC;">add_contact</span> <span style="color:#996600;">&quot;Julien&quot;</span>,  <span style="color:#996600;">&quot;julien@zzland.fr&quot;</span>
book.<span style="color:#9900CC;">add_contact</span> <span style="color:#996600;">&quot;Yoan&quot;</span>,   <span style="color:#996600;">&quot;yoan@zzland.fr&quot;</span>
book.<span style="color:#9900CC;">add_contact</span> <span style="color:#996600;">&quot;Mr Bean&quot;</span>, <span style="color:#996600;">&quot;info@mrbean.com&quot;</span></pre></div></div>

<p>Nothing impressive here, so now let's get all the contacts whose email contain a given substring:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ContactBook
  <span style="color:#9966CC; font-weight:bold;">def</span> email_containing<span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@contacts</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span> value.<span style="color:#9966CC; font-weight:bold;">include</span>? str <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> display_contact<span style="color:#006600; font-weight:bold;">&#40;</span>contact<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{contact[0]} &lt;#{contact[1]}&gt;&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
zzland = book.<span style="color:#9900CC;">email_containing</span> <span style="color:#996600;">&quot;zzland&quot;</span>
zzland.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>contact<span style="color:#006600; font-weight:bold;">|</span> display_contact<span style="color:#006600; font-weight:bold;">&#40;</span>contact<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>But what if we could have <code>email_thestringtolookfor</code> methods instead of this? Let's do it by evaluating some code on the fly:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ContactBook
  <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>id, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    method_name = id.<span style="color:#9900CC;">to_s</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> method_name<span style="color:#006600; font-weight:bold;">&#91;</span>0..5<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'email_'</span>
      str = method_name<span style="color:#006600; font-weight:bold;">&#91;</span>6..<span style="color:#9900CC;">method_name</span>.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      to_eval = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>END_FUNC
        <span style="color:#9966CC; font-weight:bold;">def</span> email_<span style="color:#008000; font-style:italic;">#{str}</span>
          <span style="color:#0066ff; font-weight:bold;">@contacts</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span> value.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#996600;">&quot;#{str}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      END_FUNC
      instance_eval to_eval
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;self.email_#{str}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
zzland = book.<span style="color:#9900CC;">email_zzland</span>
bean  = book.<span style="color:#9900CC;">email_bean</span>
zzland.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>contact<span style="color:#006600; font-weight:bold;">|</span> display_contact<span style="color:#006600; font-weight:bold;">&#40;</span>contact<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-----&quot;</span>
bean.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>contact<span style="color:#006600; font-weight:bold;">|</span> display_contact<span style="color:#006600; font-weight:bold;">&#40;</span>contact<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<ol>
<li><code>method_missing</code> is invoked whenever a message is sent to the class instance, but no matching method name can be found.</li>
<li>If the name starts with <code>email_</code>, then we are in luck! (note that I should throw an exception if the name doesn't start with this to comply with the Ruby semantics, but I was too lazy for that)</li>
<li>We generate a new method for the instance through evaluation.</li>
<li>We do not forget to invoke the new method through evaluation, as the original call would return <code>nil</code> since the method had not been found.</li>
</ol>
<p>Nice isn't it? <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2007/10/15/rails-style-ruby-meta-programming/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby fun with Rinda</title>
		<link>http://jpz-log.info/archives/2007/10/11/ruby-fun-with-rinda/</link>
		<comments>http://jpz-log.info/archives/2007/10/11/ruby-fun-with-rinda/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 19:47:33 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Geeking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jpz-log.info/archives/2007/10/11/ruby-fun-with-rinda/</guid>
		<description><![CDATA[Today I had fun playing with Rinda, the Ruby implementation of the Linda distributed process coordination paradigm. I also had some time for blogging, something that I haven't done for a few days because of too much work (and there still a bunch left to be done ) I won't go through the theory behind [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had fun playing with <a href="http://www.ruby-doc.org/stdlib/libdoc/rinda/rdoc/index.html">Rinda</a>, the Ruby implementation of the <a href="http://en.wikipedia.org/wiki/Linda_%28coordination_language%29">Linda</a> distributed process coordination paradigm. I also had some time for blogging, something that I haven't done for a few days because of too much work (and there still a bunch left to be done <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  )</p>
<p>I won't go through the theory behind Linda, but the gross idea is to use a shared memory space that implements the distributed blackboard paradigm : the <a href="http://en.wikipedia.org/wiki/Tuple_space">Tuple Space</a>. It can be summarized on the following figure:</p>
<p><img src='http://jpz-log.info/wp-content/uploads/2007/10/tuplespace.png' alt='tuplespace.png' /></p>
<p>Data is represented as sets of tuples which often have a limited lifetime. The tuple space provides the distributed processes the following primitives:</p>
<ul>
<li><em>write</em>: writes a tuple to the tuple space</li>
<li><em>read</em>: reads a tuple from the tuple space</li>
<li><em>take</em>: reads and removes a tuple from the tuple space</li>
</ul>
<p>The beauty of the model lies in the fact that the tuple space guarantees atomic operations and periodically cleans up old tuples. Also, reading operations match a tuple on:</p>
<ol>
<li>the length of the requested and available tuples</li>
<li>the matching (exact value, regular expressions, ...) of certain elements of the tuple.</li>
</ol>
<p>I have put together a first Ruby program that reads text from the console, and writes tuples whenever a line is entered. The other program takes every new tuple that is emitted.</p>
<p><strong>emitter.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'drb/drb'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rinda/ring'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rinda/tuplespace'</span>
&nbsp;
DRb.<span style="color:#9900CC;">start_service</span>
&nbsp;
tuple_space = <span style="color:#6666ff; font-weight:bold;">Rinda::TupleSpace</span>.<span style="color:#9900CC;">new</span>
ring_server = <span style="color:#6666ff; font-weight:bold;">Rinda::RingServer</span>.<span style="color:#9900CC;">new</span> tuple_space
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  tuple_space.<span style="color:#9900CC;">write</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#CC0066; font-weight:bold;">gets</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>receiver.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'drb/drb'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rinda/ring'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rinda/tuplespace'</span>
&nbsp;
DRb.<span style="color:#9900CC;">start_service</span>
&nbsp;
tuple_space = <span style="color:#6666ff; font-weight:bold;">Rinda::RingFinger</span>.<span style="color:#9900CC;">primary</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> tuple_space.<span style="color:#9900CC;">take</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>What I really like is the simplicity of the code, thanks to the cleanness of Linda and the elegance of Ruby <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  The other thing is that <a href="http://www.ruby-doc.org/stdlib/libdoc/drb/rdoc/index.html">DRb</a> is such a straightforward way to use distributed objects in Ruby...</p>
<p>BTW there is a Java implementation of this paradigm called <a href="http://java.sun.com/developer/technicalArticles/tools/JavaSpaces/index.html">JavaSpaces</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2007/10/11/ruby-fun-with-rinda/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
