[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

bending ruby space

Trans

12/9/2006 10:52:00 PM

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

t.


15 Answers

Devin Mullins

12/10/2006 12:41:00 AM

0

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
why not:
module R; extend self end
or just:
module R; module_function; ... end
?

Devin
(Thas right, biatches. extend self is the new class << self.)

dblack

12/10/2006 3:15:00 AM

0

John Wilger

12/10/2006 3:19:00 AM

0

On Dec 9, 2:51 pm, "Trans" <transf...@gmail.com> wrote:
> 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

Job security? ;-)

Trans

12/10/2006 3:31:00 AM

0


John Wilger wrote:
> On Dec 9, 2:51 pm, "Trans" <transf...@gmail.com> wrote:
> > 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
>
> Job security? ;-)

LOL! :-D

Okay. When my headache goes away I'll tell you too how you can secure
your job ;-)

T.


.---------.
david | the box | me ->
black most people
'---------'


Devin Mullins

12/10/2006 4:22:00 AM

0

dblack@wobblini.net wrote:
> I'm not sure what you mean. extend self doesn't do what class << self
> does. Am I misunderstanding?
It was a "green is the new blue" parody. Not a funny one, mind you, but
the use of "biatches" should've warned you of that.

Devin

Ara.T.Howard

12/10/2006 5:10:00 AM

0

Trans

12/10/2006 4:45:00 PM

0


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.


Ara.T.Howard

12/10/2006 5:08:00 PM

0

Trans

12/11/2006 3:45:00 PM

0


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
>
> p R.foo
>
> harp:~ > ruby a.rb
> 42

speaking of this "export" method, how does one do that for a whole
module? ie. adding one module_finction module to another.

module X
module_function
def f; "f"; end
end

module Q
module_function
include X
extend X
end

but that doesn't work b/c

Q.f #=> ERROR: private method `f' called for Q:Module

t.


Ara.T.Howard

12/11/2006 4:05:00 PM

0