I have a flat Hash:
hash = Hash["prop_one" => 100, "prop_two" => 200, "prop_three" => 300]
I have wrapped it in a class, with the class exposing these values to be read through accessors:
class WrappedHash
def prop_one
config['prop_one']
end
def prop_two
config['prop_two']
end
def prop_three
config['prop_three']
end
def initialize(config)
self.config = config
end
end
Is there a way to proxy requests for these properties to the hash without having to manually add accessors? (It’s a big hash)
use OpenStruct if you have dynamic hash keys or Struct if keys are static and performance is important.
OpenStruct can be converted back to Hash by its
marshal_dumpmethod