Skip to content

Instantly share code, notes, and snippets.

@kiennt
Created February 16, 2013 15:07
Show Gist options
  • Save kiennt/4967292 to your computer and use it in GitHub Desktop.
Save kiennt/4967292 to your computer and use it in GitHub Desktop.
Pre define method
require 'benchmark'
class A
def method_missing name, *argv
super unless name =~ /^test$/
"hello world"
end
end
class B
def method_missing name, *argv
super unless name =~ /^test$/
instance_eval <<-METHOD
def test
"hello world"
end
METHOD
test
end
end
Benchmark.bm do |x|
a = A.new
b = B.new
n = 50000
x.report("method_missing") { n.times { a.test } }
x.report("predefine method") { n.times { b.test } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment