Rather than an instance variable per key, which requires some unnecessarily bulky code, why not just a single Hash like below. Also, define_method and define_singleton_method can be your friend to avoid bad bad evals.
class MyStorageClass
def initialize
@data = {}
end
def add_entry(key, value)
(@data[key] ||= []) << value
define_singleton_method(key){ @data[key] }
end
def get_entry(key)
@data.key?(key) or raise NoMethodError
@data[key]
end
end
You may want to check that you're not overriding a predefined method first ([email protected]?(key) && self.respond_to?(key) at the top of the add_entry method would do), but that's for another conversation. Could be bad if someone tried to add a key called inspect, class, or, oh, get_entry, for example!