Last active
December 16, 2015 23:59
-
-
Save plusor/5517768 to your computer and use it in GitHub Desktop.
Deep create child hashes
This file contains hidden or 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
class Hash | |
# k = {} | |
# k.create_deeps(*%w(1 2 3 4 5 6 7 8 9 10)) | |
# #=> {"1"=>{"2"=>{"3"=>{"4"=>{"5"=>{"6"=>{"7"=>{"8"=>{"9"=>"10"}}}}}}}}} | |
def create_deeps(*args) | |
self.replace deep_create(*args) | |
end | |
def deep_create(*args) | |
dup.deep_create!(*args) | |
end | |
def deep_create!(*args) | |
if args.length > 2 && self[k=args.shift].nil? | |
self[k] = deep_create(*args) | |
else | |
self[args.shift] = args.pop | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment