Skip to content

Instantly share code, notes, and snippets.

@jvidalba1
Created December 5, 2014 16:28
Show Gist options
  • Save jvidalba1/7c60a91c6d92e0fa56bf to your computer and use it in GitHub Desktop.
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
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