From: Robert Dober Date: 2007-08-03T18:25:36+09:00 Subject: Re: From method redefinition to classless Ruby. On 8/3/07, Trans wrote: > > > On Aug 2, 4:43 pm, "Robert Dober" wrote: > > Hi list > > > > Ara's post about method redefinition was extremely rich intellectual > > food, took some time to digest, but I just thought I share my findings > > with you. > > Ara and Pit are impressive meta-coders indeed. Think Ara might even > have a Prototype lib > you'd enjoy. > > BTW, if we were all good little scouts and always modularized our code > one could use: > > class Module > # Prepend an "aspect" module to a module. > # > # module X > # def x; "x"; end > # end > # > # module U > # def x; '{' + super + '}'; end > # end > # > # X.prepend U > # > # X.x # => "{x}" > > def prepend( aspect ) > aspect.send(:include, self) > extend aspect > end > end > > Another trick for common injuns: > > class Class > > # Prepend an "aspect" module to a class. > # > # class Firetruck > # def put_out_fire(option) > # "Put out #{option}" > # end > # end > # > # module FastFiretruck > # def put_out_fire(option) > # super("very #{option}!") > # end > # end > # > # Firetruck.prepend(FastFiretruck) > # > # ft = Firetruck.new > # ft.put_out_fire('fast') #=> "Put out very fast!" > > def prepend( aspect ) > _new = method(:new) > _allocate = method(:allocate) > (class << self; self; end).class_eval do > define_method(:new) do |*args| > o = _new.call(*args) > o.extend aspect > o > end > define_method(:allocate) do |*args| > o = _allocate.call(*args) > o.extend aspect > o > end > end > end > > end > > Albeit that has some unfortunate shortcomings. > > T. Well I do not really see these shortcomings, I'd love to have this mail as a comment to my blogpost BTW, would you mind? Concerning Ara's Prototype library I have seen it flying by, I will have a closer look, sure deserves an Exolink. But probably Ara's had all the fun for himself already and there is nothing left to do for me :(. Cheers Robert > > -- [...] as simple as possible, but no simpler. -- Attributed to Albert Einstein