I need a class that acts like a Hash, though not necessarily with all the Hash methods. I've read that it is not a good idea to subclass core classes like Hash. Whether or not that is true, what is the best practice for doing this kind of thing?
# (a) subclass Hash, add new methods and instance variables
class Book < Hash
def reindex
@index = .....
end
end
# (b) create a new class from scratch, containing a hash,
# and define needed methods for the contained hash
class Book
def initialize(hash)
@data = hash
end
def []=(k,v)
@data[k] = v
end
# etc....
def reindex
@index = ....
end
# (c) like (b) but using method_missing
# (d) like (b) but using delegation
I realize that Ruby has more than one way to accomplish a given task, but are there any general rules for which of the above methods are preferable in a relatively simple case?