Aug
29

Surprising element in design

on August 29th, 2009 by admin

Ruby language design is based on the principle of least surprise. Don’t quote Matz on this line, it was not him actually who brought this up. But, true, Ruby is supposed to reduce your frustration while developing.

I was actually surprised yesterday that I encountered a frustrating situation.

Look at the following code:

hn = Hash.new(0)
hn['key'] += 1
puts hn['key']
# you expect 1 and it works
puts
ha = Hash.new([])
ha['1'] << '1'
ha['2'] << '2'
puts ha['1']
# you expect 1, but you get 1, 2
puts
puts ha['3']
# you expect nil, but the output is 1, 2

Why is this? How come, specifying an array as default value for a hash is not doing the right thing? The documentation tells, that the same single object is used to initialize the hash values, in case the hash key does not exist. So, the initial value for all hash keys becomes the same single array object. I bet that reading the source code only, most programmers assume that a new Array object gets instantiated for every new hash key.

Let’s fix it!

We need to use the default_proc method on the hash.

ha = Hash.new
ha.default_proc = lambda {|hash, key| hash[key] = []}
ha['1'] << '1'
ha['2'] << '2'
puts ha['1']
# you expect 1 and it works
puts
puts ha['3']
# you expect nil and it works

Ruby is awesome, but not always follows principle of least surprise. Now I understand why Matz did not say it himself ;)

Related Posts

  • Ruby 1.9.1 character encoding, Regular Expressions and Ruby on Rails
  • A smart Winamp play-list - the implementation
  • wxRuby application with Ruby threads
  • Compiling Ruby with MinGW
  • Ruby 1.9 debugger
Tags: programming, ruby
english - RSS Feed

Leave a Reply

Click here to cancel reply.

CAPTCHA Image
Refresh Image

    Recent Posts

    • GPU with CUDA
    • First impressions of the Mac OS
    • Snow Leopard in VMWare
    • CentOS 5 under Windows Virtual PC
    • Turek Miklós esküvőjére
    • Have I told you lately that I like Windows 7?
    • Királynő a sakktáblán
    • TrueIP under Windows 7
    • Virtualdub capture headache
    • Endless Summer

    Archives

    • September 2010 (1)
    • August 2010 (7)
    • July 2010 (2)
    • June 2010 (1)
    • April 2010 (2)
    • March 2010 (1)
    • January 2010 (2)
    • December 2009 (3)
    • November 2009 (6)
    • October 2009 (6)
    • September 2009 (8)
    • August 2009 (14)

    Categories

    • english (37)
    • magyar (16)

    életmód autó cloud fényképészet food gondolatok hírek humor ideas lesson learnt lifestyle logic logika matematika math mese movie music photography product review programming ruby science social software történelem television thoughts travel tudomány utazás video web zene

    WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

    Blogroll

    • James Chik
    • Klement Vilmos

    Links

    • Home page
    • Photography

    Meta

    • Log in
Blog software by WordPress
Theme based on blue-fade by Web Considerations, LLC
Entries (RSS) and Comments (RSS)
36 queries. 0.242