#each_with_object和#inject都可以用来构建哈希.
例如:
matrix = [['foo', 'bar'], ['cat', 'dog']]
some_hash = matrix.inject({}) do |memo, arr|
memo[arr[0]] = arr
memo # no implicit conversion of String into Integer (TypeError) if commented out
end
p some_hash # {"foo"=>["foo", "bar"], "cat"=>["cat", "dog"]}
another_hash = matrix.each_with_object({}) do |arr, memo|
memo[arr[0]] = arr
end
p another_hash # {"foo"=>["foo", "bar"], "cat"=>["cat", "dog"]}
Run Code Online (Sandbox Code Playgroud)
两者之间的关键差异之一是#each_with_object跟踪memo整个迭代,而#inject设置memo等于块在每次迭代时返回的值.
另一个区别是顺序或块参数.
有没有在这里传达意图?反转两种类似方法的块参数没有意义.
他们有不同的血统.
each_with_object 已于2007年添加到Ruby 1.9中inject 1980年回到Smalltalk我想如果语言是从开头设计的两种方法,他们可能会期望参数顺序相同.但事实并非如此.inject自Ruby开始以来一直存在,而each_with_object仅在10年后才被添加.
inject 期望参数与Smalltalk的顺序相同 inject:into:
collection inject: 0 into: [ :memo :each | memo + each ]
Run Code Online (Sandbox Code Playgroud)
这是左折.您可以将该集合视为从左侧折叠起来的长条纸,折叠功能的滑动窗口始终是已经折叠的部分以及剩余纸条的下一个元素
# (memo = 0 and 1), 2, 3, 4
# (memo = 1 and 2), 3, 4
# (memo = 3 and 3), 4
# (memo = 6 and 4)
Run Code Online (Sandbox Code Playgroud)
由于所有最初的方法Enumerable都取自Smalltalk而且Matz不想混淆那些熟悉Smalltalk的人,因此遵循Smalltalk惯例是有道理的.
任何人都不能有先见之明知道会在2007 each_with_object年引入Ruby 1.9 时发生,并且参数的顺序反映了方法名称的词法顺序,即each ... object.
因此,由于历史原因,这两种方法期望不同顺序的论证.