Reverse of Array.to_s? Sting to array method that reverses the to_s method when applied to an array?

I have an array that recursively contains only numbers, strings, and
other arrays of the same form.
So, e.g.,:
[1, 2, 3, [1, 2, 3, [1, 2, 3, “4”, 5], 5], 5].to_s
Is there a way safer than eval to reverse the to_s operation and
convert, e.g.,
“[1, 2, 3, [1, 2, 3, [1, 2, 3, “4”, 5], 5], 5]”
back into an array without using eval, which is so unlimited that it
strikes me as quite dangerous.

On 09/03/2013 12:33 PM, Carlo E. Prelz wrote:

worked correctly. So I’m a bit leery of it.
Carlo
Factors are going to be such things as “How often has word x followed
word y”, so they’re rather unpredictable. I don’t think pack/unpack is
a good solution. If I can trust JSON, then it’s the best choice I’ve
found. I’m only bothered by that weird error message. Which only
showed up once. But as I was typing in test cases, that makes it
difficult to reproduce. (I thought I was checking out how the API
worked, not debugging, so I didn’t keep a careful enough log to recreate
it.)

Still…as long as I limit myself to numbers, strings, and lists of same
JSON seems to work reliably. So far. I guess if it starts throwing
errors I can replace it with YAML or marshall. (Since the average
Array->string length is (so far) around 50 chars, overhead is quite
significant. YAML nearly doubles the length [well, a bit less]. And I
believe that Marshall also has a lot of overhead on short segments .
[Most of the numbers used will be <255, so 64 bits/integer is a bit
excessive.])

On 09/03/2013 12:04 PM, Charles H. wrote:

And in answer to the other question, above, it isn’t going to be a
bothered by the claim that a to string method didn’t exist for
Array.) Or perhaps I could modify it into a YAML string, and use YAML
to decode it.

So there are ways forwards. But I’ve been using eval, and it would be
nice if the replacement weren’t that much more work.

I just tried JSON again, and this time it worked. Perhaps it just
couldn’t deal with a class instance being present in an internal Array,
though it handled it fine when it was on the outer level. JSON is
clearly much more compact than YAML. It only added around 4 characters
over a straight string representation. So perhaps I just need to avoid
classes that use themselves to serialize and deserialize themselves, and
stick to the basic list representation.

if you want it for storing or transporting data, use different stuff
like JSON or Marshal

Subject: Re: reverse of Array.to_s? Sting to array method that reverses
the to_s method when applied to an ar
Date: Tue 03 Sep 13 12:04:37PM -0700
Sorry for the delay!

Quoting C. Hixson ([email protected]):

Marshall is plausible, but “Programming Ruby” warns that the format
is subject to change when the compiler changes. JSON said that
Array didn’t have a defined to string method. (I forget whether it
was “to_s” or not.) I found this a bit odd as my first tests had
worked correctly. So I’m a bit leery of it.

If you want to be in control of binary data at the bit level, use
Array#pack. There, for each numeric component of the array you can
specify how many bytes and what endianness to use.

Pack requires that you know how many elements you have, and of what
type, beforehand, in your array, so you have to write some code to
parse and in some way preserve the structure. Not knowing what factors
are responsible for the variability of your data, I cannot know if
this solution could be of any use…

Carlo

You could Regexp or String#split with the right criteria to form an
Array. Using this recursively you should be able to create nested
Arrays.

On 09/03/2013 11:34 AM, Hans M. wrote:

if you want it for storing or transporting data, use different stuff
like JSON or Marshal

Marshall is plausible, but “Programming Ruby” warns that the format is
subject to change when the compiler changes. JSON said that Array
didn’t have a defined to string method. (I forget whether it was “to_s”
or not.) I found this a bit odd as my first tests had worked
correctly. So I’m a bit leery of it.

And in answer to the other question, above, it isn’t going to be a fixed
structure, so no regex could match it. I suppose I could pull off the
start, and recurse whenever I encountered an internal list, but I’d
really rather use an off the shelf solution, if possible. YAML would
work, but I’d really prefer something a LOT less verbose.

One possibility that occurred to me was to just take the string and
modify it into a fake JSON data string, then compose it into a hash with
a JSON tag…but if I really trusted JSON to work, the only thing that
would gain me would be a bit of compactness. (Highly desirable, but not
if I don’t trust the underlying method. And I’m really bothered by the
claim that a to string method didn’t exist for Array.) Or perhaps I
could modify it into a YAML string, and use YAML to decode it.

So there are ways forwards. But I’ve been using eval, and it would be
nice if the replacement weren’t that much more work.