# # # # # # # # #
# /29/hash2proc.rb
#
# by Jan Lelis
# e-mail: mail@janlelis.de
# type/version: ruby
# snippet url: http://rbJL.net/29/hash2proc.rb
# original post: http://rbJL.net/29-become-a-proc-star
# license: CC-BY (DE)
#
# (c) 2010 Jan Lelis.
# If the object is found as key in the hash, this runs its value as proc,
# else it just returns the object.
class Hash
def to_proc
Proc.new{ |obj|
if self.member? obj
self[obj].to_proc.call obj
else
obj
end
}
end
end
# usage
# [1,2,3,4,5].map &{ 1 => :to_s,
# 3 => [:to_s, 2] } # if you use Array#to_proc
# => ["1", 2, "11", 4, 5]