在Ruby中从数组创建一个列表

0 ruby inject map

我该如何转换这个数组:

@list = ["one", "two", "three", "four"]
Run Code Online (Sandbox Code Playgroud)

使用前面的键进入这样的新数组:

@new_list = ["1. one", "2. two", "3. three", "4. four"]
Run Code Online (Sandbox Code Playgroud)

使用eachinjectRuby的?

Fre*_*ung 8

收集/地图是更自然的方法.

@new_list = @list.map.with_index {|item, index| "#{index+1}. #{item}"}
Run Code Online (Sandbox Code Playgroud)

从ruby 1.9开始,你可以链接这样的枚举器