Skip to content

Instantly share code, notes, and snippets.

@TimLang
Created December 23, 2013 07:04
Show Gist options
  • Save TimLang/8092775 to your computer and use it in GitHub Desktop.
Save TimLang/8092775 to your computer and use it in GitHub Desktop.
multi-dimensional array map
class Object
def deep_map(&block); yield(self) end
end
class Array
def deep_map(&block); map{|e| e.deep_map(&block)} end
end
or
class Object
def deep_map &block
if self.respond_to?(:inject)
self.inject([]) do |result, ary|
result << ary.deep_map(&block)
end
else
block.call(self)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment