From: Yann Klis Date: 2005-08-12T06:31:52+09:00 Subject: Re: Dynamically added methods In fact, I'd like to mimic the behaviour of ActiveRecord. If I have something like this: class Base def initialize(attributes) #do something clever with attributes end end class MyObject < Base end I'd like to be able to do that: object = MyObject.new({"attribute1" => "value1", "attribute2" => "value2"}) val = object.attribute1 Thanks, yk Le vendredi 12 août 2005 à 05:29 +0900, Shalev NessAiver a écrit : > To clarify - you want to be able to do something like this: > > class YourObject > > def initialize > @myhash = {'key1' => 'value1', 'key2' => 'value2', 'key3' => > 'value3'} > end > > end > > YourObject.new.key1 # Returns 'value1' > YourObject.new.key2 # Returns 'value2' > > > > .... like that? > > -Shalev > > > On Aug 11, 2005, at 1:48 PM, Yann Klis wrote: > > > Hi all, > > > > I'm pretty new to ruby so I will ask some dumb questions, sorry if > > it's > > not the right place to ask :) > > > > I'd like to add some methods dynamically to one of my object. > > I understand that with "method_missing" I can "emulate" this > > behaviour. > > > > However, imagine I have an hash table. The key would be the attributes > > of my object, and the keys their value. How could I add dynamically > > accessors for those attributes/keys ? > > > > Thanks in advance, > > > > ++ > > > > yk > > > > > > > >