-1

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).

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kurt W
  • 321
  • 2
  • 15
  • Please read "[mcve]". We need to see your code that attempts to solve this. Is the "Output" section what you received from the API? How are you viewing the data? – the Tin Man Jun 17 '16 at 23:57
  • Yes, the output is the array returned by the API. I printed that data using `pretty print` but variable.inspect would have returned the same content. I have been meaning to ask you: how do I provide a question that satisfies minimal, complete, and verifiable when I truly don't have code to share. For example, I know I cannot use an array to access that data as key/value pairs. I could show that array doesn't have any methods to do that, but that wouldn't be helpful. I could show an attempt to split on `=` but that is quite contrived. Trying to ask good questions for future SO users. – Kurt W Jun 18 '16 at 00:17
  • 1
    The output you've shown is almost certainly a normal array with a single element. So the first step is `obj = output[0]`. After that I'm willing to bet `obj.name`, `obj.addr`, etc. will work just fine. – Jordan Running Jun 18 '16 at 00:59
  • If you want to convert it to a hash you have to do it manually unless the library you're using provides a method for this (like ActiveRecord does with `attributes`). – max pleaner Jun 18 '16 at 01:33
  • I'm pretty sure you have `ObjectType::ObjectRef` defined somewhere. Maybe it's a gem. So, you could do the next: `output.first._ref`. To be sure please provide the gem you're using or code that produces that output. – retgoat Jun 18 '16 at 04:16
  • To reassemble parts of what others said, what you're getting is an object that almost certainly already has methods available to get what you need. Wouldn't converting it to a hash be excessive complexity without benefit to justify that complexity? – Keith Bennett Jun 18 '16 at 04:53
  • Thanks everyone. You are indeed correct -- the object I was working with was configured no differently than the de facto standard and what everyone else referred to above. No `attributes` method, but sometime very similar to what @Jordan described. Thanks all for humoring me with this novice question. Learning more as the days go by and will certainly give back where possible. – Kurt W Jun 25 '16 at 01:37

1 Answers1

0

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"]]
scorix
  • 2,436
  • 19
  • 23
  • Thank you, this is helpful for when direct methods aren't available to access the data. In my case though, the folks above were correct--I just didn't understand how to access the object I was working with. – Kurt W Jun 25 '16 at 01:35