I wrote a ruby program to read a list of graph edges and produce
an adjacency list using a Hash. The graph edges look like
p12 -- p31
I hoped to write this as

nbs = Hash.new([])
while line = gets
  if line =~ /p(\d+) -- p(\d+)/
    u, v = $1.to_i, $2.to_i
    nbs[u] << v
    nbs[v] << u
  end
end

but unfortunately, the default value [] for the Hash is shared by all
elements, rather than copied, as I expected.

I was wondering if this was a conscious design decision, or an oversight.
I finally ended up with the much uglier

nbs = Hash.new()
while line = gets
  if line =~ /p(\d+) -- p(\d+)/
    u, v = $1.to_i, $2.to_i
    nbs[u] = [] unless nbs.has_key?(u)
    nbs[u] << v
    nbs[v] = [] unless nbs.has_key?(v)
    nbs[v] << u
  end
end

Is there another way to avoid the explicit tests?

regards,

%!PS                       %  -John Tromp (http://www.cwi.nl/~tromp/)
42 42 scale 7 9 translate .07 setlinewidth .5 setgray/c{arc clip fill
setgray}def 1 0 0 42 1 0 c 0 1 1{0 3 3 90 270 arc 0 0 6 0 -3 3 90 270
arcn 270 90 c -2 2 4{-6 moveto 0 12 rlineto}for -5 2 5{-3 exch moveto
9 0 rlineto}for stroke 0 0 3 1 1 0 c 180 rotate initclip}for showpage