本文介绍了Ruby中的字符串到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的字符串 --- \n- aaaadddhh ddddggg\n- bbbdddddf ff ddd\n- cccf\ \n 转换为Ruby中的 array

I need my string: "---\n- aaaadddhh ddddggg\n- bbbdddddf ff ddd\n- cccf\n" to convert to an array in Ruby

这就是我想要的

["aaaadddhh ddddggg", "bbbdddddf ff ddd", "cccf"]


推荐答案

那是。使用 YAML.load(str)将其从字符串解组到对象中。

That's YAML. Use YAML.load(str) to unmarshal it from a string into an object.

> require 'yaml'
 => true
> YAML.load "---\n- aaaadddhh ddddggg\n- bbbdddddf ff ddd\n- cccf\n"
 => ["aaaadddhh ddddggg", "bbbdddddf ff ddd", "cccf"]

请注意,有在解组任意YAML时存在安全风险,因此请确保您传递给它的源是受信任的。

Be aware that there are security risks in unmarshalling arbitrary YAML, so be sure that the source you are passing into it is trusted.

这篇关于Ruby中的字符串到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:00