I received an array from an API that appears to contain, for lack of a better phrase, object-style notation.
Since it is an array, I cannot obviously access it by a key or value. I don't see a method in this particular object type that allows me to convert it to a hash, or JSON and then to a hash.
What is the most appropriate way of converting this to a hash where I can access the data by key and value?
Output:
[#<ObjectType::ObjectRef:0x30f2862
@_ref="record:full/data/location/setting/partition",
@configured=false,
@name="John Doe",
@addr="10.10.10.10">]
Alternatively, if this can be converted to an array with multiple elements (instead of one big chunked together element), I could parse it to CSV and filter outside of Ruby. The elements in the new array would contain the "values" (e.g. false, "John Doe", "10.10.10.10).
Try this:
array # => [#<ObjectType::ObjectRef:0x30f2862
@_ref="record:full/data/location/setting/partition",
@configured=false,
@name="John Doe",
@addr="10.10.10.10">]
array.map { |a| {configured: a.configured, name: a.name, addr: a.addr} }
# or if you can't access the instance variables
array.map do |a|
{
configured: a.instance_variable_get(:@configured),
name: a.instance_variable_get(:@name),
addr: a.instance_variable_get(:@addr)
}
end # => [{configured: false, name: "John Doe", addr: "10.10.10.10"}]
# and if you want an array
array.map do |a|
[
a.instance_variable_get(:@configured),
a.instance_variable_get(:@name),
a.instance_variable_get(:@addr)
]
end # => [[false, "John Doe", "10.10.10.10"]]