Skip to content

Instantly share code, notes, and snippets.

@pi3r
Created December 14, 2011 20:09
Show Gist options
  • Save pi3r/1478257 to your computer and use it in GitHub Desktop.
Save pi3r/1478257 to your computer and use it in GitHub Desktop.
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