I am trying to write a class which acts as an Array (i.e inherits the methods from the Array class) and as a Hash (i.e inherits the methods from the Hash class), based on the object passed to it.
class MyClass
end
class_a = MyClass[1,2,3,4,5,6,7,8,9,10] # => Acts like an Array
class_a[1] # => 2
class_h = MyClass[:a => 1, :b => 2] # => Acts like a Hash
class_h.key(1) #=> :a
I know Ruby doe not really support multiple inheritance. How can I accomplish this? Help appreciated.
Edit
Use case:
Lets say I have a class which performs some 'special' functions on a hash (transforms the hash based on criteria, does a hash lookup of some sort sort, etc)
I would write the class like this:
class MyClass < Hash
def some_function
code
end
end
and use it like so
hash = MyClass[:a => 1, :b => 2, :c => 3].some_function
But a small problem arises, when I do something like this
hash = MyClass[[
{:a => 1, :b => 2},
{:a => 2, :b => 3},
{:a => 3, :b => 3}
]].some_function
In the case above, I would need to iterate over the array, and call some_function and each hash.