Skip to content

Instantly share code, notes, and snippets.

@heph
Created May 16, 2012 23:12
Show Gist options
  • Save heph/2714799 to your computer and use it in GitHub Desktop.
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
#!/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