Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby枚举器链接_Ruby_List_Enumerator - Fatal编程技术网

Ruby枚举器链接

Ruby枚举器链接,ruby,list,enumerator,Ruby,List,Enumerator,在这个例子中 [1, 2, 3].each_with_index.map{|i, j| i * j} # => [0, 2, 6] [1, 2, 3, 4].map.find {|i| i == 2} # => 2 我的理解是,由于each_with_index枚举器链接到map,map的行为类似于each_with_index,在块内传递一个索引,并返回一个新数组 为此, [1, 2, 3].map.each_with_index{|i, j| i * j} # => [

在这个例子中

[1, 2, 3].each_with_index.map{|i, j| i * j}
# => [0, 2, 6]
[1, 2, 3, 4].map.find {|i| i == 2}
# => 2
我的理解是,由于
each_with_index
枚举器链接到
map
map
的行为类似于
each_with_index
,在块内传递一个索引,并返回一个新数组

为此,

[1, 2, 3].map.each_with_index{|i, j| i * j}
# => [0, 2, 6] 
我不知道该怎么解释它

在这个例子中

[1, 2, 3].each_with_index.map{|i, j| i * j}
# => [0, 2, 6]
[1, 2, 3, 4].map.find {|i| i == 2}
# => 2
我希望输出是
[2]
,假设
map
链接到
find
,并且
map
将返回一个新数组

此外,我看到:

[1, 2, 3, 4].find.each_with_object([]){|i, j| j.push(i)}
# => [1]

[1, 2, 3, 4].each_with_object([]).find{|i, j| i == 3}
# => [3, []]

您能告诉我如何解释和理解Ruby中的枚举数链吗?

您提到的方法是在
可枚举对象上定义的。根据是否传递块,这些方法的行为会有所不同

  • 当您不传递块时,它们通常返回一个
    枚举器
    对象
    ,您可以将进一步的方法链接到该对象,如
    每个带有_索引的
    带有_索引的
    映射
    ,等等
  • 当您将一个块传递给这些方法时,它们会返回不同类型的对象,具体取决于该方法的意义。
    • 对于像
      find
      这样的方法,它的目的是找到满足条件的第一个对象,而将其包装在数组中没有特别意义,因此它返回该对象
    • 对于
      select
      reject
      等方法,它们的目的是返回所有相关对象,因此它们不能返回单个对象,并且必须将它们包装在数组中(即使相关对象恰好是单个对象)

您可能会发现分解这些表达式并使用IRB或PRY查看Ruby在做什么很有用。让我们从以下几点开始:

[1,2,3].each_with_index.map { |i,j| i*j }

这并不奇怪。但是
enum1
没有块。相反,我们发送的方法是:

我们看到数组
[1,0]
是进入块的第一个元素
enum2
。“消歧”应用于该数组,为块变量赋值:

i => 1
j => 0
也就是说,Ruby正在设置:

i,j = [1,0]
现在,我们可以调用
enum2
,方法是将
和块一起发送给它:

enum2.each { |i,j| i*j }
  #=> [0, 2, 6]
下一步考虑:

[1,2,3].map.each_with_index { |i,j| i*j }
我们有:

enum3 = [1,2,3].map
  #=> #<Enumerator: [1, 2, 3]:map>
enum3.to_a
  #=> [1, 2, 3]
enum4 = enum3.each_with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:map>:each_with_index>
enum4.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum4.each { |i,j| i*j }
  #=> [0, 2, 6]
enum3 = [1,2,3].map
  #=> #<Enumerator: [1, 2, 3]:map>
enum3.to_a
  #=> [1, 2, 3]
enum5 = enum3.with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:map>:with_index>
enum5.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum5.each { |i,j| i*j }
  #=> [0, 2, 6]
enum6 = [1,2,3].select
  #=> #<Enumerator: [1, 2, 3]:select>
enum6.to_a
  #=> [1, 2, 3]
enum7 = enum6.with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:select>:with_index>
enum7.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum8 = enum7.with_object({})
  #=> #<Enumerator: #<Enumerator: #<Enumerator: [1, 2, 3]:
  #     select>:with_index>:with_object({})>
enum8.to_a
  #=> [[[1, 0], {}], [[2, 1], {}], [[3, 2], {}]]
我们有:

enum3 = [1,2,3].map
  #=> #<Enumerator: [1, 2, 3]:map>
enum3.to_a
  #=> [1, 2, 3]
enum4 = enum3.each_with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:map>:each_with_index>
enum4.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum4.each { |i,j| i*j }
  #=> [0, 2, 6]
enum3 = [1,2,3].map
  #=> #<Enumerator: [1, 2, 3]:map>
enum3.to_a
  #=> [1, 2, 3]
enum5 = enum3.with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:map>:with_index>
enum5.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum5.each { |i,j| i*j }
  #=> [0, 2, 6]
enum6 = [1,2,3].select
  #=> #<Enumerator: [1, 2, 3]:select>
enum6.to_a
  #=> [1, 2, 3]
enum7 = enum6.with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:select>:with_index>
enum7.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum8 = enum7.with_object({})
  #=> #<Enumerator: #<Enumerator: #<Enumerator: [1, 2, 3]:
  #     select>:with_index>:with_object({})>
enum8.to_a
  #=> [[[1, 0], {}], [[2, 1], {}], [[3, 2], {}]]
我们有:

enum3 = [1,2,3].map
  #=> #<Enumerator: [1, 2, 3]:map>
enum3.to_a
  #=> [1, 2, 3]
enum4 = enum3.each_with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:map>:each_with_index>
enum4.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum4.each { |i,j| i*j }
  #=> [0, 2, 6]
enum3 = [1,2,3].map
  #=> #<Enumerator: [1, 2, 3]:map>
enum3.to_a
  #=> [1, 2, 3]
enum5 = enum3.with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:map>:with_index>
enum5.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum5.each { |i,j| i*j }
  #=> [0, 2, 6]
enum6 = [1,2,3].select
  #=> #<Enumerator: [1, 2, 3]:select>
enum6.to_a
  #=> [1, 2, 3]
enum7 = enum6.with_index
  #=> #<Enumerator: #<Enumerator: [1, 2, 3]:select>:with_index>
enum7.to_a
  #=> [[1, 0], [2, 1], [3, 2]]
enum8 = enum7.with_object({})
  #=> #<Enumerator: #<Enumerator: #<Enumerator: [1, 2, 3]:
  #     select>:with_index>:with_object({})>
enum8.to_a
  #=> [[[1, 0], {}], [[2, 1], {}], [[3, 2], {}]]
然后应用消歧来为块变量赋值:

i => 1
j => 0
h => {}

请注意,
enum8
显示在
enum8.to_a
的三个元素中的每一个元素中都传递了一个空哈希值,但这当然只是因为Ruby不知道传入第一个元素后哈希值会是什么样子。

我明白了这一点,但如何像我上面所说的那样解释链
[1,2,3]。每个元素都带有_index.map{i,j | i*j}
[1,2,3].map.每个带有{u索引{i,j | i*j}
的_返回完全相同的结果,我认为我相信的结果是非常好地提出的