From: Carl Youngblood Date: 2002-11-04T03:56:29+09:00 Subject: Extending Hash I'm trying to create a modified version of a Hash that, if it doesn't contain anything for a given key, will return whatever element it has that most closely matches the key. Here's what I've done so far: ----------------------------------- class FragHash < Hash def [](key) index = key.dup while !self.has_key?(index) and index.chop! != nil end return self.default if index.empty? return fetch(index) end end ----------------------------------- so if I do the following: ----------------------------------- h = FragHash.new h['t'] = 23 puts h['test'] ----------------------------------- the output is 23. In other words, it didn't find the index "test" but it kept shortening the index until it found something that matches. The only problem I have now is that I want to make it so that when I call each() it also enumerates through the default value. However, I don't know how to extend the each function (I'm still a little hazy on how to overload functions that use blocks). It would be nice if I could look at the source for the Hash class itself. Does anyone know where I can find it? Does anyone have any suggestions for how I might modify the each function? Thanks, Carl Youngblood