From: Rich Kilmer Date: 2002-12-16T23:03:32+09:00 Subject: Re: un-extending objects Actually on a somewhat related idea, I wanted to have an object be able to mixin an eval'ed set of methods. That way I could dynamically change them by re-eval'ing changed behavior. Its for a software agent system I am playing with...anyway...what I did was have a random module created for my target "Agent" object which was empty of methods. I then eval'ed the method definitions (String) into that module. Whenever the methods definitions changed I could then remove all the methods on the module cleanly and re-eval the defintions. It works really well, but not at the granularity your describe below (during a method call). -rich > -----Original Message----- > From: dblack@candle.superlink.net > [mailto:dblack@candle.superlink.net] > Sent: Saturday, December 14, 2002 7:46 PM > To: ruby-talk ML > Subject: un-extending objects > > > Hi -- > > I've become intrigued by the idea of unextending objects -- > that is, removing capabilities from them module-wise. > > A couple of things that have been mentioned here recently > that relate at least very indirectly to this are: the idea of > a "clean" root class for objects; and the discussion about > inheritance (especially from core classes) leaving an object > with "extra" methods. In general, all of these thoughts seem > to pertain to the idea of a the granularity of the modular, > "plug-in" capabilities of Ruby objects. > > Anyway, I don't have any big idea in mind. Just playing > around with the idea of being able to add and remove modules > at run-time. My current little testbed for this looks like this: > > # Object#stretch(aModule) -- extend only for duration of block > class Object > def stretch(mod) > c = class << self; self; end > before = methods > extend(mod) > yield > (methods - before).each { |m| c.class_eval {undef_method(m)} } > end > end > > # Demo > module Stuff > def talk; puts "hi"; end > end > > a = [1,2,3] > a.stretch(Stuff) { a.talk } # hi > p a.respond_to?(:talk) # false > > This has a kind of aesthetic or maybe narrative appeal to me: > the capabilities required of an object for a given purpose > exist only at a kind of flashpoint, the exact place and time > where they're needed, and then they disappear again. > > I'm interested in whether people have done things like this > before, whether it looks like the kind of thing one would > ever actually use, etc. > > > David > > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > > >