<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atomfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="0.3" xml:lang="en-US">
  <title>RubyEnRails.nl: Category english</title>
  <tagline mode="escaped" type="text/html">Brengt de Ruby en RubyOnRails revolutie naar Nederland</tagline>
  <id>tag:blog.rubyenrails.nl,2005:Typo</id>
  <generator url="http://typo.leetsoft.com">Typo</generator>
  <link href="http://blog.rubyenrails.nl/articles/category/english" rel="alternate" type="text/html" />
  <modified>2009-09-21T17:06:30+02:00</modified>
  <link rel="start" href="http://feeds.feedburner.com/rubyenrails_en" type="application/atom+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:0690f040-f741-41e6-90c1-4eb5a453179d</id>
    <issued>2008-12-12T12:23:00+01:00</issued>
    <modified>2009-09-21T17:06:30+02:00</modified>
    <title>Calling Clojure From JRuby</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/bHwCHr4jGT4/calling-clojure-from-jruby" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>ruby on rails</dc:subject>
    <dc:subject>java</dc:subject>
    <dc:subject>english</dc:subject>
    <dc:subject>jruby</dc:subject>
    <dc:subject>clojure</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;&lt;a href="http://clojure.org/"&gt;Clojure&lt;/a&gt; is a new functional programming language based on Lisp, running on the &lt;span class="caps"&gt;JVM&lt;/span&gt;. Even though Clojure is only a year old, there&amp;#8217;s already &lt;a href="http://pragprog.com/titles/shcloj/programming-clojure"&gt;a book&lt;/a&gt; being written about it.&lt;/p&gt;


	&lt;p&gt;Since this is a Ruby site, and Rails is still the best framework for web-apps regardless of language, I&amp;#8217;ll demonstrate how to call Clojure functions from a JRuby Rails-app.&lt;/p&gt;&lt;a href="/articles/2008/12/12/calling-clojure-from-jruby"&gt;Lees verder...&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/bHwCHr4jGT4" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/12/12/calling-clojure-from-jruby</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:63fb359a-d796-4dcb-80b4-8f859e5fb752</id>
    <issued>2008-03-05T14:43:00+01:00</issued>
    <modified>2009-08-23T23:46:19+02:00</modified>
    <title>Our Daily Method #19: Time#method_missing</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/5mx8culG5ag/our-daily-method-19-time-method_missing" rel="alternate" type="text/html" />
    <dc:subject>english</dc:subject>
    <dc:subject>daily_method</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;Our stock of cool tricks is depleted, but inspiration still strikes once in a while.&lt;/p&gt;


	&lt;p&gt;Are you using &lt;code&gt;strftime&lt;/code&gt; a lot? Do you &lt;em&gt;like&lt;/em&gt; writing &lt;code&gt;strfime&lt;/code&gt;? Have you ever spelled it as &lt;code&gt;strtime&lt;/code&gt;? &lt;code&gt;strf_time&lt;/code&gt;? Thought it was &lt;code&gt;frmtstr&lt;/code&gt;?&lt;/p&gt;


	&lt;p&gt;And if you spell it right, you still have to type those stupid %-signs. All in all, room for improvement.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Time
  def method_missing(name,*args)
    if name.to_s =~ /f_/
      c = args[0] || ' '
      x = name.to_s[2..-1].split(//).
          map {|f| f =~ /[a-z]/i ? "%"+f : c}.
          join
      strftime(x)
    else
      super
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So, how does it work?&lt;/p&gt;


	&lt;p&gt;It handles every method starting with &lt;code&gt;f_&lt;/code&gt;. What follows are the characters used within &lt;code&gt;strftime&lt;/code&gt;: Y for 4-digit year, d for day of month, etc. Underscores are converted to spaces, or to the first argument, if you provide one.&lt;/p&gt;


	&lt;p&gt;Examples:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# standard lib
Time.now.strftime("%Y%j")       #=&amp;gt; "2008065" 
Time.now.strftime("s")          #=&amp;gt; "1204725800" 
Time.now.strftime("%d %m %Y")   #=&amp;gt; "05 03 2008" 
Time.now.strftime("%d-%m-%Y")   #=&amp;gt; "05-03-2008" 
Time.now.strftime("%d %b %Y")   #=&amp;gt; "05 Mar 2008" 

# new and improved
Time.now.f_Yj           #=&amp;gt; "2008065" 
Time.now.f_s            #=&amp;gt; "1204725800" 
Time.now.f_d_m_Y        #=&amp;gt; "05 03 2008" 
Time.now.f_d_m_Y('-')   #=&amp;gt; "05-03-2008" 
Time.now.f_d_b_Y        #=&amp;gt; "05 Mar 2008" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Cool? Stupid? Why don&amp;#8217;t you &lt;a href="http://codepad.org/IZ0YDnJF"&gt;play with it&lt;/a&gt; ?&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://workingwithrails.com/recommendation/new/person/5468-michiel-de-mare"&gt;&lt;img alt="Recommend Michiel de Mare" title="Recommend Michiel de Mare" src="http://workingwithrails.com/images/tools/compact-small-button.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/5mx8culG5ag" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/03/05/our-daily-method-19-time-method_missing</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:0de7ec20-0d46-4b46-b6b3-a24f9e0a0622</id>
    <issued>2008-03-05T07:24:00+01:00</issued>
    <modified>2009-09-23T11:39:26+02:00</modified>
    <title>More Prevention of NoMethodErrors on nil</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/Crg-B8NYAJs/more-prevention-of-nomethoderrors-on-nil" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;What a sudden interest in avoiding NoMethodErrors on nil lately!
I&amp;#8217;ve just thought of another method. (Remember, you can solve any problem by adding a layer of indirection.)&lt;/p&gt;


	&lt;p&gt;Say you have Remco&amp;#8217;s canonical example: &lt;code&gt;person.name.upcase&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;Both &lt;code&gt;person&lt;/code&gt; and &lt;code&gt;person.name&lt;/code&gt; may be nil. So we usually write: &lt;code&gt;person &amp;#38;&amp;#38; person.name &amp;#38;&amp;#38; person.name.upcase&lt;/code&gt;.&lt;/p&gt;


Now for the level of indirection. Remember &lt;code&gt;rrss&lt;/code&gt;? I&amp;#8217;ve &lt;a href="http://blog.rubyenrails.nl/articles/2008/02/18/our-daily-method-10-object-r-rs-ds-s"&gt;introduced&lt;/a&gt;
it last month. It stands for &amp;#8220;returns result, same self&amp;#8221;. Its definition is beyond trivial:
&lt;pre&gt;&lt;code&gt;
class Object
  def rrss
    yield self
  end
end
&lt;/code&gt;&lt;/pre&gt;
With &lt;code&gt;rrss&lt;/code&gt; we can rewrite the original as: 
&lt;code&gt;person.rrss {|p| p.name}.rrss {|n| n.upcase}&lt;/code&gt;
Or with &lt;code&gt;to_proc&lt;/code&gt; notation: 
&lt;code&gt;person.rrss(&amp;#38;:name).rrss(&amp;#38;:upcase)&lt;/code&gt;. 
We still need to check for nil though: 

&lt;pre&gt;&lt;code&gt;
person.rrss {|p| p &amp;#38;&amp;#38; p.name}.
       rrss {|n| n &amp;#38;&amp;#38; n.upcase}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Doesn&amp;#8217;t this last bit look like it&amp;#8217;s in need of a bit of refactoring? Its lack of &lt;span class="caps"&gt;DRY&lt;/span&gt;-ness jumps right out of the page.&lt;/p&gt;&lt;a href="/articles/2008/03/05/more-prevention-of-nomethoderrors-on-nil"&gt;Lees verder...&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/Crg-B8NYAJs" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/03/05/more-prevention-of-nomethoderrors-on-nil</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Remco van 't Veer</name>
    </author>
    <id>urn:uuid:ebd5d86d-3c9f-4d93-8460-ad848119c0fd</id>
    <issued>2008-02-29T08:40:00+01:00</issued>
    <modified>2009-09-27T04:04:43+02:00</modified>
    <title>Our Daily Method #18: NilClass#method_missing</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/UlTkHOeCxCc/our-daily-method-18-nilclass-method_missing" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <dc:subject>daily_method</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt;: only apply the following under parental guidance!&lt;/p&gt;


	&lt;p&gt;We know the whiny nil, as applied in Rails, but what about an obedient nil:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class NilClass
  def method_missing(*args)
    nil
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;No more monkeying like:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
person &amp;#38;&amp;#38; person.name &amp;#38;&amp;#38; person.name.upcase
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Not &lt;acronym title="Don&amp;#8217;t Repeat Yourself"&gt;DRY&lt;/acronym&gt; at all!  Let&amp;#8217;s use the obedient nil instead:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
person.name.upcase
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This concludes our last daily method.  Ode to Michiel for providing a vast amount of nice tricks!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/UlTkHOeCxCc" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/29/our-daily-method-18-nilclass-method_missing</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:24fe5974-14d9-46e4-a79d-892d9b800b3e</id>
    <issued>2008-02-27T09:03:00+01:00</issued>
    <modified>2009-08-23T23:35:41+02:00</modified>
    <title>Our Daily Method #17: Enumerable#make_index</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/c34jJtBzC9s/our-daily-method-17-enumerable-make_index" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <dc:subject>daily_method</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;Do you know what I like so much about relational databases? And what I really miss in object-models? That databases make it so effortless to index data.&lt;/p&gt;


	&lt;p&gt;Imagine, you&amp;#8217;ve got a huge pile of data, unordered, continually changing, but because your database keeps a few indexes, you can access any item you wish within a few milliseconds.&lt;/p&gt;


	&lt;p&gt;Wouldn&amp;#8217;t it be fun if you could do that in Ruby? Especially the effortless part? It happens regularly that I&amp;#8217;ve got an array with hundreds of items in which I have to search. That quicky gets slow, so I create a few hashes that I use as indexes. But what a difference with a real database!&lt;/p&gt;


	&lt;p&gt;Let make a first step in the right direction, and implement the &lt;code&gt;make_index&lt;/code&gt; method.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
module Enumerable
  def make_index(*names)
    Struct.new(*names).new(*names.map{|n| group_by(&amp;#38;n)})
  end
end

index = User.find(:all).make_index(:login, :id, :nickname)

index.login['mdemare'] =&amp;gt; [&amp;lt;#User ...&amp;gt;]
index.nickname['Michiel'] =&amp;gt; [&amp;lt;#User ...&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I admit, not a huge improvement yet. But we&amp;#8217;re getting there. For another time, indexes on more than one attribute, unique indexes and indexing attributes with a natural order. (&lt;code&gt;index.created_at &amp;gt; Time.now.at_midnight&lt;/code&gt;)&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://workingwithrails.com/recommendation/new/person/5468-michiel-de-mare"&gt;&lt;img alt="Recommend Michiel de Mare" title="Recommend Michiel de Mare" src="http://workingwithrails.com/images/tools/compact-small-button.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/c34jJtBzC9s" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/27/our-daily-method-17-enumerable-make_index</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:0cc5e34b-ac77-4444-8146-c772a149d681</id>
    <issued>2008-02-26T09:00:00+01:00</issued>
    <modified>2009-08-13T14:02:07+02:00</modified>
    <title>Our Daily Method #16: Array#bsearch</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/rq7564GnAS8/our-daily-method-16-array-bsearch" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <dc:subject>daily_method</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;Searching in arrays is pretty slow. Better use a hash.&lt;/p&gt;


	&lt;p&gt;But if the array is already sorted, you can use a binary search, which works by dividing the array in two and searching in the left orthe right part, until there&amp;#8217;s only one element left.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Array
  def bsearch(k)
    x,y = -1,size
    while y != x + 1 do
      m = (x + y) / 2
      if self[m] &amp;lt;= k
        x = m
      else
        y = m
      end
    end
    x &amp;gt;= 0 &amp;#38;&amp;#38; self[x] == k &amp;#38;&amp;#38; x
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;a href="http://workingwithrails.com/recommendation/new/person/5468-michiel-de-mare"&gt;&lt;img alt="Recommend Michiel de Mare" title="Recommend Michiel de Mare" src="http://workingwithrails.com/images/tools/compact-small-button.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/rq7564GnAS8" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/26/our-daily-method-16-array-bsearch</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:07d85411-67d2-4f99-aa68-8229a48aad75</id>
    <issued>2008-02-26T01:42:00+01:00</issued>
    <modified>2009-09-18T09:30:01+02:00</modified>
    <title>A Hash Is A Function (Hash#to_proc)</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/Y1vzMyRS5as/a-hash-is-a-function-hash-to_proc" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;Staring at the following code, I got an idea (inspired by &lt;a href="http://arclanguage.org/"&gt;Arc&lt;/a&gt;):&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;specialism_codes&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;map&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;SPECIALISMS&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Why do I have to create a block to map from a list of codes to a list of descriptions, when those codes and descriptions are all contained in a constant? Isn&amp;#8217;t that what a Hash is, a mapping from a domain to a range? Isn&amp;#8217;t that &lt;strong&gt;exactly&lt;/strong&gt; the definition of a function in mathematics? Why then that block?&lt;/p&gt;


	&lt;p&gt;Anyway, I solved that pretty quickly:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Hash&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;to_proc&lt;/span&gt;
    &lt;span class="ident"&gt;lambda&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;x&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;x&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;specialism_codes&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;map&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&lt;/span&gt;&lt;span class="constant"&gt;SPECIALISMS&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;&lt;a href="http://workingwithrails.com/recommendation/new/person/5468-michiel-de-mare"&gt;&lt;img alt="Recommend Michiel de Mare" title="Recommend Michiel de Mare" src="http://workingwithrails.com/images/tools/compact-small-button.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/Y1vzMyRS5as" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/26/a-hash-is-a-function-hash-to_proc</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Michiel de Mare</name>
    </author>
    <id>urn:uuid:d072f32c-6ec9-4de4-8e62-833263423be5</id>
    <issued>2008-02-25T09:00:00+01:00</issued>
    <modified>2009-07-14T14:01:04+02:00</modified>
    <title>Our Daily Method #15: Kernel#levenshtein</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/eciyOfrGmJM/our-daily-method-14-kernel-levenshtein" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <dc:subject>daily_method</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;Not in any way invented by us, but still extremely useful and not a part of the standard library: the &lt;a href="http://en.wikipedia.org/wiki/Levenshtein_distance"&gt;Levenshtein Distance&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;What does it calculate? The number of characters to add, delete or switch to turn the first string into the second. Ideal for detecting typos.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# example: 
levenshtein('levenstine', 'levenshtein') # =&amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;

Or how about instead of a &lt;span class="caps"&gt;LIKE&lt;/span&gt;-clause? Not quite as fast (cough) but much more accurate.
&lt;pre&gt;&lt;code&gt;
# In the old days:
User.find(:all, 
    :conditions =&amp;gt; ['name like ?', params[:name] + '%']])

# Now:
User.find(:all).
    sort_by {|u| levenshtein(params[:name], u.name)}.
    first

def levenshtein(s1, s2)
  d = {}
  (0..s1.size).each { |row| d[[row, 0]] = row }
  (0..s2.size).each { |col| d[[0, col]] = col }
  (1..s1.size).each do |i|
    (1..s2.size).each do |j|
      i1,j1 = i-1,j-1
      cost = s1[i1] != s2[j1] ? 1 : 0
      d[[i, j]] = [d[[i1, j1]] + cost,
                   d[[i1, j]] + 1,
                   d[[i, j1]] + 1].min
      if i1*j1 &amp;gt; 0 and s1[i1] == s2[j-2] and s1[i-2] == s2[j1]
        d[[i, j]] = [d[[i,j]] , d[[i-2, j-2]] + cost].min
      end
    end
  end
  d[[s1.size, s2.size]]
end
&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/eciyOfrGmJM" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/25/our-daily-method-14-kernel-levenshtein</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Remco van 't Veer</name>
    </author>
    <id>urn:uuid:05067914-2734-45e9-8d04-e09bd9c08963</id>
    <issued>2008-02-23T12:21:00+01:00</issued>
    <modified>2009-09-04T19:25:36+02:00</modified>
    <title>Breaking out of Rails MVC with Mongrel handlers</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/Kf_ofiYUMxk/breaking-out-of-rails-mvc-with-mongrel-handlers" rel="alternate" type="text/html" />
    <dc:subject>ruby on rails</dc:subject>
    <dc:subject>english</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;&lt;img src="/files/mongrel.jpg" style="margin: 0 0 1em 1em; float: right;" alt="" /&gt; Module View Controller is a nice concept but sometimes you need te be very creative to fit a problem in that mold.  In such cases a more lowlevel approach can be much more effective.   The case in ran into recently involves streaming audio from a Rails application.  I wanted to recode and stream &lt;acronym title="Moving Picture Experts Group Audio Layer 3"&gt;MP3&lt;/acronym&gt; files over &lt;acronym title="HyperText Transfer Protocol"&gt;HTTP&lt;/acronym&gt;.  There&amp;#8217;s not much &lt;acronym title="Module View Controller"&gt;MVC&lt;/acronym&gt; about that, there&amp;#8217;s nothing to view!&lt;/p&gt;


	&lt;p&gt;A memory refresher; what is &lt;a href="http://mongrel.rubyforge.org/"&gt;Mongrel&lt;/a&gt;?  Mongrel is a web server (mostly) written in Ruby.  Ruby version 1.8 comes with a web server named &lt;a href="http://www.webrick.org/"&gt;WEBrick&lt;/a&gt; but its performance is very limited and not useable for public production web applications.  Mongrel, on the other hand, is more like a hybrid between a greyhound and a poodle, fast and tough.  The little beast is multi-threaded and can easily handle hundreds of concurrent requests.&lt;/p&gt;&lt;a href="/articles/2008/02/23/breaking-out-of-rails-mvc-with-mongrel-handlers"&gt;Lees verder...&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/Kf_ofiYUMxk" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/23/breaking-out-of-rails-mvc-with-mongrel-handlers</feedburner:origLink></entry>
  <entry>
    <author>
      <name>Remco van 't Veer</name>
    </author>
    <id>urn:uuid:2783d649-bc50-4ff2-bf05-f074c3b4eb35</id>
    <issued>2008-02-22T09:00:00+01:00</issued>
    <modified>2009-07-20T17:36:46+02:00</modified>
    <title>Our Daily Method #14: Time#warp</title>
    <link href="http://feedproxy.google.com/~r/rubyenrails_en/~3/sGBxzC-a4xk/our-daily-method-14-time-warp" rel="alternate" type="text/html" />
    <dc:subject>ruby</dc:subject>
    <dc:subject>english</dc:subject>
    <dc:subject>daily_method</dc:subject>
    <content mode="escaped" type="text/html">&lt;p&gt;Time travel can be very convenient but unfortunately it isn&amp;#8217;t very easy.  Reality won&amp;#8217;t allow it but we can bend the &lt;code&gt;Time&lt;/code&gt; class!&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Time
  def self.now_with_warping
    @warptime || now_without_warping
  end

  class &amp;lt;&amp;lt; self
    alias_method :now_without_warping, :now
    alias_method :now, :now_with_warping
  end

  def warp
    self.class.instance_variable_set('@warptime', self)
    yield
  ensure
    self.class.instance_variable_set('@warptime', nil)
  end
end
&lt;/code&gt;&lt;/pre&gt;

Now we can travel back to  &amp;#8220;Unix Epoch&amp;#8221;:
&lt;pre&gt;&lt;code&gt;
Time.at(0).warp do
  puts "The current time is: #{Time.now}" 
end
&lt;/code&gt;&lt;/pre&gt;

Or just before the end of time as we know it:
&lt;pre&gt;&lt;code&gt;
Time.at(2 ** 31 - 1).warp do
  Time.now + 1
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;What&amp;#8217;s the use?  It makes testing time dependent code very easy!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/rubyenrails_en/~4/sGBxzC-a4xk" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://blog.rubyenrails.nl/articles/2008/02/22/our-daily-method-14-time-warp</feedburner:origLink></entry>
</feed>
