From: Robert Dober Date: 2006-08-29T08:07:40+09:00 Subject: Re: Partial append_features? ------=_Part_184541_10063997.1156806456734 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 8/28/06, Logan Capaldo wrote: > > > On Aug 28, 2006, at 3:00 PM, Ola Bini wrote: > > > Hi, > > > > I'm looking for a way to copy methods from a Module, or specify > > more directly which methods get included in a class. More or less, > > I would like to be able to do something like this: > > > > module Foo > > def do_one_thing > > end > > def do_second > > end > > def do_third > > end > > end > > > > class Bar > > append_from Foo, :do_second, :do_third > > end > > > > or > > module Baz > > append_from Foo, :do_second, :do_third > > end > > > > and I would have a module Baz which could be included, without > > having do_one_thing included. > > > > Is this possible in Ruby right now? My first approach was to get > > the UnboundMethod instance_method from the Module, but I couldn't > > find a way to attach these to an unrelated class since > > UnboundMethod must have a is_a?-relationship with the binding object. > > > > Regards > > -- > > Ola Bini (http://ola-bini.blogspot.com) > > JvYAML, RbYAML, JRuby and Jatha contributor > > System Developer, Karolinska Institutet (http://www.ki.se) > > OLogix Consulting (http://www.ologix.com) > > > > "Yields falsehood when quined" yields falsehood when quined. > > > > > > % cat Projects/Ruby Experiments/append_from.rb > class Module > def append_from( mod, *methods_to_keep ) > methods_to_keep.map! { |m| m.to_s } > methods_to_remove =3D mod.instance_methods(false) - methods_to_keep > new_mod =3D Module.new > new_mod.module_eval do > include mod > methods_to_remove.each { |meth| undef_method meth } > end > include new_mod > end > end > > module M > def a > puts "a" > end > > def b > puts "b" > end > end > > class A > append_from M, :b > end > > a =3D A.new > a.b > a.a > > % ruby Projects/Ruby Experiments/append_from.rb > b > -:40: undefined method `a' for # (NoMethodError) > > > > > unfortunately that approach undefines eralier defined methods in the appendee, especially inherited ones :( I guess that could be fixed but the code will become rather clumsy. Cheers Robert --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_184541_10063997.1156806456734--