From: davekam@... (Dave) Date: 2003-04-24T08:04:28+09:00 Subject: two oddities in ruby 1.8.0p2 OK, recently a cool feature has been added to ruby to start assigning immediately to a hash of hashes, hash of arrays, etc. as follows: x = Hash.new{|a,b| a[b] = Hash.new} y = Hash.new{|a,b| a[b] = Array.new} x['a']['b'] = 'hello' # -> {"a"=>{"b"=>"hello"}} y['a'][3] = 100 # -> {"a"=>[nil, nil, nil, 100]} However, this is not possible for arrays, so we cannot do the following for arrays of hashes or arrays of arrays: x = Array.new{|a,b| a[b] = Hash.new} y = Array.new{|a,b| a[b] = Array.new} I'm hoping this is just an oversight, because it would be useful. Comments? Another oddity is one that I think has been around for a while, but I haven't seen it discussed before (correct me if I'm wrong -- I'm new to ruby). It's the infamous local variable scope issue. In the discussions I've read, methods are supposed to define a scope for local variables. Blocks within methods also have their own scope, if the variables have not been used already. BUT control structures like if-else, for, while, etc. are NOT supposed to define their own scope. How, then, to explain this example? i = 0 while i < 10 puts x if defined? x x = 10 i += 1 end Nothing is outputted. The value of x is indeed local to the while loop; it becomes undefined again each time the loop restarts. On the other hand, if x is first defined outside the loop, this does not occur. I thought a while loop was supposed to act differently from a block? Please enlighten me, Dave