Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have built a presenter class whose only job is to convert a given array into a string. I am test driving the solution, so I started with "[nil, nil, nil]" but each nil will eventually be replaced with letter. That functionality is being handled by another class.

I am trying now to build an interface whose only job is to convert that string back to an array. So I will need to convert e.g. "[\"a\", \"b\", nil]" back to ["a", "b", nil]. But I am stuck.

For example, I'd like to convert

"[nil, nil, nil]"

to

 [nil, nil, nil]

How could I do it?

share|improve this question
2  
I think you're being downvoted because it's not clear how your input can vary. If the only thing you ever need to convert is "[nil,nil,nil]" then you don't need to convert it all - just type [nil,nil,nil]. –  Max Jun 19 at 16:16
    
Where is this data coming from? –  tadman Jun 19 at 16:18
    
Where does the string come from? There's probably a better solution. –  p11y Jun 19 at 16:20
    
I have built a presenter class whose only job is to convert a given array into a string. I am test driving the solution, so I started with [nil, nil, nil] but each nil will eventually be replaced with letter. That functionality is being handled by another class. I asked the above question because I am building an interface whose only job is to convert that sting back to an array. So I will need to convert "[\"a\", \"b\", nil]" back to ["a", "b", nil]. –  user3245240 Jun 19 at 16:32
1  
@user3245240 what you want is called serializing or marshalling an object, which is a pretty common problem. See my answer for some options on how you could use existing libraries for that. –  p11y Jun 19 at 16:37

2 Answers 2

Just use:

eval(string)

eval("[nil, nil, nil]")

Caution:

This is a very insecure method and you have to use it only if you are completely sure that the string contains a SAFE array..

share|improve this answer
6  
This is very un-secure way to parse input, as it may contain un-expected data, which may be dangerous. –  Uri Agassi Jun 19 at 16:16
1  
Dont. Use. Eval. –  p11y Jun 19 at 16:18
1  
@p11y Sometimes you need to, but you better have a very good reason to. –  tadman Jun 19 at 16:19
    
Editing to add the disclaimer. –  Jorge de los Santos Jun 19 at 16:19
2  
Uri Agassi, p11y, anyone who upvoted those comments, and anyone who downvoted this answer, Give an alternative way of doing this without using eval. If you cannot, then retract what you wrote/did. –  sawa Jun 19 at 16:21

I am guessing that you are producing the string yourself on one side, for example:

arr = [nil, nil, nil]
str = arr.inspect
#=> "[nil, nil, nil]"

Whereas I would advise you to serialize the array using a format such as JSON, YAML or Ruby's built in Marshalling library.

JSON

require 'json'

arr = [nil, nil, nil]
str = JSON.dump(arr)
#=> "[null,null,null]"

JSON.load(str)
#=> [nil, nil, nil]

YAML

require 'yaml'

arr = [nil, nil, nil]
str = YAML.dump(arr)
#=> "---\n- \n- \n- \n"

YAML.load(str)
#=> [nil, nil, nil]

Marshal

arr = [nil, nil, nil]
str = Marshal.dump(arr)
#=> "\x04\b[\b000"

Marshal.load(str)
#=> [nil, nil, nil]
share|improve this answer
1  
None of the three ways you suggest would solve the OP's problem. Notice that, even the closest YAML cannot handle "[nil, nil, nil]"; it requires a different string "[null,null,null]". –  sawa Jun 19 at 16:31
    
@sawa Yes, my answer is only valid under the assumption that the OP controls the generation of the string as well. He just commented on his own question that this is the case, so I still think my answer is valid. –  p11y Jun 19 at 16:36
    
@sawa: OP has presented an X/Y problem, and actually wants to serialise/de-serialise data. I think p11y is correct. Question should ideally be re-written with current serialisation code, and "what now?" (I've had a try at that) –  Neil Slater Jun 19 at 19:38
    
This is a far better answer than any which strictly adhere to the OP's narrow problem definition. –  Mark Thomas Jun 19 at 23:19
    
p11y, Neil Slater, Mark Thomas: The problem was terrible. I agree with that. –  sawa Jun 20 at 2:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.