From: ara.t.howard@... Date: 2006-06-15T03:09:02+09:00 Subject: Re: Why the lack of mixing-in support for Class methods? ---1400102478-735090016-1150308539=:4620 Content-Type: MULTIPART/MIXED; BOUNDARY="-1400102478-735090016-1150308539=:4620" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---1400102478-735090016-1150308539=:4620 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed On Thu, 15 Jun 2006, Daniel Schierbeck wrote: > gwtmp01@mac.com wrote: >> >> On Jun 14, 2006, at 10:30 AM, transfire@gmail.com wrote: >>> Okay, so I'm wondering, will the annonymous module this creates get a >>> name of some sort instead of the ususal "#" >> >> I was thinking that the block would just be evaluated in the context of >> the targeted singleton class as vs. the context of a new anonymous >> module which is *then* included into the targeted singleton class. > > Then inheritance problems will arise -- I think we need to use modules. can someone point out to me what's wrong with this impl? it's simple, works in a recursive way, is self documenting, is already used, and is 34 lines long. as far as i can tell it already does every required and it very simple and robust. module Mixable Mixin = lambda do |this, other| cm = this.const_get 'ClassMethods' rescue nil im = this.const_get 'InstanceMethods' rescue nil other.extend cm if cm other.module_eval{ include im if im extend RecursiveMixin } end module RecursiveMixin def included other Mixin[self, other] super end end extend RecursiveMixin end class Object def mixin other sc = class << self self end sc.module_eval{ mixin other } end end class Module def mixin other other.module_eval{ include Mixable } unless Mixable > other include other end end if __FILE__ == $0 # # example 1 - simple usage # module M include Mixable module ClassMethods def foo() 42 end end end class C include M end p C.foo #=> 42 # # example 2 - note the how it works recursively # module A include Mixable module ClassMethods def bar() 'forty-two' end end module InstanceMethods def foobar() 42.0 end end end module B include A end class K include B # and recurively add ClassMethods from A! end p K.bar #=> 'forty=two' p K.new.foobar #=> 42.0 # # example 3 - we needn't both with Mixable if the 'includee' specifies that it # wants to to a mixin # module M module ClassMethods def bar() 42.0 end end def foo() 42 end end class C mixin M end p C.bar #=> 42.0 p C.new.foo #=> 42 # # and, of course, it works with objects # mixin M p foo #=> 42 class << self p bar #=> 42.0 end end -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama ---1400102478-735090016-1150308539=:4620 Content-Type: TEXT/PLAIN; charset=US-ASCII; name=mixable.rb Content-Transfer-Encoding: BASE64 Content-ID: Content-Description: Content-Disposition: attachment; filename=mixable.rb bW9kdWxlIE1peGFibGUNCiAgTWl4aW4gPSBsYW1iZGEgZG8gfHRoaXMsIG90 aGVyfA0KICAgIGNtID0gdGhpcy5jb25zdF9nZXQgJ0NsYXNzTWV0aG9kcycg cmVzY3VlIG5pbA0KICAgIGltID0gdGhpcy5jb25zdF9nZXQgJ0luc3RhbmNl TWV0aG9kcycgcmVzY3VlIG5pbA0KICAgIG90aGVyLmV4dGVuZCBjbSBpZiBj bQ0KICAgIG90aGVyLm1vZHVsZV9ldmFsew0KICAgICAgaW5jbHVkZSBpbSBp ZiBpbQ0KICAgICAgZXh0ZW5kIFJlY3Vyc2l2ZU1peGluDQogICAgfQ0KICBl bmQNCiAgbW9kdWxlIFJlY3Vyc2l2ZU1peGluIA0KICAgIGRlZiBpbmNsdWRl ZCBvdGhlcg0KICAgICAgTWl4aW5bc2VsZiwgb3RoZXJdDQogICAgICBzdXBl cg0KICAgIGVuZA0KICBlbmQNCiAgZXh0ZW5kIFJlY3Vyc2l2ZU1peGluDQpl bmQNCmNsYXNzIE9iamVjdA0KICBkZWYgbWl4aW4gb3RoZXINCiAgICBzYyA9 IA0KICAgICAgY2xhc3MgPDwgc2VsZg0KICAgICAgICBzZWxmDQogICAgICBl bmQNCiAgICBzYy5tb2R1bGVfZXZhbHsgbWl4aW4gb3RoZXIgfQ0KICBlbmQN CmVuZA0KY2xhc3MgTW9kdWxlDQogIGRlZiBtaXhpbiBvdGhlcg0KICAgIG90 aGVyLm1vZHVsZV9ldmFseyBpbmNsdWRlIE1peGFibGUgfSB1bmxlc3MgDQog ICAgICBNaXhhYmxlID4gb3RoZXINCiAgICBpbmNsdWRlIG90aGVyDQogIGVu ZA0KZW5kDQoNCg0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iAgZW5kDQogIGNsYXNzIEMNCiAgICBtaXhpbiBNDQogIGVu ZA0KICBwIEMuYmFyICAgICAjPT4gNDIuMA0KICBwIEMubmV3LmZvbyAjPT4g NDINCg0KIw0KIyBhbmQsIG9mIGNvdXJzZSwgaXQgd29ya3Mgd2l0aCBvYmpl Y3RzDQojDQogIG1peGluIE0NCiAgcCBmb28gIz0+IDQyDQogIGNsYXNzIDw8 IHNlbGYNCiAgICBwIGJhciAjPT4gNDIuMA0KICBlbmQNCg0KZW5kDQoNCg== ---1400102478-735090016-1150308539=:4620-- ---1400102478-735090016-1150308539=:4620--