在Ruby中遍历数组有多种方法,下面列举了几种常用的方法:
使用each方法:array = [1, 2, 3, 4, 5]array.each do |item| puts itemend使用for循环:array = [1, 2, 3, 4, 5]for item in array puts itemend使用each_with_index方法遍历数组并获取索引:array = [1, 2, 3, 4, 5]array.each_with_index do |item, index| puts "Index: #{index}, Value: #{item}"end使用map方法:array = [1, 2, 3, 4, 5]new_array = array.map { |item| item * 2 }puts new_array这些方法可以帮助你正确遍历数组并处理其中的元素。选择合适的方法取决于你的需求和个人喜好。


