From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-12-15T10:58:13+09:00 Subject: Re: un-extending objects In article , wrote: >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. > I like this. I've been thinking for a while that it would be nice to be able to un-extend an object. A while back I was doing something where I was passing an object around and extending it when it arrived somewhere, but I wanted to un-extend it prior to passing it on the next receiver. Phil -- "Or perhaps the truth is less interesting than the facts?" Amy Weiss (accusing theregister.co.uk of engaging in 'tabloid journalism') Senior VP, Communications Recording Industry Association of America