Stefan Tilkov's Random Stuff

Hello World and Fibonacci

Tim Bray makes two points about language books. The first one ('In any such book, the first example program should print the string “Hello world.”') is of course absolutely correct. The second one is perfectly addressed by DeWitt Clinton – what I like best is this gem from the David Madden's comment:

fib = Hash.new{ |h, n| n < 2 ? h[n] = n : h[n] = h[n - 1] + h[n - 2] }
puts fib[15]

(For the none-Rubyists among you: the Hash constructor accepts a block that will be called when a lookup is performed for a key that has no value, in this case storing the value and then returning it – a simple write-through-cache.)