Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Created March 26, 2014 11:17
Show Gist options
  • Save adammcarth/9781067 to your computer and use it in GitHub Desktop.
Save adammcarth/9781067 to your computer and use it in GitHub Desktop.
require "ostruct"
# Actual value obtained from elsewhere, must
# be in this format up until now.
required_records = {"1" => { "name" => "Adam" }, "2" => { "name" => "John" }}
# Loop through each required record and
# assemble each key-value into the open structure output.
output = Array.new
required_records.each do |id, record|
output += OpenStruct.new({ "id" => id.to_i }.merge(record))
end
# Real output => can't convert open struct to array
# Desired output => [#<OpenStruct id=1, name="Adam">, #<OpenStruct id=2, name="John">]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment