bra*_*boy 0 ruby arrays string enumeration collect
我有一个String数组,我想转换为特定的格式.例如
y = ["hello","how","you"]
Run Code Online (Sandbox Code Playgroud)
输出应该是以下完全字符串
[["hello","hello"],["how","how"],["you","you"]]
Run Code Online (Sandbox Code Playgroud)
我目前使用以下方式对我来说很好,但我需要知道在Ruby中有更好的方法
"[#{y.collect {|x| "[#{["\"#{x}\"", "\"#{x}\""].join(",")}]" }.join(",").to_s}]"
Run Code Online (Sandbox Code Playgroud)
您可以使用zip:
y.zip(y).inspect
Run Code Online (Sandbox Code Playgroud)
编辑 - 只是注意到你是在追求一个字符串 - 我认为检查应该做的工作.