From: Kevin Mahler Date: 2011-04-17T00:32:22+09:00 Subject: Re: What do you do when you need to attach data to an object instance? 7stud -- wrote in post #993115: > Kevin Mahler wrote in post #992907: >> >> def extend_accessor(obj, name) >> obj.singleton_class.module_eval { attr_accessor name } >> obj >> end >> > > Hey, now. obj.singleton_class is a class, so how about using the > synonym class_eval(): > > obj.singleton_class.class_eval { attr_accessor name } A class is a module, but a module is not a class. Class.new.is_a? Module #=>true Module.new.is_a? Class #=>false Because module_eval and class_eval are aliases, class_eval can be called on a module, a situation which is at worst wrong and at best confusing. module_eval can never be wrong or confusing. Not a hard choice. -- Posted via http://www.ruby-forum.com/.