From: Luis Parravicini Date: 2010-05-16T00:31:04+09:00 Subject: Re: class best way for getters ? On Sat, May 15, 2010 at 12:27 PM, Ben Bleything wrote: > 2010/5/15 Une Bévue : >> def [a_key] >>  return nil unless @attr.keys.include?(a_key) >>  return @attr[a_key] >> end > >  def []( a_key ) >    return @attr[a_key] >  end > > but yes, that's a good option. By default hashes return nil when you > dereference a key that doesn't exist, so unless you've changed that > (by creating it with Hash.new(...) or similar) then you can leave that > first line out. > > Ben You can also remove the "return" and leave it like this: def [](key) @attr[key] end -- Luis Parravicini http://ktulu.com.ar/blog/