From: Dave Thomas Date: 2002-07-23T13:28:12+09:00 Subject: Re: attr_reader for great number of instance vars michael libby writes: > The attr_reader line looks clumsy to me. Is there a way to get the class to > automatically allow for any instance variable to be given the attr_reader > characteristic? THe class doesn't know it's instance variables: they only appear in the actually instances when first used (that is, each instance could potentially have a different set of instance variables). The code below is kind of dangerous, but dynamically defines reader methods for any otherwise unknown method call: class Dave def method_missing(name, *args) fail "No one expects the extra args" unless args.empty? type.class_eval "attr_reader :#{name}" send name end def initialize @a = 1 @b = "cat" @c = /abc/ end end d = Dave.new p d.a p d.b p d.c If there was some point at which you knew all your instance variables were set, you could always use the Object#instance_variables method to iterate over them and create accessors. Cheers Dave