Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 将数组从嵌套数组解析为字符串_Ruby_Arrays_Parsing_Nested - Fatal编程技术网

Ruby 将数组从嵌套数组解析为字符串

Ruby 将数组从嵌套数组解析为字符串,ruby,arrays,parsing,nested,Ruby,Arrays,Parsing,Nested,假设我有嵌套数组,如: nested = [ [0.5623507523876472, ["h", "e", "l", "l", "o"]], [0.07381531933500263, ["h", "a", "l", "l", "o"]], [0.49993338806153054, ["n", "i", "h", "a", "o"]], [0.6499234734532127, ["

假设我有嵌套数组,如:

nested = [
             [0.5623507523876472, ["h", "e", "l", "l", "o"]], 
             [0.07381531933500263, ["h", "a", "l", "l", "o"]], 
             [0.49993338806153054, ["n", "i", "h", "a", "o"]], 
             [0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
         ]
最初我想把它转换成散列。但首先我必须将数组(上面的示例[“h”、“e”、“l”、“l”、“o”])转换为“hello”

所以我的问题是如何将
嵌套的
转换为:

[ 
   [0.5623507523876472, "hello"],
   [0.07381531933500263, "hallo"],
   [0.49993338806153054, "nihao"],
   [0.6499234734532127, "konnichiwa"]
]
如果不想销毁嵌套的源数组
: 使用:

如果要销毁源数组
nested
使用方法:

nested = [
             [0.5623507523876472, ["h", "e", "l", "l", "o"]], 
             [0.07381531933500263, ["h", "a", "l", "l", "o"]], 
             [0.49993338806153054, ["n", "i", "h", "a", "o"]], 
             [0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
         ]

nested_map = nested.map { |a,b| [a,b.join] }
# => [[0.5623507523876472, "hello"],
#     [0.07381531933500263, "hallo"],
#     [0.49993338806153054, "nihao"],
#     [0.6499234734532127, "konnichiwa"]]
nested = [
             [0.5623507523876472, ["h", "e", "l", "l", "o"]], 
             [0.07381531933500263, ["h", "a", "l", "l", "o"]], 
             [0.49993338806153054, ["n", "i", "h", "a", "o"]], 
             [0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
         ]

nested.map! { |a,b| [a,b.join] }
# => [[0.5623507523876472, "hello"],
#     [0.07381531933500263, "hallo"],
#     [0.49993338806153054, "nihao"],
#     [0.6499234734532127, "konnichiwa"]]