1

I have the following OpenStruct object but if I call .class on it is a string. How do I turn it back into a OpenStruct object?

[
 #<OpenStruct source="hkepc.com", visits="8", visitBounceRate="37.5", avgTimeOnSite="199.375", pageviews="25", pageviewsPerVisit="3.125">, 
 #<OpenStruct source="1st-tag.co.uk", visits="6", visitBounceRate="100.0", avgTimeOnSite="0.0", pageviews="6", pageviewsPerVisit="1.0">, 
 #<OpenStruct source="facebook.com", visits="5", visitBounceRate="80.0", avgTimeOnSite="8.2", pageviews="6", pageviewsPerVisit="1.2">
]
6
  • This is not an OpenStruct object its an Array
    – Gerry
    Commented Mar 31, 2014 at 15:18
  • Can you please post a working example to reproduce the problem?
    – toro2k
    Commented Mar 31, 2014 at 15:20
  • so could I just do split? Commented Mar 31, 2014 at 15:20
  • @MaxRose-Collins the Object#class method always returns a String (ruby-doc.org/core-2.1.1/Object.html#method-i-class) - am I misunderstanding what you are asking? Also, you've posted an Array here as opposed to a single OpenStruct, so I assumed you are referring to an element of this array. Commented Mar 31, 2014 at 15:21
  • @AndreasKavountzis: no, it returns a class. Commented Mar 31, 2014 at 15:57

1 Answer 1

1

A little bit hacky, but I think achieves your goal:

require 'ostruct'

string_array = '[
 #<OpenStruct source="hkepc.com", visits="8", visitBounceRate="37.5", avgTimeOnSite="199.375", pageviews="25", pageviewsPerVisit="3.125">, 
 #<OpenStruct source="1st-tag.co.uk", visits="6", visitBounceRate="100.0", avgTimeOnSite="0.0", pageviews="6", pageviewsPerVisit="1.0">, 
 #<OpenStruct source="facebook.com", visits="5", visitBounceRate="80.0", avgTimeOnSite="8.2", pageviews="6", pageviewsPerVisit="1.2">
]'

string_array.gsub(/(\n|#<OpenStruct|\[|\]|\s+)/, '').split('>,').map do |attrs|
  struct_attrs = attrs.split(',')
  attrs = struct_attrs.inject({}) do |hash, elem|
    hash[elem.split('=').first] = elem.split('=').last.gsub('"', '')
    hash
  end

  OpenStruct.new(attrs)
end
7
  • but at the moment the array is actually a string, if you understand what i mean? Commented Mar 31, 2014 at 15:25
  • Ah, OK sorry that wasn't clear from the OP. So that mean that these are the .to_s versions of these objects most likely... I'm not sure of an approach that would allow you to reconstruct them as OpenStructs without having to parse out the attributes (as mentioned in your comment above). Commented Mar 31, 2014 at 15:28
  • That is pretty close to it! but it seems to add extra quotes and slashes? #<OpenStruct source="\"youtube.com\"", visits="\"71\"", visitBounceRate="\"39.436619718309856\"", avgTimeOnSite="\"83.29577464788733\"", pageviews="\"192\"", pageviewsPerVisit="\"2.704225352112676\""> Commented Mar 31, 2014 at 16:40
  • @MaxRose-Collins answer updated. Hopefully that works! Commented Mar 31, 2014 at 16:54
  • How would I preserve spaces within each <OpenStruct> ? For example #<OpenStruct country="UnitedStates", visits="425"> United States should have a space between it? Commented Apr 8, 2014 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.