From: Johnathon Wright Date: 2010-05-21T02:25:08+09:00 Subject: Re: class best way for getters ? 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 am, unbewusst.s...@google.com.invalid (Une Bévue) 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] >  return nil unless @attr.keys.include?(a_key) >  return @attr[a_key] > end > > something like that ? > > -- >  « Je suis sûr que la gaîté ambiguë du drôle de garçon qui a bu du > whisky et fait le zèbre sur un vieux caïman à Noël dans le cañon, a été > bénéfique à l'âme du poète blême, ainsi qu'à son c½ur & cætera ! » >  (© Jean-Paul Blanc)