-
-
Save chendo/317937 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Symbol to Proc is a mutha' fucking dirty hack. It brings the indirection | |
# that we hated from Pearl to Ruby. How it made it into core I don't know. | |
# Anyway, rather than bitch and moan too much, here is an alternative method | |
# which is much more expressive and has the same number of characters. The | |
# name pick comes from Rebol, though it functions more like select there. | |
# Benchmark: | |
# user system total real | |
# 0.200000 0.000000 0.200000 ( 0.212325) | |
# 0.470000 0.000000 0.470000 ( 0.475471) | |
require 'benchmark' | |
module Enumerable | |
def pick(meth) | |
map {|o| o.send(meth.to_sym)} | |
end | |
end | |
class Symbol | |
def to_proc | |
Proc.new { |*args| args.shift.__send__(self, *args) } | |
end | |
end | |
N = 10000 | |
Benchmark.bm do |x| | |
x.report do | |
N.times do | |
%W( d e a t h _ t o _ s y m _ t o _ p r o c).pick(:upcase).join | |
end | |
end | |
x.report do | |
N.times do | |
%W( d e a t h _ t o _ s y m _ t o _ p r o c).map(&:upcase).join | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment