2

I have an array like so: ["data","data2"]

What would be the most Ruby-like way to convert this into a string with brackets included: "['data','data2']"

Thanks

2
  • 3
    ["data","data2"].to_s ? whats wrong with this? Commented May 25, 2012 at 17:46
  • @rubish It will escape the double quotes. I think the op want to use single quotes. Commented May 25, 2012 at 17:58

1 Answer 1

5

I can't say how "ruby-ish" this is, but it's what I would typically do:

puts "['" + array_of_strings.join("','") + "']"
Sign up to request clarification or add additional context in comments.

1 Comment

"['#{array_of_strings.join("','")}']" looks better to me

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.