<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>loopkid: Category Ruby</title>
    <link>http://loopkid.net/articles/category/ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>sad songs make me happy</description>
    <item>
      <title>ruby 1.9 utf-8 mostly works</title>
      <description>&lt;p&gt;I was curious to see how far the implementation of utf-8 in Ruby 1.9 has developed. First we assign a pure Ascii string and check it&amp;#8217;s encoding.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; a = "Restaurant"
=&amp;gt; "Restaurant"
&amp;gt;&amp;gt; a.encoding
=&amp;gt; #&amp;lt;Encoding:US-ASCII&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Okay, nothing spectacular so far. Next we take a Unicode string.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b = "Café"
=&amp;gt; "Café"
&amp;gt;&amp;gt; b.encoding
=&amp;gt; #&amp;lt;Encoding:UTF-8&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looking great, now let&amp;#8217;s work with that string.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b.each_byte {|byte| puts byte}
67
97
102
195
169
=&amp;gt; "Café"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we see the utf-8 encoded é as two bytes.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b.each_char {|char| puts char}
C
a
f
é
=&amp;gt; "Café"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Works as expected.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b.size
=&amp;gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Ruby 1.8 this would have returned 5.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b.reverse
=&amp;gt; "éfaC"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Ruby 1.8 this would have generated a broken character at the start.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b.chop
=&amp;gt; "Caf"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Ruby 1.8 this would have generated a broken character at the end.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; b.upcase
=&amp;gt; "CAFé"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is where work is still needed! In Ruby 1.8 there is Nikolai Weibull&amp;#8217;s &lt;a href="http://bitwi.se/software/ruby/character-encodings/"&gt;Ruby Character Encodings Library&lt;/a&gt; that does the job.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; require 'encoding/character/utf-8'
=&amp;gt; true
&amp;gt;&amp;gt; b = "Café"
=&amp;gt; "Caf\303\251"
&amp;gt;&amp;gt; b.length
=&amp;gt; 5
&amp;gt;&amp;gt; b = u"Café"
=&amp;gt; u"Caf\303\251"
&amp;gt;&amp;gt; b.length
=&amp;gt; 4
&amp;gt;&amp;gt; b = +"Café" 
=&amp;gt; u"Caf\303\251"
&amp;gt;&amp;gt; b.length 
=&amp;gt; 4
&amp;gt;&amp;gt; puts b.upcase
CAFÉ
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The library however is &lt;a href="http://rubyforge.org/tracker/index.php?func=detail&amp;amp;aid=21150&amp;amp;group_id=1982&amp;amp;atid=7741"&gt;not compatible&lt;/a&gt; with Ruby 1.9. Another outstanding issue is the sorting of arrays containing Unicode strings.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; a = %w[ä a b c]
=&amp;gt; ["ä", "a", "b", "c"]
&amp;gt;&amp;gt; a.sort
=&amp;gt; ["a", "b", "c", "ä"]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The result would be okay, if I wanted swedish sorting order, but what if I wanted german sorting order? This needs to be addressed. There are libraries for this in Java, so Ruby shouldn&amp;#8217;t stand behind!&lt;/p&gt;</description>
      <pubDate>Mon, 07 Jul 2008 21:40:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b09c8f6a-3bc4-4199-93ad-86d2097786ce</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/07/07/ruby-1-9-utf-8-mostly-works</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8576</trackback:ping>
    </item>
    <item>
      <title>predefined character classes in grep</title>
      <description>&lt;blockquote&gt;
    &lt;p&gt;Many regex implementations have &amp;#8220;macros&amp;#8221; for various character classes. In Perl, for example, \d matches any digit ([0-9]) and \w matches any &amp;#8220;word character&amp;#8221; ([a-zA-Z0-9_]). Grep uses a slightly different notation for the same thing: [:digit:] for digits and [:alnum:] for alphanumeric characters. (&lt;a href="http://www.bsd.org/regexintro.html"&gt;BSD&lt;/a&gt;)&lt;/p&gt;
    
    &lt;p&gt;Finally,  certain  named classes of characters are predefined within bracket expressions, as follows. Their names are self explanatory, and they are [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].  For example, [[:alnum:]] means [0-9A-Za-z]. (&lt;a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?grep"&gt;grep man page&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Was unter Ruby&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;line = "length 1450"
puts line if line =~ /\d{4}/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;heißt wird also unter der bash mit grep zu&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export line="length 1450"
echo $line | egrep [[:digit:]]{4}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;was zwar die regulären Ausdrücke unnötig aufbläht, aber immerhin die gleiche Funktionalität zur Verfügung stellt.&lt;/p&gt;</description>
      <pubDate>Sun, 06 Jul 2008 15:54:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b2ca6c21-592f-47f8-a7b7-d788580bd74f</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/07/06/predefined-character-classes-in-grep</link>
      <category>Mac OS X</category>
      <category>Linux</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8551</trackback:ping>
    </item>
    <item>
      <title>conciseness vs. readability</title>
      <description>&lt;p&gt;To print the contents of all files passed as arguments or in case of no arguments print the contents of stdin you could write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts *ARGF
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but isn&amp;#8217;t&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if ARGV.empty?
  puts $stdin.readlines
else
  ARGV.each do |filename|
    puts File.readlines(filename)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;much more readable?&lt;/p&gt;</description>
      <pubDate>Tue, 01 Jul 2008 03:21:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:4efe33e5-3818-402c-bc07-0189df6c3213</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/07/01/conciseness-vs-readability</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8472</trackback:ping>
    </item>
    <item>
      <title>read file with one line of code</title>
      <description>&lt;p&gt;There are probably at least a have a dozen ways to print the contents of a file in Ruby, here I will present four ways of doing it.&lt;/p&gt;

&lt;p&gt;The first method is rather verbose. Assign a file object and iterate on the file object with the &lt;code&gt;each_line&lt;/code&gt; method and then close the file.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;f = open('file.txt')
f.each_line do |line|
  puts line
end
f.close
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The second method still assigns a file object, but then uses the more compact &lt;code&gt;readlines&lt;/code&gt; instance method and closes the file.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;f = open('file.txt')
puts f.readlines
f.close
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The third method uses block form to access the file and automatically close it.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;open('file.txt') { |f| puts f.readlines }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The fourth method calls the class method &lt;code&gt;readlines&lt;/code&gt; to wrap it all up in one statement.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts File.readlines('file.txt')
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Tue, 01 Jul 2008 01:58:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:760b092c-1d98-4618-b7fa-cea31ac3ff8f</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/07/01/read-file-with-one-line-of-code</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8471</trackback:ping>
    </item>
    <item>
      <title>the beauty of ruby 1.9</title>
      <description>&lt;p&gt;Let&amp;#8217;s say we have a string and three arrays&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string = "672"
java = []
ruby18 = []
ruby19 = []
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you want to convert the string to an array of integers. If you were writing Ruby in javastyle you would probably write something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;i=0
digits = string.split(//)
while i &amp;lt; digits.size do
  java &amp;lt;&amp;lt; digits[i].to_i
  i+=1
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but since we&amp;#8217;re we&amp;#8217;re doing Ruby in rubystyle it looks more like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string.split(//).each { |digit| ruby18 &amp;lt;&amp;lt; digit.to_i }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which looks &lt;em&gt;much&lt;/em&gt; nicer, but still not quite right, but with Ruby 1.9&amp;#8217;s &lt;a href="http://www.ruby-doc.org/core/classes/String.html#M000777"&gt;&lt;code&gt;chars&lt;/code&gt;&lt;/a&gt; interator it gets even better.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string.chars { |digit| ruby19 &amp;lt;&amp;lt; digit.to_i }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Goes down like butter, doesn&amp;#8217;t it? Isn&amp;#8217;t that how it always should have been? And the good thing is, there is lots of this good stuff in Ruby 1.9. Maybe in Ruby 2.0 there will even be a &lt;code&gt;to_i&lt;/code&gt; method in the array class, but now I&amp;#8217;m getting carried away.&lt;/p&gt;</description>
      <pubDate>Sat, 28 Jun 2008 12:11:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f8db2c61-51ea-4fa3-9032-29377f0ed221</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/28/the-beauty-of-ruby-1-9</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8426</trackback:ping>
    </item>
    <item>
      <title>ordered hashes in ruby 1.8</title>
      <description>&lt;p&gt;Ruby 1.8 doesn&amp;#8217;t offer builtin ordered hashes, but there are several libraries out there which extend or replace Ruby&amp;#8217;s Hash class. Among them are &lt;a href="http://raa.ruby-lang.org/project/ruby-rbtree/"&gt;Ruby/RBTree&lt;/a&gt;, Ara T. Howard&amp;#8217;s &lt;a href="http://codeforpeople.com/lib/ruby/orderedhash/"&gt;orderedhash&lt;/a&gt; and &lt;a href="http://facets.rubyforge.org/"&gt;Ruby Facets&lt;/a&gt;. All three of these offer a hashlike syntax, letting you do things like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ordered_hash = Library['a', 1, 'b' ,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;resulting in&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{'a'=&amp;gt;1, 'b'=&amp;gt;2}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sadly all three of these libraries have poor documentation. The one with most bearable in my eyes is Ruby Facet&amp;#8217;s &lt;a href="http://facets.rubyforge.org/doc/api/more/classes/Dictionary.html"&gt;Dictionary&lt;/a&gt;, which also has a RubyGem available, so let&amp;#8217;s have a look at it.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ sudo gem install facets
Successfully installed facets-2.4.1
1 gem installed
Installing ri documentation for facets-2.4.1...
Installing RDoc documentation for facets-2.4.1...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ruby Facets is a rather large library, so you&amp;#8217;ll want to require just the Dictionary class.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; require 'rubygems'
&amp;gt;&amp;gt; require 'facets/dictionary'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;#8217;s make an ordered hash with five elements and print it out.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; dictionary = Dictionary[*(1..10)]
&amp;gt;&amp;gt; dictionary.each {|k,v| puts "#{k}=&amp;gt;#{v}"}
1=&amp;gt;2
3=&amp;gt;4
5=&amp;gt;6
7=&amp;gt;8
9=&amp;gt;10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Works fine. Now for the bonus round. Dictionary can also be used as a sorted hash by calling the &lt;code&gt;order_by&lt;/code&gt; or more conveniently the &lt;code&gt;order_by_key&lt;/code&gt; method. Once called this method ensures that all previously and subsequently inserted keys will be in sorting order.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; array = Array.new(10){rand(100)}
&amp;gt;&amp;gt; dictionary = Dictionary[*array]
=&amp;gt; {1=&amp;gt;49, 48=&amp;gt;97, 17=&amp;gt;66, 6=&amp;gt;70, 52=&amp;gt;77}
&amp;gt;&amp;gt; dictionary.order_by_key
&amp;gt;&amp;gt; dictionary.each {|k,v| puts "#{k}=&amp;gt;#{v}"}
1=&amp;gt;49
6=&amp;gt;70
17=&amp;gt;66
48=&amp;gt;97
52=&amp;gt;77
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Thu, 19 Jun 2008 23:51:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:4b9991c1-e0e3-473c-84ad-158126846bf1</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/19/ordered-hashes-in-ruby-1-8</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8237</trackback:ping>
    </item>
    <item>
      <title>ordered hashes in ruby 1.9</title>
      <description>&lt;p&gt;If you create a hash in Ruby 1.8 and then browse it with the each-iterator you get the keys delivered in abitrary order.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; hash = {:a=&amp;gt;1, :b=&amp;gt;2, :c=&amp;gt;3,}
&amp;gt;&amp;gt; hash.each {|key,value| puts "#{key}=&amp;gt;#{value}"}
c=&amp;gt;3
b=&amp;gt;2
a=&amp;gt;1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This might be okay for some cases, but in other cases you might want the insertation order preserved. Well, good news in Ruby 1.9 this works.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; hash = {:a=&amp;gt;1, :b=&amp;gt;2, :c=&amp;gt;3,}
&amp;gt;&amp;gt; hash.each {|key,value| puts "#{key}=&amp;gt;#{value}"}
a=&amp;gt;1
b=&amp;gt;2
c=&amp;gt;3
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Thu, 19 Jun 2008 00:51:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:7c711bc6-7e24-42e3-97b3-e5ea3a035ed4</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/19/ordered-hashes-in-ruby-1-9</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8216</trackback:ping>
    </item>
    <item>
      <title>slow iterators in ruby 1.9 on mac</title>
      <description>&lt;p&gt;If you ever wondered why your Ruby programs are not faster but much slower with Ruby 1.9 you might be using &lt;code&gt;each&lt;/code&gt; or &lt;code&gt;times&lt;/code&gt; iterators on large collections.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;10000000.times {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In fact if you do a benchmark,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'benchmark'

n = 10000000

Benchmark.bm do |x|
  x.report { n.times {} }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;you get stunning results.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ruby bench.rb
   user     system      total        real
   0.720000   0.000000   0.720000 (  0.721872)
$ ruby1.9 bench.rb 
   user     system      total        real
   16.670000   9.110000  25.780000 ( 25.923977)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When increasing the number of iterations by powers of ten it looks like the computational complexity of the &lt;code&gt;times&lt;/code&gt; iterator is O(n) in Ruby 1.9. According to &lt;a href="http://antoniocangiano.com/2008/03/25/inject-each-and-times-methods-much-slower-in-ruby-19/"&gt;Antonio Cangiano&lt;/a&gt; this behaviour is specific to Mac OS X and does not appear on Linux.&lt;/p&gt;</description>
      <pubDate>Tue, 17 Jun 2008 22:45:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:ae74e534-29d8-4b96-9562-7bd1f26e3824</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/17/slow-iterators-in-ruby-1-9-on-mac</link>
      <category>English</category>
      <category>Mac OS X</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8201</trackback:ping>
    </item>
    <item>
      <title>nested hashes with default value</title>
      <description>&lt;p&gt;To instantiate arbitrarily nested hashes in Ruby you can write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h = Hash.new{|hash,key| hash[key] = Hash.new(&amp;amp;h.default_proc)}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This let&amp;#8217;s you do&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h[1] = 2 # =&amp;gt; 2
h[2][1] = 3 # =&amp;gt; 3
h[3][1][1] = 4 # =&amp;gt; 4
h.inspect # =&amp;gt; {1=&amp;gt;2, 2=&amp;gt;{1=&amp;gt;3}, 3=&amp;gt;{1=&amp;gt;{1=&amp;gt;4}}}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s nice, but maybe you want your innermost hash to have a default value to access the keys via &lt;code&gt;+=&lt;/code&gt;-operator without having to check if the key has been initialized before? You might be tempted to write the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h = Hash.new(Hash.new(0))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks nice. Now let&amp;#8217;s assign a value and check if it&amp;#8217;s stored correctly.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h[1][1] = 2 # =&amp;gt; 2
h[2][2] = 3 # =&amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks fine too. Now let&amp;#8217;s inspect the hash.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h.inspect # =&amp;gt; {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Whoops! Where have my keys gone? Let&amp;#8217;s examine the inner hashes.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h[1].object_id # =&amp;gt; 1751410
h[2].object_id # =&amp;gt; 1751410
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Okay, so obviously instead of instantiating new hashes for each key the same hash was used. Let&amp;#8217;s examine the default value.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h.default.object_id # =&amp;gt; 1751410
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ah, so the keys were stored in the default value hash. Let&amp;#8217;s check and see.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h.default # =&amp;gt; {1=&amp;gt;2, 2=&amp;gt;3}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So instead of generating a nested structure for each key all keys are stored flatly in the innermost hash. That is not what we wanted. We can however talk the hash into doing the intented by using a default procedure instead of a default value, which is of course documentated in the lovely &lt;a href="http://ruby-doc.org/core-1.9/classes/Hash.html#M002708"&gt;core API documentation&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = 0}}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While working fine in Ruby 1.9, this causes problems in in Ruby 1.8 because block arguments are not local there.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = 0}} # =&amp;gt; {}
h[1][1] = 1 # =&amp;gt; 1
h.inspect # {1=&amp;gt;{1=&amp;gt;1}}
h[2][1] # =&amp;gt; 0
h.inspect # =&amp;gt; {1=&amp;gt;0}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To get it working in Ruby 1.8 the variables in each block have to be uniquely named.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h = Hash.new {|h,k| h[k] = Hash.new {|h1,k1| h1[k1] = 0}} # =&amp;gt; {}
h[1][1] = 1 # =&amp;gt; 1
h.inspect # {1=&amp;gt;{1=&amp;gt;1}}
h[2][1] # =&amp;gt; 0
h.inspect # {1=&amp;gt;{1=&amp;gt;1}, 2=&amp;gt;{1=&amp;gt;0}}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can fill the twodimensional hash with data and see the result.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;h = Hash.new {|h,k| h[k] = Hash.new {|h1,k1| h1[k1] = 0}} # =&amp;gt; {}
h[1][1] += 1 # =&amp;gt; 1
h[1][2] += 2 # =&amp;gt; 2
h[2][3] += 3 # =&amp;gt; 3
h.inspect # =&amp;gt; "{1=&amp;gt;{1=&amp;gt;1, 2=&amp;gt;2}, 2=&amp;gt;{3=&amp;gt;3}}"
h[1][3] # =&amp;gt; 0
h.inspect # =&amp;gt; {1=&amp;gt;{1=&amp;gt;3, 2=&amp;gt;4, 3=&amp;gt;0}, 2=&amp;gt;{1=&amp;gt;0, 3=&amp;gt;6}}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Et voilà!&lt;/p&gt;</description>
      <pubDate>Tue, 17 Jun 2008 04:02:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:34ee5e3e-cde2-4bac-8c5a-4ff74a339442</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/17/nested-hashes-with-default-value</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8200</trackback:ping>
    </item>
    <item>
      <title>installing ruby 1.9 on leopard</title>
      <description>&lt;p&gt;Thanks to &lt;a href="http://www.nabble.com/Ruby-1.9-port-td17494424.html"&gt;Caspar Florian Ebeling&lt;/a&gt; installing Ruby 1.9 on Mac OS 10.5 is now a breeze. Just install &lt;a href="http://www.macports.org/"&gt;MacPorts&lt;/a&gt; if you haven&amp;#8217;t already and then issue a&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo port install ruby19
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can launch Ruby 1.9 with &lt;code&gt;ruby1.9&lt;/code&gt; and irb with &lt;code&gt;irb1.9&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For the more adventurous souls among you here&amp;#8217;s how to compile it by hand. To compile Ruby 1.9 you need Readline 5.2, therefore it will be installed if it isn&amp;#8217;t already there. Commands with a trailing backslash are multiline statements. If you prefer to compile Readline 5.2 on Leopard by hand, you should have a look at &lt;a href="http://secretdiaryofhan.wordpress.com/2007/12/26/building-readline-52-on-os-x-leopard/"&gt;this&lt;/a&gt; patch.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo port install readline
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz
tar xvfz ruby-1.9.0-0.tar.gz
cd ruby-1.9.0-0
./configure --prefix=`echo ~`/ruby19 --program-suffix=1.9 \
--with-readline-dir=/opt/local
make
make install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now add &lt;code&gt;~/ruby19/bin&lt;/code&gt; to your &lt;code&gt;PATH&lt;/code&gt;-variable (for example in ~/.bash_profile or ~/.bashrc)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export PATH=~/ruby19/bin:$PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To activate the changes just launch a new terminal window. Now you can launch Ruby 1.9 with &lt;code&gt;ruby1.9&lt;/code&gt; and irb with &lt;code&gt;irb1.9&lt;/code&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 17 Jun 2008 02:38:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:efda5675-0230-4e79-97a1-03aed2639127</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/17/installing-ruby-1-9-on-leopard</link>
      <category>English</category>
      <category>Mac OS X</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8198</trackback:ping>
    </item>
  </channel>
</rss>
