Created
December 14, 2011 20:09
-
-
Save pi3r/1478257 to your computer and use it in GitHub Desktop.
This file contains 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
directory structure | |
app/ | |
lib/ | |
core_ext/ | |
hash.rb | |
core_ext.rb | |
In config/application.rb: | |
config.autoload_paths += %W(#{config.root.join}/lib) | |
In lib/core_ext.rb | |
raise 'LOADED' | |
require 'core_ext/hash' | |
In lib/core_ext/hash.rb | |
class Hash | |
# allow to pass multiple keys, useful for nested hashes. | |
# Ex: h = { :a => { :b => { :c => 42 } } } | |
# h.r(:a, :b, :c) => 42 | |
def r(*args) | |
v = self[args.shift] | |
if args.empty? | |
v | |
elsif v.is_a?(Hash) | |
v.r(*args) | |
else | |
nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment