Skip to content

Instantly share code, notes, and snippets.

@kml
Last active October 26, 2015 10:01
Show Gist options
  • Save kml/0fb1588fc1b6ba4c60fa to your computer and use it in GitHub Desktop.
Save kml/0fb1588fc1b6ba4c60fa to your computer and use it in GitHub Desktop.
# encoding: utf-8
# Usage:
# jruby coercible_coercer_object_method_missing_patch_bench.rb
# jruby coercible_coercer_object_method_missing_patch_bench.rb patch
# Issue: https://github.com/solnic/coercible/pull/22
if $0 == __FILE__
require "bundler/inline"
gemfile(ARGV[0] == "install") do
source "https://rubygems.org"
gem "coercible"
end
end
if ARGV[0] == "patch"
# https://github.com/solnic/coercible/blob/master/lib/coercible/coercer/object.rb#L163
require "coercible"
module Coercible
class Coercer
class Object
private
def method_missing(method, *args)
if method.to_s =~ COERCION_METHOD_REGEXP && args.size == 1
self.class.send(:define_method, method) do |value|
value
end
return args.first
end
super
end
end
end
end
end
if $0 == __FILE__
require "benchmark"
require "coercible"
coercer = Coercible::Coercer.new
repetitions = 100_000
Benchmark.bmbm do |x|
x.report("coerce #{ARGV[0]}") do
repetitions.times do
coercer[String].to_object("STRING")
coercer[TrueClass].to_boolean(true)
coercer[Object].to_object(nil)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment