Sean Russell wrote: > software. The most beautiful, functional code is absolutely worthless if > it isn't used because it is too slow. On the other hand, fast code will > tend to be used even if it is really, really. ....real? Ugly? Hackish? Dangerous? > True. I agree with you completely. XP, however, insists that you > refactor, refactor, refactor, and remove all possible redundancies. > Sometimes the way to improve speed and remove bottlenecks is to inline some > code. I was pondering about the %{ ... } trick mentioned in another thread, when a method wanted the textual version of a proc as well the Proc itself. Seeing as Ruby is so dynamic, I think it would be possible to do the inlining, while still separating the functionality in source. This because I read the refactoring rules in XP sort-of like the DRY principle in Pragmatic Programmers. So if the code gets inserted automatically, it doesn't matter as much. Now, talk is cheap, so I gave a (naive) stab at doing the inlining at method definition time. Its ugly, limited and probably buggy, but it should give you and idea of what I mean... <CODE> class Class def def_use_inline toClass, toInline, callingCodeText # modify any appearance of calls to toInline with the text of # methodcall itself... methodname = toInline.to_s data = toClass.getInlineData( toInline ) argumentNames = data[1] calledCodeText = data[2] # Find the call, # find arguments, # substitute the argumentNames in the calledCodeText # with the arguments passed # # i.e.: # from: "+ birthYear(2001).to_s" # to: "+ (begin; 2001 - @age;end).to_s" args = (".*," * argumentNames.length)[0...-1] reg = Regexp.new(methodname+'\(' + args + '\)') m = reg.match( callingCodeText ) realargsText = m.to_s[methodname.size+1...-1] realargs = realargsText.split(",") count = 0 newCalledCodeText = calledCodeText.dup realargs.each{|arg| newCalledCodeText.gsub!( argumentNames[ count ].to_s, arg) count += 1 } callingCodeText.gsub( reg, "(begin; puts 'Inlined version called!';"+ newCalledCodeText + ";end)" ) end def def_inlineable toClass, toInline, argumentNames, codeText toClass.registerInline( toInline, argumentNames, codeText ) plain_method = " def " + toInline.to_s + "(" + argumentNames.join(",") + ")\n" + " " + codeText + "\n" + "end" return plain_method end end module InlineTools @@inline_data = {} def registerInline( a, b, c) # puts "Register inline" @@inline_data[ a ] = [a,b,c] end def getInlineData( a ) @@inline_data[ a ] end end class Person extend InlineTools def initialize @name = "Kent" @age = 23 end # define a method that may be used inline eval def_inlineable Person, :birthYear,[:a_thisYear], %{ a_thisYear - @age } # define a method that may be used inline eval def_inlineable Person, :sayName,[:a_firstName, :a_secondName], %{ a_secondName + ", " + a_firstName } # define a method that may use inlined methods eval def_use_inline Person, :birthYear, %{ def sayHi( now ) puts "Hi, I'm "+ @name.to_s + ", and I was born " + birthYear(now).to_s end } eval def_use_inline Person, :sayName, %{ def sayHello puts "I'm: " + sayName( @name, "Dahl" ) end } end p = Person.new p.sayHi( 2001 ) p.sayHello </CODE> Could this be useful? -- <[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]> )____(stud.techn.;ind.шл.data)||(softwareDeveloper.at(Trustix))_( /"Opinions expressed are mine and not those of my Employer, "\ ( "the University, my girlfriend, stray cats, banana fruitflies, " ) \"nor the frontal lobe of my left cerebral hemisphere. "/