From: Trans Date: 2006-12-11T01:44:53+09:00 Subject: Re: bending ruby space ara.t.howard@noaa.gov wrote: > On Sun, 10 Dec 2006, Trans wrote: > > > i came upon this "pattern" working on a rather difficult problem. see > > if you can wrap your head around this bending of ruby space and what it > > might be good for. > > > > module R; end > > module U; include R; end > > module R; extend U; end > > one side effect is that all instance methods of R are available at the module > level too. i generally do that this way: > > harp:~ > cat a.rb > class Module > def export meth > module_function meth > public meth > end > end > > module R > def foo() 42 end > export :foo > end that's an interesting method in it's own right. i'm tempted to add to facets, though maybe the name isn't the best. I say that only b/c I've been using #import to load a lib directly into the current module space. and it would seem to me #import and #export should have some related usage --one way or the other. in any case you all are partly right of course. but the neat part of this --the real "bending" and the reason it uses two modules the way it does.... module R; end module U; include R; end module R; extend U; end module R def x; "x"; end end module U def x; "{" + super + "}"; end end R.x #=> "{x}" the effect is *dynamic* AOP wrapping of R by U. it is dynamic b/c it is possible to define the "aspect" U prior to any corresponding method in R. (btw, 'if defined?(super)' is helpful in such cases). I used this myself to cache specifically named methods that may, or may not, be in a user-defined code snippet. it is unfortuate however that, afaict, the same pattern can't be used on a class --just a module. t.