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 =3D> 'fu')

or just

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

unless you want to specify a default...

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

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


jw

On May 15, 9:42=A0am, unbewusst.s... / google.com.invalid (Une B=E9vue)
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
> =A0 @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]
> =A0return nil unless @attr.keys.include?(a_key)
> =A0return @attr[a_key]
> end
>
> something like that ?
>
> --
> =A0=AB Je suis s=FBr que la ga=EEt=E9 ambigu=EB du dr=F4le de gar=E7on qu=
i a bu du
> whisky et fait le z=E8bre sur un vieux ca=EFman =E0 No=EBl dans le ca=F1o=
n, a =E9t=E9
> b=E9n=E9fique =E0 l'=E2me du po=E8te bl=EAme, ainsi qu'=E0 son c=BDur & c=
=E6tera ! =BB
> =A0(=A9 Jean-Paul Blanc)