Olaf Klischat ha scritto: > Hi, > > yesterday I wanted to implement a hash "facade", i.e. a Hash that's > just a read-only view of some external data. That data may > (synchronously) change anytime, and the view should reflect that > immediately. > > This turned out to be surprisingly difficult: You can't just derive > from Hash, override some central methods like [] or each, and then get > all of Hash's functionality. That's because you don't know the > implementation details of Hash (which methods call which other > methods) (this appears to be a manifestation of the "fragile base > class problem"; see, for example, > http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html). > > If I had wanted to implement an *array* facade (read-only view) to > some external data, it would've been easy -- just implement "each" and > include Enumerable. One wishes for an equivalent to Enumerable for > "hashable" (or, more precisely, "mappable") things. > > So, this is the proposal: Provide a mixin named "Map" that implements > all of Hash's read-only functionality on top of methods > get_mapped_value(key) and each{|key,value|..}. I've included a sample > implementation of such a thing below. This implementation is quite > trivial, but please note (and maybe discuss) the caveat mentioned at > the beginning. I'd also wrap this up in a Rubygem and publish it on > Rubyforge. see the existing RCR, #277 IIRC. Maybe I should add your code (if you allow me to do it), but I'm out of time ATM :/ Anyway, I guess a C patch to current ruby would have more chance to be accepted :) Oh, and I definitely agree with you a Map mixin would be great :)