2 def constantize_full(camel_cased_word)
3 unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
4 raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
7 Object.module_eval("::#{$1}", __FILE__, __LINE__)
10 def constantize_no_re(camel_cased_word)
11 Object.module_eval("::#{camel_cased_word}", __FILE__, __LINE__)
14 def constantize_no_eval(camel_cased_word)
15 unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
16 raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
22 Integer(ARGV[0] || 10).times do
23 Benchmark.bm(30) do |bm|
24 bm.report("constantize_full(\"Hash\")") { 500_000.times { constantize_full("Hash") }}
25 bm.report("constantize_no_re(\"Hash\")") { 500_000.times { constantize_no_re("Hash") }}
26 bm.report("constantize_no_eval(\"Hash\")") { 500_000.times { constantize_no_eval("Hash") }}