Ruby 在迭代时返回迭代对象和索引

Ruby 在迭代时返回迭代对象和索引,ruby,Ruby,当然,我可以通过array.index[element]返回索引,但我正在寻找更自然的解决方案。类似于Rails关联中的代理所有者 Ruby 1.8.7 我想要输出什么?我想返回迭代的对象(在我的例子中是数组),以及迭代的次数(在每个_的例子中是索引,带有_索引)迭代的下一个和priviouse元素 作为输入,我得到了一个数组和迭代器(每个、映射、注入等)在ruby1.8中,有每个带有索引的\u 如果您希望在其他迭代器上使用该方法,如inject,map,…,并且如果您使用的是ruby1.9,那

当然,我可以通过
array.index[element]
返回索引,但我正在寻找更自然的解决方案。类似于Rails关联中的代理所有者

Ruby 1.8.7

我想要输出什么?我想返回迭代的对象(在我的例子中是数组),以及迭代的次数(在每个_的例子中是索引,带有_索引)迭代的下一个和priviouse元素


作为输入,我得到了一个数组和迭代器(每个、映射、注入等)

在ruby1.8中,有
每个带有索引的\u

如果您希望在其他迭代器上使用该方法,如
inject
map
,…,并且如果您使用的是ruby1.9,那么可以将
枚举器#和_index
方法附加到各种迭代器上

使用
可枚举的#每个组件
。以下是来自的副本。它应该在ruby 1.8.7上运行


  • 每个(n){…}
  • 每项(n)
为每个连续元素数组迭代给定的块。如果未给定块,则返回枚举数


使用此选项,您可以给出一个数组:

array = [1,2,3,{:name => "Peter"}, "hello"]
array.each do |element| # it can be "inject", "map" or other iterators
  # How to return object "array" and position of "element"
  # also next and priviouse "element"
end
或者:

['a', 'b', 'c', 'd', 'e'].each_cons(3).to_a

# => ["a", "b", "c"], ["b", "c", "d"], ["c", "d", "e"]]
如果你想要印度菜

['a', 'b', 'c', 'd', 'e'].each_cons(3) {|previous, current, nekst|
    puts "#{previous} - #{current} - #{nekst}"
}

# => a - b - c
# => b - c - d
# => c - d - e
您可以非常普遍地将数组传递给其他枚举数,例如,使用
inject

['a', 'b', 'c', 'd', 'e'].each_cons(3).to_a.each_with_index {|(previous, current, nekst), i|
    puts "#{i + 1}. #{previous} - #{current} - #{nekst}"
}

# => 1. a - b - c
# => 2. b - c - d
# => 3. c - d - e
['a'、'b'、'c'、'd'、'e'].每一个都包含(3)到a.注入('''){str,(先前的、当前的、新的)|
str“abcbcdcde”

无法测试,但如果正确记住:

['a', 'b', 'c', 'd', 'e'].each_cons(3).to_a.inject(''){|str, (previous, current, nekst)|
    str << previous+current+nekst
}

# => "abcbcdcde"
a=[4,3,3,1,6,6,1]

p a.enum_for(:每个_带有_索引)。injection([]){| m,args | mEdit:这是针对1.9的,现在我看到您的问题明确提到了1.8.7。我将把它放在这里作为参考,但如果它干扰了任何人,我可以删除它

sawa已经指出了带有索引的枚举器:

示例:

p a.each_with_index.inject([]){ |m,args| m<<args }
与此问题相关的还有
枚举器#next
枚举器#peek
,它们将分别返回枚举器中的下一个对象,而不向前移动内部位置


Ruby的
每个带有索引的功能都可以轻松地重新创建:

>> [1,2,3].map.with_index { |x, i| [x,i] }
=> [[1, 0], [2, 1], [3, 2]]
>> [1,2,3].each_cons(2).with_index { |(x, y), i| p [x,y,i] }
[1, 2, 0]
[2, 3, 1]
=> nil

每个带有索引的\u是否都有效?如果是,您可以执行
数组[i-1]
数组[i+1]
。关于
用索引插入\u
用索引选择\u
?:)我认为有一些常见的方法什么是期望的输出?最好显示实际的输入/输出,而不是试图用文字解释。因此,没有办法知道哪个对象正在迭代以及优先级和下一个值是什么?您使用的是ruby 1.9吗?如果是的话如果将
与索引一起使用,则可以同时获取对象和索引。使用索引
i
,可以执行
数组[i-1]
获取上一个元素。你想用更简单的方式访问上一个/下一个元素吗?我明白了。这可能需要更多的想法。我现在还没有答案。非常聪明和熟练:)我会使用它。但这不是我真正想要的。你能修复链接,并澄清它是否会给出
'a',b',c'
然后
'“b”,“c”,“d”
,或
“a”,“b”,“c”
,然后
“d”,“e”,“f”
?sawa,最后我使用了这种方法。我唯一不喜欢的是,我不能得到第一个和最后一个con,如[nil,a,b]和[d,e,nil]或者任何确定第一个元素为中间元素的方法。因为现在我无法正确迭代,因为我得到了
Array.count-2
cons@fl00r我很高兴您接受了我的解决方案。您遇到的问题很小:迭代
[nil,*original\u array,nil]
而不是
原始的数组
。Rails 1.8.7?我相信它适用于1.9是的,这是Ruby 1.9。Rails 1.8.7已经很旧了,顺便说一句;-)对于1.8.7,你可以用
替换
.map.with\u index
。每个\u都用\u index.map
。当然你可以,但我希望最终看到每个人都转到1.9。它真的已经过时一段时间了y代码仍然不能与最新的Ruby版本一起使用的借口已经不多了。我认为这是
peek
,而不是
poke
。请注意,xs.inject([]){m,args | m这是我发现的隐藏在堆栈溢出问题低级别答案中的Ruby智慧中最有用的片段之一。
>> [1,2,3].map.with_index { |x, i| [x,i] }
=> [[1, 0], [2, 1], [3, 2]]
>> [1,2,3].each_cons(2).with_index { |(x, y), i| p [x,y,i] }
[1, 2, 0]
[2, 3, 1]
=> nil
ary = %w[zero one two three]
ary.zip((0 .. (ary.size - 1)).to_a).to_a # => [["zero", 0], ["one", 1], ["two", 2], ["three", 3]]

ary.zip((0 .. (ary.size - 1)).to_a).each do |a, i|
  puts "this element: #{a}"
  puts "previous element: #{ary[i - 1]}" if (i > 0)
  puts "next element: #{ary[i + 1]}" if (i < (ary.size - 1))
  puts
end
# >> this element: zero
# >> next element: one
# >> 
# >> this element: one
# >> previous element: zero
# >> next element: two
# >> 
# >> this element: two
# >> previous element: one
# >> next element: three
# >> 
# >> this element: three
# >> previous element: two
# >> 
module Enumerable
  def my_each_with_index
    self.zip((0 .. (self.size - 1)).to_a).each do |a, i|
      yield a, i
    end
  end
end

ary.my_each_with_index { |a,i| puts "index: #{i} element: #{a}" }
# >> index: 0 element: zero
# >> index: 1 element: one
# >> index: 2 element: two
# >> index: 3 element: three