Skip to content

Instantly share code, notes, and snippets.

@valo
Created October 21, 2011 18:29
Show Gist options
  • Save valo/1304570 to your computer and use it in GitHub Desktop.
Save valo/1304570 to your computer and use it in GitHub Desktop.
class MethodMissing
def method_missing(name, *args)
msg = name.to_s[/print_(.*)/, 1]
print msg
end
end
class LazyMethodDefine
def method_missing(name, *args)
msg = name.to_s[/print_(.*)/, 1]
self.class.send(:define_method, name) do
print msg
end
send name
end
end
require 'benchmark'
puts Benchmark.measure { 100000.times { MethodMissing.new.print_h } } # => 0.760000 0.130000 0.890000 ( 0.890088)
puts Benchmark.measure { 100000.times { LazyMethodDefine.new.print_h } } # => 0.310000 0.100000 0.410000 ( 0.413843)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment