From: Robert Klemme Date: 2005-08-12T16:56:11+09:00 Subject: Re: Dynamically added methods Yann Klis wrote: > 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 As others have pointed out already, use OpenStruct: 09:51:10 [robert.klemme]: irbs >> require 'ostruct' => true >> object = OpenStruct.new( "attribute1" => "value1", "attribute2" => ?> "value2") => >> object.attribute1 => "value1" >> object.attribute2 => "value2" If you need inheritance, you can do class MyObject < OpenStruct end That's it. If you need additional initialization, you'll have to do class MyObject < OpenStruct def initialize(attrs) super(attrs) # do more init end end Kind regards robert