0

I have a string in Ruby:

[[nil,nil,nil,nil,nil,nil,nil,nil,nil,'Average of','Transaction Amount','dataset_test_3.SampleData100'],[nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,'Country','dataset_test_3.SampleData100']]

It is formatted perfectly as a multidimensional Array, and has 2 sub-arrays. (in some cases, it may have 3).

How can I create an actual Array out of this?

Appreciate any directions, have been look for a while on this to no avail whatsoever.

2
  • When supplying data, please reduce it to the bare minimum necessary to reproduce the problem you're seeing. Anything else beyond that is a waste of our time. Your question sounds like an XY question, where you're asking about question Y, when you should be asking about X. How did you get that string representation of the an array? That's very rare and usually is the result of trying to use the output of inspect or capturing a puts-type output. Commented Sep 12, 2014 at 15:55
  • If you could use a neutral format like JSON your life would be a lot easier. Commented Sep 12, 2014 at 16:29

2 Answers 2

2

You can also use YAML to parse your string into an array

require 'yaml'

array = YAML.load(input)
array.flatten 
# => ["nil", "nil", "nil", "nil", "nil", "nil", "nil", "nil", "nil", "Average of", "Transaction Amount", "dataset_test_3.SampleData100", "nil", "nil", "nil", "nil", "nil", "nil", "nil", "nil", "nil", "nil", "Country", "dataset_test_3.SampleData100"]
Sign up to request clarification or add additional context in comments.

2 Comments

Almost! If I do this though, the nil values in my string remain strings. I've tried to make them nil with gsub, like below, but it given an error. array.each do |s| s.gsub('\"nil\"',nil) end
Still brilliant to this day! I was passing a 2D array of integers via CLI to another process, and as long as I planted the entire array into a single string, this was the perfect method for converting it back to an array of integers. Cheers!
2

The answer for your question is eval().

You can use eval() to evaluate a string as "code" in ruby.

For example, in your case, if you have this string called input, which contains an array declaration then:

b = eval(input)
b # => [[nil,nil,nil,'Average of',....]]

Here is a great introduction for eval() from RubyMonk:

http://rubymonk.com/learning/books/5-metaprogramming-ruby-ascent/chapters/24-eval/lessons/63-eval

And after eval(), you can use flatten on your array, to flat it out to a one dimension one.

So, in short, eval(input).flatten would do the job. :) ( as Nimir suggested)

It's also worth noting this can be extremely dangerous to do on untrusted data. DO NOT ever evaluate arbitrary user data. (by tadman )

3 Comments

My string is named "input". If I do input.flatten, I get this error: undefined method `flatten' for #<String:0x00000000fdceb0> (NoMethodError)
You can't flatten a String.
It's also worth noting this can be extremely dangerous to do on untrusted data. DO NOT ever evaluate arbitrary user data.

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.