Created
May 16, 2012 23:12
-
-
Save heph/2714799 to your computer and use it in GitHub Desktop.
populate hash with command line arguments in the form of ./script.rb key=value key.key=value key.key.key=value
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
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'json' | |
h = {} | |
ARGV.each do |a| | |
position = ObjectSpace._id2ref(h.object_id) | |
keys,value = a.split('=') | |
keys = keys.split('.') | |
while key = keys.shift do | |
if keys.length > 0 | |
if position["#{key}"].nil? | |
position["#{key}"] = Hash.new | |
end | |
position = ObjectSpace._id2ref(position["#{key}"].object_id) | |
else | |
position["#{key}"] = value | |
end | |
end | |
end | |
puts JSON.pretty_generate(h) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment