PHP尽管有缺点,但在这方面做得很好。数组和哈希之间没有区别(也许我很天真,但这对我来说显然是正确的),要遍历其中任何一个,你都可以这样做

foreach (array/hash as $key => $value)

在Ruby中,有很多方法可以做到这一点:

array.length.times do |i|
end

array.each

array.each_index

for i in array

哈希更有意义,因为我总是用

hash.each do |key, value|

为什么我不能对数组这样做?如果我只想记住一个方法,我想我可以使用each_index(因为它使索引和值都可用),但是必须使用array[index]而不是value是很烦人的。


哦,对了,我忘了array。each_with_index。然而,这个很糟糕,因为它是|值,键|和哈希。每个键|,值|!这不是很疯狂吗?


当前回答

以下是你问题中列出的四个选项,按控制自由程度排列。您可能想要使用不同的方法,这取决于您需要什么。

简单回顾一下价值观: array.each 简单地看一下索引: array.each_index 遍历索引+索引变量: 对于数组中的I 控制循环计数+索引变量: array。length。乘以do | I |

其他回答

The other answers are just fine, but I wanted to point out one other peripheral thing: Arrays are ordered, whereas Hashes are not in 1.8. (In Ruby 1.9, Hashes are ordered by insertion order of keys.) So it wouldn't make sense prior to 1.9 to iterate over a Hash in the same way/sequence as Arrays, which have always had a definite ordering. I don't know what the default order is for PHP associative arrays (apparently my google fu isn't strong enough to figure that out, either), but I don't know how you can consider regular PHP arrays and PHP associative arrays to be "the same" in this context, since the order for associative arrays seems undefined.

因此,Ruby的方式对我来说似乎更加清晰和直观。:)

这将遍历所有元素:

array = [1, 2, 3, 4, 5, 6]
array.each { |x| puts x }

# Output:

1
2
3
4
5
6

这将遍历所有给你值和索引的元素:

array = ["A", "B", "C"]
array.each_with_index {|val, index| puts "#{val} => #{index}" }

# Output:

A => 0
B => 1
C => 2

从你的问题我不太确定你在找哪一个。

在Ruby 2.1中,each_with_index方法被移除。 相反,您可以使用each_index

例子:

a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }

生产:

0 -- 1 -- 2 --

我认为没有唯一正确的方法。有许多不同的迭代方法,每种方法都有自己的细分市场。

对于许多用法,每个都足够了,因为我不经常关心索引。 each_ with _index的作用类似于Hash#each——你得到值和索引。 Each_index—只有索引。我不经常用这个。相当于"length.times"。 Map是迭代的另一种方式,当你想将一个数组转换为另一个数组时很有用。 Select是要选择子集时使用的迭代器。 Inject在生成总和或乘积或收集单个结果时很有用。

看起来要记住的东西太多了,但别担心,你不知道所有的单词也能应付过去。但是当您开始学习和使用不同的方法时,您的代码将变得越来越清晰,您将走上精通Ruby的道路。

如果使用可枚举的mixin(如Rails所做的那样),则可以执行与列出的php代码片段类似的操作。只需使用each_slice方法并将散列平铺。

require 'enumerator' 

['a',1,'b',2].to_a.flatten.each_slice(2) {|x,y| puts "#{x} => #{y}" }

# is equivalent to...

{'a'=>1,'b'=>2}.to_a.flatten.each_slice(2) {|x,y| puts "#{x} => #{y}" }

更少的猴子补丁需要。

但是,当您有一个递归数组或数组值的散列时,这确实会导致问题。在ruby 1.9中,这个问题通过flatten方法的参数来解决,该方法指定递归的深度。

# Ruby 1.8
[1,2,[1,2,3]].flatten
=> [1,2,1,2,3]

# Ruby 1.9
[1,2,[1,2,3]].flatten(0)
=> [1,2,[1,2,3]]

至于这是否是代码气味的问题,我不确定。通常当我不得不竭尽全力迭代某些内容时,我就会后退一步,意识到我在错误地处理问题。