"Gavin Kistner" <gavin / refinery.com> schrieb im Newsbeitrag news:eee6f56d3405a57c4480046c35fca189 / refinery.com... > On Feb 10, 2005, at 7:35 AM, Robert Klemme wrote: > > "Gavin Kistner" <gavin / refinery.com> schrieb im Newsbeitrag > > news:991b24e67ac5d4902f4b04192210830a / refinery.com... > >>> Robert Klemme wrote: > >>>> class << self;self;end.class_eval do > >> Is it to add new methods to the instance only, and not all instances > >> derived from Foo? > > > > Exactly. > > Coming from Javascript (where every instance is trivially extensible > directly) that seems somewhat cumbersome. JavaScript has no classes as far as I know. Ruby has classes and from that alone it's obvious that it's a bit more complicated. Usually when you define instance methods you do obj = .... class <<obj def this_is_only_visible_in_a_single_instance() ... end end The construction with class<<self;self;end is only needed in order to be able to drag local variables into the class_eval block. > Do many people write things like: > > class Object > def instance_class > class<<self; self; end > end > end > > in a common library they re-use frequently? Is there an RCR (or has > there been discussion) Discussion - a lot, RCR - maybe. > about adding a simple method like that to the > language, so you can simply do: > > f = Foo.new > f.instance_class.class_eval{ ... } class Object def instance_class() class<<self;self;end end end > ? > > Or (what I'm really looking for) perhaps it might be nice to have a > define_method method that worked directly for instances. Hrm, but what > would the syntax be? Perhaps that's not such a good idea. IMHO it's sufficient to have the simplified access to the singleton class. OTOH, it's not too difficult to obtain that, so... Regards robert