[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Undocumented behavior related to Hash::default_proc

Carl

4/18/2005 3:51:00 PM

Hello,

I found a behavior in Ruby 1.8.2 in using Hashes with a default_proc.
Here is what I did:

def initialize
@var = Hash.new { |hash,key| hash[key] = Array.new }
...
elements.each { |element|
...
@var[key].push element
...
}
...

I like that I can make this Hash autovivifying (somewhat like Perl) for
this initial loop. It made the loop more concise. However, I also
like that in Ruby, I can 'turn it off' when I'm done. After the above
iterator executes I do this:

@var.default = nil

At least in Ruby 1.8.2 this line makes the default_proc effectively go
away.

The problem is that I cannot find any documentation for Hash that
explicitly states this as the behavior of hash.default = nil.

My fear is that someday down the road this undocumented behavior will
go away or change. Since Hash does not implement default_proc= to
explicitly assign the default_proc this is the only way to accomplish
what I want to do. I would recommend one of the following:

1 - Document this behavior and keep it for good.
2 - Implement default_proc= and allow hash.default_proc = nil to
accomplish this.
3 - Come up with a better way to allow 'turning off' the default_proc
and document it.

Thoughts?

Thank you,
Carl