From: Christoph Date: 2005-05-18T21:22:38+09:00 Subject: Re: Noobie ... Simple Inheriting from Hash Question .. David A. Black schrieb: > > This will work but also suffers from the usual problem with extending > core classes -- namely, it's unsafe to do unless you're sure > that your code will run in isolation. > > Another possibility is to add the behavior on a per-object basis: > > module MyHashStuff > def foo > # ... > end > end > > h = {1,2,3,4} > h.extend(MyHashStuff) > h.foo # h now has the food method Jet another possibility (it's a pretty rare animal, I've never seen in the wild) is to clone a core class and alter the clone - For example, it should be straight forward to reimplement the Set class as a clone of the Hash class. --- Set = Hash.clone class Set # aliasing stuff is half the rent ... alias :each , :each_key alias :include? , :is_key? private # a couple of original method are be private. alias :_implement_add, , :store # throw out superfluous original methods ['[]', '[]=',:each_key,:each_value, :store,:is_key? ... ].each {|m| remove_method(m) } public # build the methods def add(o) _implement_add(o,true) end ... end --- Note I am not advocating a rewrite of the Set class (if at all it it should be a c-extension), it just makes a poster child example for this idiom. /Christoph