Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何转换此数组:
@list = ["one", "two", "three", "four"]
进入一个像这样的新数组,前面有键:
@new_list = ["1. one", "2. two", "3. three", "4. four"]
在 Ruby 中使用each和?inject
each
inject
收集/映射是这里更自然的方法。
@new_list = @list.map.with_index {|item, index| "#{index+1}. #{item}"}
从 ruby 1.9 开始,您可以像这样链接枚举器