On Feb 7, 10:42=A0pm, Roger Pack <rogerdp... / gmail.com> wrote:
> > Currently when a module is included into a classes, it is appended to t=
he class hierarchy (ie. > the method lookup order). This of course makes se=
nse, but there are times when it would be > useful to *prepend* the module.=
 For example:
>
> I suppose one [not too useful] hack at it could be something like
>
> class Class
> =A0 def insert_module_at_top mod
> =A0 =A0 previous_ancestors =3D self.ancestors.select{|a| a.class =3D=3D M=
odule}
> =A0 =A0 include mod
> =A0 =A0 previous_ancestors.each{|a| include a.dup}
> =A0 end
> end
>
> #again we have to start with a module.
>
> module Original
> =A0 def x; '[' + super + ']'; end
> end
>
> class A
> =A0include Original
> end
>
> modulePrepend
> =A0def x; "x"; end
> end
> A.insert_module_at_topPrepend
> puts A.new.x
> =3D> [x]
>
> Perhaps what we're saying here is we wish we could "grab" methods from
> classes, to be able to manipulate the hierarchy better? =A0This is
> possible with RubyParser, since you can basically get back to the
> source of the method and thus copy it around, but not afaik with 1.9

Well, that's not really the issue here. The need is to *wrap*
previously defined instance methods. If every method were defined in a
module (something I have suggested in the past actually) then it would
not be needed.

The utility comes from doing AOP-esque coding. Consider modules that
can initialize values.

  class X
  end

  module P
    attr :p
    def initialize
      @p =3D []
      super
    end
  end

  class X
    prepend P
  end

  X.new.p  =3D> []

T.