我该如何转换这个数组:
@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)
使用each
和inject
Ruby的?
收集/地图是更自然的方法.
@new_list = @list.map.with_index {|item, index| "#{index+1}. #{item}"}
Run Code Online (Sandbox Code Playgroud)
从ruby 1.9开始,你可以链接这样的枚举器