相关疑难解决方法(0)

Array#each vs. Array #map

hash = { "d" => [11, 22], "f" => [33, 44, 55] }

# case 1
hash.map {|k,vs| vs.map {|v| "#{k}:#{v}"}}.join(",")
=> "d:11,d:22,f:33,f:44,f:55"

# case 2
hash.map {|k,vs| vs.each {|v| "#{k}:#{v}"}}.join(",")
=> "11,22,33,44,55"
Run Code Online (Sandbox Code Playgroud)

唯一的区别是案例1使用vs.map,案例2使用vs.each.

这里发生了什么?

ruby arrays enumerable

89
推荐指数
3
解决办法
5万
查看次数

标签 统计

arrays ×1

enumerable ×1

ruby ×1