From: matz@... (Yukihiro Matsumoto) Date: 2001-06-19T13:00:49+09:00 Subject: [ruby-talk:16616] Re: Class Var question Hi, In message "[ruby-talk:16610] Re: Class Var question" on 01/06/19, Steve Tuckner writes: |What I want to do is have some "meta" information about an object's members. |So for example if an object has two members: ht and wt, I want to associated |with them the names to display if I have an object display module. I would |like to use the following code to use this capability. Is the following code close to what you want? ---- class Object def Object.uservar(name, printname) @user_vars ||= {} @user_vars[name] = printname attr_reader name end def Object.uservars if defined? @user_vars @user_vars else {} end end end class Contact uservar :first_name, "First Name" uservar :last_name, "Last Name" def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end end class Address uservar :street, "Street" uservar :city, "City" uservar :country, "Country" uservar :postal_code, "Postal Code" def initialize(street, city, country, postal_code) @street = street @city = city @country = country @postal_code = postal_code end end def dump_objects(objects) objects.each do |object| for k, v in object.type.uservars print v, ": ", object.send(k), "\n" end print "\n" end end dump_objects([Contact.new("Joe", "Schmo"), Address.new("1212 Penny Lane", "London", "UK", "4A3B2A")])