0

I have the following Strings. I want them to convert into Arrays like below in rails

"[\"Winter\", \"Summer\", \"Spring\"]" to ["Winter", "Summer", "Spring"]

"[\"IELTS\", \"GRE\", \"PTE\", \"SAT\"]" to ["IELTS", "GRE", "PTE", "SAT"]

How can i convert these

2

2 Answers 2

10

You can do it with JSON.

require 'json'

string = "[\"Winter\", \"Summer\", \"Spring\"]"
JSON.parse(string)
=> ["Winter", "Summer", "Spring"]
Sign up to request clarification or add additional context in comments.

Comments

1

just alternate solution (not safe) :

> string = "[\"Winter\", \"Summer\", \"Spring\"]"
> eval(string)
#=> ["Winter", "Summer", "Spring"]

Note: better option to parse with JSON

1 Comment

Please, never do things like this in production code. It may lead to a serious vulnerability.

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.