From: Christoph Rippel Date: 2001-03-14T03:09:07+09:00 Subject: [ruby-talk:12577] RE: interlude > From: Mathieu Bouchard [mailto:matju@sympatico.ca] [...] > interrupting your hair-pulling session with the most wasteful use of > inheritance yet. > > > > # computes a triangular number > $n = ARGV[0].to_i > $count = 0 > module Foo; def count; $count+=1; super; end end > module Bar; def count; 0; end end > class Zut > include Bar > zut = self.new > $n.times { include Foo.dup; zut.count } > end > p $count Nice ... > > > stay tuned for more abuse. Okay, I could not resist (I came up with this script when toying around with my multi-objects functional programming idea) - this is really sick and the ackerfunction ack(3,8) is around 63 - seconds versus 46-seconds for the fastest (Ruby) native integer implementation hard to believe but true .... ------------- $ time ruby Do.rb "I am from above" "Myself" 8 true 2045 real 1m3.872s user 1m2.710s sys 0m0.270s ------------- class Object @@me= Object def succ @@me =self # one could accessor function local variables won't work @succ = ObjectSpace._id2ref( class << self @pred = @@me def self.pred; @pred end def self.inspect; pred.inspect.succ end def self.to_truth; true end id end ) def self.succ @succ end @succ end Null = Object def Null.inspect; 0 end def Null.to_truth; false end def Null.pred; nil end One = Null.succ Two = One.succ Three = Two.succ Four = Three.succ Five = Four.succ Six = Five.succ #simple stuff def +(r); pred + r.succ end def Null.+(r); r end def *(r); pred * r + r end def One.*(r); r end def Null.*(r); Null end def cmp (r); pred.cmp ( r.pred) end def Null.cmp(r); r.to_truth end # other stuff cut #ackerman def ack n return pred.ack One if n.equal? Null return pred.ack ( self.ack (n.pred)) end def Null.ack n; n.succ end end Res= Three.ack (Three+Five) # might not work with 1.6.2 MRes =Res+One class MRes def self.res; p "Myself" end def res ; p "I am from above" end end Res.res MRes.res p Two * Three + Two p Res.cmp MRes p Res class Fixnum def ack n return n.succ if zero? return (self - 1).ack 1 if n.zero? return (self -1).ack ( self.ack (n - 1)) end end #p 3.ack 8 [...] Christoph