Search code examples
ruby-on-railsruby

Convert string array to array object


I want to import my CSV file to my database, my CSV file has following string and I need to parse it to a Ruby array object, how to do that?

"[\"Mt Roskill\", \"Sylvia Park\"]"

Solution

  • That example string looks like JSON. Use JSON.parse to return a Ruby array:

    require 'json'
    
    JSON.parse("[\"Mt Roskill\", \"Sylvia Park\"]")
    #=> ["Mt Roskill", "Sylvia Park"]