Created
December 5, 2014 16:28
-
-
Save jvidalba1/7c60a91c6d92e0fa56bf to your computer and use it in GitHub Desktop.
creating hash given a string and deeping them with deep_merge from active_support gem
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
require 'active_support/all' | |
def single_hash(keys, value) | |
splitted = keys.split(":") | |
hash = { splitted.last => value } | |
splitted[0..-2].reverse.each do |key| | |
hash = { key => hash} | |
end | |
hash | |
end | |
arr = [ | |
["a:b:c", 4], | |
["a:b:d", 5], | |
["a:x", 6] | |
] | |
h = {} | |
arr.each do |key, value| | |
h.deep_merge!(single_hash(key, value)) | |
end | |
p h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment