From: Drew Olson Date: 2007-10-26T07:45:26+09:00 Subject: Re: Need help understanding metaclassing Chris Czub wrote: > Any ideas? Basically the same idea as why's solution to dwenthy's array: class User def self.metaclass class << self; self; end end def self.attribs *args return @attribs if args.empty? attr_accessor *args args.each do |arg| metaclass.instance_eval do define_method(arg) do |val| @attribs ||= {} @attribs[arg] = val end end end class_eval do define_method(:initialize) do self.class.attribs.each do |attrib,val| instance_variable_set("@#{attrib}",val) end end end end end class UKUser < User attribs :name, :age, :weight name "john doe" age 25 weight 170 end u = UKUser.new puts u.name puts u.weight puts u.not_real -- Posted via http://www.ruby-forum.com/.