From: 7stud -- Date: 2011-04-17T02:14:23+09:00 Subject: Re: What do you do when you need to attach data to an object instance? Kevin Mahler wrote in post #993207: > 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. > Yes, of course. > 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 It certainly isn't "wrong"--ruby allows it. > and at best > confusing. module_eval can never be wrong or confusing. Not a hard > choice. I thought using module_eval was confusing in your code, and that was why I suggested class_eval(). -- Posted via http://www.ruby-forum.com/.