2011-03-26 82 views
4
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 

当然我可以通过array.index[element]返回索引,但我在寻找更自然的解决方案。就像Rails的协会proxy_owner迭代时返回迭代对象和索引

的Ruby 1.8.7

我要输出什么?我想返回对象,我迭代(在我的情况下数组),迭代次数(each_with_index的情况下索引)和迭代的priviouse元素。

作为输入我有一个阵列和迭代器(每个,地图,注入等)

+1

不'each_with_index'工作?如果是这样,你可以做'array [i-1]','array [i + 1]'。 – sawa 2011-03-26 19:58:37

+0

'inject_with_index'或'select_with_index'怎么办? :)我认为那里提出了共同的方法 – fl00r 2011-03-26 20:00:47

+0

什么是期望的输出?显示实际的输入/输出比试图用文字解释更好。 – tokland 2011-03-26 22:02:35

回答

6

使用Enumerable#each_cons。以下是ruby1.8.7 rdoc的副本。它应该在红宝石1.8.7上工作。


  • each_cons(N){...}
  • each_cons(n)的

迭代连续元件的每个阵列的给定的块。如果没有给出块,则返回一个枚举器。


有了这个,你可以给一个数组:

['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 

如果你想指数之,

['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 

可以传递数组相当普遍的其他普查员,例如,inject

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

# => "abcbcdcde" 
+0

非常聪明,熟练:)我会用它。但它实际上并不是我正在寻找的 – fl00r 2011-03-26 21:00:21

+1

你可以修复这个链接,并澄清它是否会给'a','b','c',然后''b','c','d' '或'a','b','c',然后是'd','e','f'? – 2011-03-26 22:03:23

+0

@Andrew感谢他们指出。我修好了它。 – sawa 2011-03-27 01:50:43

5

在ruby1.8的,有each_with_index

如果您希望在其他迭代器像injectmap,...,如果你正在使用ruby1.9,有Enumerator#with_index方法,你可以连接到不同的迭代器。

Enumerator#with_index

+0

所以没有办法知道这个对象是迭代的,什么是priviouse和next值? – fl00r 2011-03-26 20:07:18

+0

你使用红宝石1.9吗?如果你使用'with_index',你可以同时获得对象和索引。使用索引'i',你可以使用'array [i-1]'获得前一个。你甚至想要更简单的方法来访问上一个/下一个元素? – sawa 2011-03-26 20:08:01

+0

对不起,没有。稀土元素(1.8.7) – fl00r 2011-03-26 20:08:19

1

无法测试,但如果没记错的话:

a = [4, 3, 3, 1, 6, 6,1] 
p a.enum_for(:each_with_index).inject([]){ |m,args| m<<args } 
#=> [[4, 0], [3, 1], [3, 2], [1, 3], [6, 4], [6, 5], [1, 6]] 

替换选择注入,拒绝或什么的。随着红宝石1.9:

p a.each_with_index.inject([]){ |m,args| m<<args } 
+1

注意xs.inject([]){| m,args | m << args} = xs.map {| x | x} = xs.to_a。无论如何,在Ruby 1.9(可能是1.8.7),你应该能够简单地写一个each_with_index.to_a – tokland 2011-05-20 17:41:06

1

编辑:这是1.9,我现在看到你的问题明确提到1.8.7。我会把它留在这里作为参考,但如果它打扰任何人,我可以删除它。

泽已经指出Enumerator#with_index

http://www.ruby-doc.org/core/classes/Enumerator.html#M000303

例子:

>> [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 

也与此相关的问题是Enumerator#nextEnumerator#peek,这将返回的下一个对象的枚举,分别而无需向内移动内部位置。

http://www.ruby-doc.org/core/classes/Enumerator.html#M000307

http://www.ruby-doc.org/core/classes/Enumerator.html#M000308

+0

Rails 1.8.7?我相信它是1.9 – fl00r 2011-03-26 21:47:13

+0

是的,这是Ruby 1.9。 Rails 1.8.7是相当古老的顺便说一句。 ;-) – 2011-03-26 21:48:13

+1

对于1.8.7,你可以用'.each_with_index.map'替换'.map.with_index'。 – 2011-03-26 22:05:28

3

Ruby的each_with_index功能可以很容易地重新创建:

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 
+0

这是我发现的最有用的红宝石智慧片段之一在StackOverflow问题的低排名答案中。 – 2016-03-31 18:17:41