From: hemant Date: 2007-03-28T22:06:56+09:00 Subject: Re: dynamically adding class variables On 3/28/07, Siemen Baader wrote: > Thanks very much for the answers Phrogz, Ara and Gary! > > I actually had it set up with class instance vars first, but they were > so longish to access (self.class.method) that I thought there had to be > a cleaner, more rubyish solution I was missing... > Well I have written this small bit of code, which makes use of class instance variable still more simpler: class Object def self.metaclass; class << self; self; end; end def self.iattr_accessor *args metaclass.instance_eval do attr_accessor *args end args.each do |attr| class_eval do define_method(attr) do self.class.send(attr) end end end end end This is how you use it: class Foobar iattr_accessor :foo end Foobar.foo = "blah" p Foobar.foo lol = Foobar.new p lol.foo > Also thanks for the google link, it was great help to me even though the > query is pretty obvious, at least after you see it .. :) > > As for the strange beavior af class variables: > > - It seems ok to me that they are singleton in the whole inheritance > tree; that's a design choice. > > - But: Is it intended behaviour that they can be seen by class_variables > but not used in class methods like in my example and here: > > ---> > http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html > > - siemen > > -- gnufied ----------- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary. http://people.inxsasia.com/~hemant