Skip to content

Instantly share code, notes, and snippets.

@codekitchen
Forked from zwily/gist:633072
Created October 18, 2010 21:16
Show Gist options
  • Save codekitchen/633077 to your computer and use it in GitHub Desktop.
Save codekitchen/633077 to your computer and use it in GitHub Desktop.
class Object
def send_chain(meths)
result = self
meths.each do |m|
return nil if result == nil
if result.respond_to?(m)
result = result.__send__(m)
elsif result.respond_to?(:[])
result = result[m]
else
raise "#{result.class} doesn't respond to #{m}"
end
end
result
end
end
module Enumerable
def supermap(*args, &block)
res = []
self.each do |e|
arr = []
args.each { |a| arr << e.send_chain(a.to_s.split('.')) }
arr << block.call(e) if block
res << arr
end
res
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment