From: Kevin Mahler Date: 2011-04-15T10:07:16+09:00 Subject: Re: What do you do when you need to attach data to an object instance? Aaron D. Gifford wrote in post #992887: > def extend_accessor(obj, name) > mod = Module.new > mod.send(:public).send(:attr_accessor, name.to_sym) > obj.extend(mod) > obj > end Or more compactly, def extend_accessor(obj, name) obj.extend Module.new { attr_accessor name } obj end But that's an odd maneuver anyway. My original example extended an object with a specified, named mixin. For ad hoc methods the singleton class is more natural. def extend_accessor(obj, name) obj.singleton_class.module_eval { attr_accessor name } obj end You probably found a bug with attr_accessor; it should produce public methods in any context, I think. Nobody has encountered the bug because Module.new { } and module_eval { } are commonly used to do those things, and those behave correctly. -- Posted via http://www.ruby-forum.com/.