* [ruby-core:21822] [Feature #1102] Prepend Module
@ 2009-02-04 2:37 Thomas Sawyer
2009-02-08 2:42 ` [ruby-core:21922] " Roger Pack
0 siblings, 1 reply; 6+ messages in thread
From: Thomas Sawyer @ 2009-02-04 2:37 UTC (permalink / raw
To: ruby-core
Feature #1102: Prepend Module
http://redmine.ruby-lang.org/issues/show/1102
Author: Thomas Sawyer
Status: Open, Priority: Normal
Category: core, Target version: 1.9.x
Currently when a module is included into a classes, it is appended to the class hierarchy (ie. the method lookup order). This of course makes sense, but there are times when it would be useful to *prepend* the module. For example:
class C
def x; "x"; end
end
module M
def x; '[' + super + ']'; end
end
class C
prepend M
end
C.new.x #=> "[x]"
One big advantage of this is being able to override methods in a safer way, rather than using alias or tricks like alias_method_chain.
----------------------------------------
http://redmine.ruby-lang.org
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:21922] Re: [Feature #1102] Prepend Module
2009-02-04 2:37 [ruby-core:21822] [Feature #1102] Prepend Module Thomas Sawyer
@ 2009-02-08 2:42 ` Roger Pack
2009-03-20 9:50 ` [ruby-core:22971] " trans
0 siblings, 1 reply; 6+ messages in thread
From: Roger Pack @ 2009-02-08 2:42 UTC (permalink / raw
To: ruby-core
> Currently when a module is included into a classes, it is appended to the class hierarchy (ie. > the method lookup order). This of course makes sense, 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
def insert_module_at_top mod
previous_ancestors = self.ancestors.select{|a| a.class == Module}
include mod
previous_ancestors.each{|a| include a.dup}
end
end
#again we have to start with a module.
module Original
def x; '[' + super + ']'; end
end
class A
include Original
end
module Prepend
def x; "x"; end
end
A.insert_module_at_top Prepend
puts A.new.x
=> [x]
Perhaps what we're saying here is we wish we could "grab" methods from
classes, to be able to manipulate the hierarchy better? This 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
-=r
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:22971] Re: [Feature #1102] Prepend Module
2009-02-08 2:42 ` [ruby-core:21922] " Roger Pack
@ 2009-03-20 9:50 ` trans
2010-03-01 19:47 ` [ruby-core:28389] " Roger Pack
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: trans @ 2009-03-20 9:50 UTC (permalink / raw
To: ruby-core
On Feb 7, 10:42 pm, Roger Pack <rogerdp...@gmail.com> wrote:
> > Currently when a module is included into a classes, it is appended to the class hierarchy (ie. > the method lookup order). This of course makes sense, 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
> def insert_module_at_top mod
> previous_ancestors = self.ancestors.select{|a| a.class == Module}
> include mod
> previous_ancestors.each{|a| include a.dup}
> end
> end
>
> #again we have to start with a module.
>
> module Original
> def x; '[' + super + ']'; end
> end
>
> class A
> include Original
> end
>
> modulePrepend
> def x; "x"; end
> end
> A.insert_module_at_topPrepend
> puts A.new.x
> => [x]
>
> Perhaps what we're saying here is we wish we could "grab" methods from
> classes, to be able to manipulate the hierarchy better? This 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 = []
super
end
end
class X
prepend P
end
X.new.p => []
T.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:28389] [Feature #1102] Prepend Module
2009-03-20 9:50 ` [ruby-core:22971] " trans
@ 2010-03-01 19:47 ` Roger Pack
2010-03-02 1:49 ` [ruby-core:28391] " Michael Fellinger
2010-03-02 3:40 ` [ruby-core:28393] " Yusuke Endoh
2 siblings, 0 replies; 6+ messages in thread
From: Roger Pack @ 2010-03-01 19:47 UTC (permalink / raw
To: ruby-core
Issue #1102 has been updated by Roger Pack.
+1 for Module#prepend
----------------------------------------
http://redmine.ruby-lang.org/issues/show/1102
----------------------------------------
http://redmine.ruby-lang.org
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:28391] [Feature #1102] Prepend Module
2009-03-20 9:50 ` [ruby-core:22971] " trans
2010-03-01 19:47 ` [ruby-core:28389] " Roger Pack
@ 2010-03-02 1:49 ` Michael Fellinger
2010-03-02 3:40 ` [ruby-core:28393] " Yusuke Endoh
2 siblings, 0 replies; 6+ messages in thread
From: Michael Fellinger @ 2010-03-02 1:49 UTC (permalink / raw
To: ruby-core
Issue #1102 has been updated by Michael Fellinger.
+1 for Module#prepend
----------------------------------------
http://redmine.ruby-lang.org/issues/show/1102
----------------------------------------
http://redmine.ruby-lang.org
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:28393] [Feature #1102] Prepend Module
2009-03-20 9:50 ` [ruby-core:22971] " trans
2010-03-01 19:47 ` [ruby-core:28389] " Roger Pack
2010-03-02 1:49 ` [ruby-core:28391] " Michael Fellinger
@ 2010-03-02 3:40 ` Yusuke Endoh
2 siblings, 0 replies; 6+ messages in thread
From: Yusuke Endoh @ 2010-03-02 3:40 UTC (permalink / raw
To: ruby-core
Issue #1102 has been updated by Yusuke Endoh.
Target version changed from 1.9.x to 2.0
Hi,
This ticket was also discussed in the thread from [ruby-core:25208].
Module#prepend may be very significant feature not only to implementation
but also to Ruby's OO model itself.
Don't consider it just convenient method like Array's and String's.
So, in my opinion, this feature should not be included in 1.9.x.
We should discuss it towards 2.0.
Even if it will be included in 1.9.x, we need more discussion.
Just seeing clean example, you'll find it cool. But in fact, we must
also discuss many dirty things:
- edge semantics
- prepend into embedded class
- prepend into singleton class
- collaboration with reflection
- collaboration with future expansion (e.g., classbox)
- etc.
- implementation
- robustness
- binary compatibility
- expandability
- maintainability
- performance
I think it is difficult to discuss them without material. So, please
write a patch first if you really want.
--
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
http://redmine.ruby-lang.org/issues/show/1102
----------------------------------------
http://redmine.ruby-lang.org
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2010-03-02 3:43 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-04 2:37 [ruby-core:21822] [Feature #1102] Prepend Module Thomas Sawyer
2009-02-08 2:42 ` [ruby-core:21922] " Roger Pack
2009-03-20 9:50 ` [ruby-core:22971] " trans
2010-03-01 19:47 ` [ruby-core:28389] " Roger Pack
2010-03-02 1:49 ` [ruby-core:28391] " Michael Fellinger
2010-03-02 3:40 ` [ruby-core:28393] " Yusuke Endoh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).