Ruby: Parsing a string representation of nested arrays into an Array?

后端 未结 4 483
梦毁少年i
梦毁少年i 2020-12-03 14:01

Let\'s say I had the string

\"[1,2,[3,4,[5,6]],7]\"

How would I parse that into the array

[1,2,[3,4,[5,6]],7]
4条回答
  •  盖世英雄少女心
    2020-12-03 14:51

    The same can be done using Ruby standard libaray YAML as below :

    require 'yaml'
    s = "[1,2,[3,4,[5,6]],7]"
    YAML.load(s)
    # => [1, 2, [3, 4, [5, 6]], 7]
    

提交回复
热议问题