From: Robert Klemme Date: 2006-08-01T20:45:05+09:00 Subject: Re: using the current method name within current method --------------060700080701060605030609 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Logan Capaldo wrote: > > On Jul 31, 2006, at 4:25 AM, Robert Klemme wrote: > >> Matt Todd wrote: >>> If you wanted to modularize it just for the sake of it, you could do >>> something similar to the following: >>> module CurrentMethodName >>> def this_method >>> caller[0]=~/`(.*?)'/ >>> $1 >>> end >>> end >> >> You can rewrite that as >> >> def this_method >> caller[0][/`([^']*)'/, 1] >> end >> >> and thusly avoid the global variable. >> >> Kind regards >> >> robert >> > > I have a sneaking suspicion he might have done it with the global > variable (although $1 isn't really global) on purpose, since =~ > /literal/ is the fastest way to do a regexp. I did a benchmark just to put some meat to this: 13:37:39 [Temp]: ./bm.rb Rehearsal -------------------------------------------- global 1.859000 0.000000 1.859000 ( 1.863000) String[] 2.156000 0.000000 2.156000 ( 2.180000) ----------------------------------- total: 4.015000sec user system total real global 1.828000 0.000000 1.828000 ( 1.872000) String[] 2.094000 0.000000 2.094000 ( 2.172000) IOW, the global var version uses 87,3% of the time or is 15% faster. Kind regards robert --------------060700080701060605030609 Content-Type: text/plain; name="bm.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="bm.rb" #!/usr/bin/env ruby require 'benchmark' REPEAT = 1_00_000 def this_method_1 caller[0][/`([^']*)'/, 1] end def this_method_2 caller[0] =~ /`([^']*)'/ and $1 end Benchmark.bmbm do |bm| bm.report "global" do REPEAT.times do this_method_2 end end bm.report "String[]" do REPEAT.times do this_method_1 end end end --------------060700080701060605030609--