From: Roger Pack Date: 2009-01-23T03:06:01+09:00 Subject: [ruby-core:21525] Re: Proposal: Module#copy_method --0016364c716bd07a400461162330 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit > > > It would be used like: > > class Bar > def foo > 1 > end > > chain do > def foo > super + 1 > end > end > end > Just throwing this out [and I realize it's already been discussed a bit]... It seems possible as long as you start with a module always... class Class def chainable &block m = Module.new m.module_eval &block include m end end class A chainable do def abc print "in original abc" end end chainable do def abc print "in second abc" super end end end >> A.new.abc in second abcin original abc I will admit the inability to share a method from one class to another [unrelated class] is surprising. It seems that it is possible to move methods from module to module and from module to class but not from class to module or class to class. Odd but not the end of the world. I suppose for a more aggressive approach you could incorporate object2module [1]. Take care. -=r [1] http://banisterfiend.wordpress.com/2008/12/18/object2module-converting-ruby-objects-to-modules/ --0016364c716bd07a400461162330 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

It would be used like:

class Bar
  def foo
    1
  end
    
  chain do
    def foo
      super + 1
    end
  end
end
 
Just throwing this out [and I realize it's already been discu= ssed a bit]...
It seems possible as long as you start with a module always...
 
class Class
 def chainable &block
  m =3D Module.n= ew
  m.module_eval &block
  include m
 end
e= nd

class A
 chainable do
   def abc
    = print "in original abc"
   end
 end
 
 chainable do
    def abc
   =    print "in second abc"
    &nb= sp; super
    end
 end
 
end
 
>> A.new.abc
in second abcin original abc
 
 
I will admit the inability to share a method from one class to another= [unrelated class] is surprising.  It seems that it is possible to&nbs= p;move methods from module to module and from module to class but not = from class to module or class to class.  Odd but not the end of the wo= rld.
 
I suppose for a more aggressive approach you could incorporate object2= module <tongue in cheek> [1].
 
Take care.
 
-=3Dr
 
[1] http://banisterfiend.wordpress.co= m/2008/12/18/object2module-converting-ruby-objects-to-modules/
--0016364c716bd07a400461162330--