I wrote a gem to handle this kind of situation... have a look at
http://valuable.mustmodify.com/

class HFSFile < Valuable
  has_value :name
end

would create a getter, setter, and accept an attributes hash like...

HFSFile.new(:name => 'fu')

or just

file = HFSFile.new
>> file.name
=> nil

unless you want to specify a default...

class HFSFile < Valuable
  has_value :name, :default => 'anything'
end

>> file = HSFile.new
>> file.name
=> 'anything'


jw

On May 15, 9:42  󮮮 ue)
wrote:
> i have a class "HFSFile" initialized by a parsed string
> after initialization i get a Hash @attr with keys like :name,
> :creation_date etc...
>
> what is the best way to write a getter for each individual key ?
>
> the obvious would be for example :
>
> def name
> @attr[:name]
> end
>
> or does exist a more clever way to get all of those getters ?
> (same name for the getter that the key symbol)
>
> another way :
>
> def [a_key]
> eturn nil unless @attr.keys.include?(a_key)
> eturn @attr[a_key]
> end
>
> something like that ?
>
> --
>  Je suis s que la gaambigudu dre de garn qui a bu du
> whisky et fait le zre sur un vieux caan No dans le can, a > bique l'e du poe ble, ainsi qu'son cr & cera ! 
> Jean-Paul Blanc)