I want to be able to add and retrieve elements to a hash via getters and setters. Here is my class
class Config
attr_reader :p1, :p2, :p4
def initialize(options={})
options.each do |k,v|
instance_variable_set("@#{k}", v)
end
end
end
Currently I am only able to add and retrieve elements with keys as p1, p2 and p3
If I try adding new elements e.g
> a = Config.new({name: "kevin"})
return value is
=> #<Config:0x000000000a4422a8 @name="kevin">
on trying to access the name
> a.name
NoMethodError: undefined method `name' for #<Config:0x000000000a4422a8 @name="kevin">
How can I access the name value with
a.name to get "Kevin"
If you only want a data struct storing like hash, maybe you can try
OpenStructFor example:
Than you can do this
For more detail about
OpenStruct, you can checkout here