Skip to content

Instantly share code, notes, and snippets.

@sandrods
Created September 29, 2011 02:17
Show Gist options
  • Save sandrods/1249828 to your computer and use it in GitHub Desktop.
Save sandrods/1249828 to your computer and use it in GitHub Desktop.
# coding: UTF-8
require 'ostruct'
def hash2col(hash, opts={})
ret = []
hash.each do |key, value|
value = yield(value) if block_given?
ret << OpenStruct.new(opts[:key] => key, opts[:value] => value)
end
ret
end
my_hash = {}
my_hash["VAL_01"] = {"SUB_01_01" => [1, 2, 3], "SUB_01_02" => [4, 5, 6], "SUB_01_03" => [7, 8, 9] }
my_hash["VAL_02"] = {"SUB_02_01" => [21, 22, 23], "SUB_02_02" => [24, 25, 26], "SUB_02_03" => [27, 28, 29] }
my_hash["VAL_03"] = {"SUB_03_01" => [31, 32, 33], "SUB_03_02" => [34, 35, 36], "SUB_03_03" => [37, 38, 39] }
col = hash2col(my_hash, :key => 'name', :value => 'children') do |value|
hash2col(value, :key => 'descr', :value => 'items')
end
puts col
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment