如何理解 ruby 枚举器链中的工作流



下面的代码会产生两种不同的结果。

letters = %w(e d c b a)
letters.group_by.each_with_index { |item, index| index % 3 }
#=> {0=>["e", "b"], 1=>["d", "a"], 2=>["c"]}
letters.each_with_index.group_by { |item, index| index % 3 }
#=> {0=>[["e", 0], ["b", 3]], 1=>[["d", 1], ["a", 4]], 2=>[["c", 2]]}

我认为执行流是从右到左,数据流是从左到右。块应作为参数从右到左传递。

使用puts,我观察到块是在内each执行的。

在第一个链中,group_by应该向each索要数据,each会返回index%3的结果,group_by应该处理结果并将其交给另一个区块。但是这个区块是如何通过的呢?如果块在each中执行,each不会传递两个参数itemindex而只传递一个参数item

在第二个链中,根据我的理解,each_with_index将首先从each方法接收数据;each屈服于index%3.在这种情况下,each_with_index如何处理index%3

看来我的理解有些不对。谁能详细说明这两个例子,并给出这种情况的一般工作流程?

代理对象

执行和数据流都是从左到右的,就像 Ruby 中的任何方法调用一样。

从概念上讲,它可以帮助从右到左读取Enumerator调用链,因为它们是一种代理对象。

调用没有块,他们只记住调用方法的顺序。然后,仅在需要时才能真正调用该方法,例如,当Enumerator转换回Array或元素打印在屏幕上时。

如果在链的末端没有调用这样的方法,基本上什么都不会发生:

[1,2,3].each_with_index.each_with_index.each_with_index.each_with_index
# #<Enumerator: ...>
[1,2,3].each_with_index.each_with_index.each_with_index.each_with_index.to_a
# [[[[[1, 0], 0], 0], 0], [[[[2, 1], 1], 1], 1], [[[[3, 2], 2], 2], 2]]

这种行为使得处理非常大的对象流成为可能,而无需在方法调用之间传递巨大的数组。如果不需要输出,则不计算任何内容。如果最后需要 3 个元素,则仅计算 3 个元素。

代理模式在 Rails 中大量使用,例如ActiveRecord::Relation

@person = Person.where(name: "Jason").where(age: 26)

在这种情况下,启动 2 个数据库查询效率低下。但是,您只能在链接方法的末尾知道这一点。这里有一个相关的答案(Rails ActiveRecord如何在没有多个查询的情况下链接"where"子句?)

我的枚举器

这是一个快速而肮脏的MyEnumerator课。它可能有助于您理解问题中方法调用的逻辑:

class MyEnumerator < Array
def initialize(*p)
@methods = []
@blocks = []
super
end
def group_by(&b)
save_method_and_block(__method__, &b)
self
end
def each_with_index(&b)
save_method_and_block(__method__, &b)
self
end
def to_s
"MyEnumerable object #{inspect} with methods : #{@methods} and #{@blocks}"
end
def apply
result = to_a
puts "Starting with #{result}"
@methods.zip(@blocks).each do |m, b|
if b
puts "Apply method #{m} with block #{b} to #{result}"
else
puts "Apply method #{m} without block to #{result}"
end
result = result.send(m, &b)
end
result
end
private
def save_method_and_block(method, &b)
@methods << method
@blocks << b
end
end
letters = %w[e d c b a]
puts MyEnumerator.new(letters).group_by.each_with_index { |_, i| i % 3 }.to_s
# MyEnumerable object ["e", "d", "c", "b", "a"] with methods : [:group_by, :each_with_index] and [nil, #<Proc:0x00000001da2518@my_enumerator.rb:35>]
puts MyEnumerator.new(letters).group_by.each_with_index { |_, i| i % 3 }.apply
# Starting with ["e", "d", "c", "b", "a"]
# Apply method group_by without block to ["e", "d", "c", "b", "a"]
# Apply method each_with_index with block #<Proc:0x00000000e2cb38@my_enumerator.rb:42> to #<Enumerator:0x00000000e2c610>
# {0=>["e", "b"], 1=>["d", "a"], 2=>["c"]}
puts MyEnumerator.new(letters).each_with_index.group_by { |_item, index| index % 3 }.to_s
# MyEnumerable object ["e", "d", "c", "b", "a"] with methods : [:each_with_index, :group_by] and [nil, #<Proc:0x0000000266c220@my_enumerator.rb:48>]
puts MyEnumerator.new(letters).each_with_index.group_by { |_item, index| index % 3 }.apply
# Apply method each_with_index without block to ["e", "d", "c", "b", "a"]
# Apply method group_by with block #<Proc:0x0000000266bd70@my_enumerator.rb:50> to #<Enumerator:0x0000000266b938>
# {0=>[["e", 0], ["b", 3]], 1=>[["d", 1], ["a", 4]], 2=>[["c", 2]]}

最新更新