如何与“each”迭代器中的前一项进行比较



更新:对不起,我修复了我的程序:

a = [ 'str1' , 'str2', 'str2', 'str3'  ]
name = ''
a.each_with_index do |x, i |
  if x == name
    puts "#{x} found duplicate."
  else 
    puts x
    name = x  if i!= 0 
  end
end

     output: 
str1
str2
str2 found duplicate.
str3

ruby语言中,有没有其他漂亮的方法可以做同样的事情?

顺便说一句,实际上。在我的实际情况中,aActiveRecord::Relation

谢谢。

each_cons可能存在的问题是它在n-1对中迭代(如果Enumerable的长度是n)。在某些情况下,这意味着您必须分别处理第一个(或最后一个)元素的边情况。

在这种情况下,实现类似于each_cons的方法非常容易,但它将为第一个元素产生(nil, elem0)(而不是each_cons,它产生(elem0, elem1):

module Enumerable
  def each_with_previous
    self.inject(nil){|prev, curr| yield prev, curr; curr}
    self
  end
end

您可以使用each_cons:

irb(main):014:0> [1,2,3,4,5].each_cons(2) {|a,b| p "#{a} = #{b}"}
"1 = 2"
"2 = 3"
"3 = 4"
"4 = 5"

您可以使用each_cons

a.each_cons(2) do |first,last|
  if last == name
    puts 'got you!'
  else
    name = first
  end
end

您可以使用Enumerable#each_cons:

a = [ 'str1' , 'str2', 'str3' , ..... ]
name = ''
a.each_cons(2) do |x, y|
  if y == name
     puts 'got you! '
  else 
     name = x
  end
end

由于您可能希望对重复项进行puts以外的处理,因此我宁愿将重复项保留在一个结构中:

 ### question's example:
 a = [ 'str1' , 'str2', 'str2', 'str3'  ]
 #  => ["str1", "str2", "str2", "str3"] 
 a.each_cons(2).select{|a, b| a == b }.map{|m| m.first}
 #  => ["str2"] 
 ### a more complex example:
 d = [1, 2, 3, 3, 4, 5, 4, 6, 6]
 # => [1, 2, 3, 3, 4, 5, 4, 6, 6] 
 d.each_cons(2).select{|a, b| a == b }.map{|m| m.first}
 #  => [3, 6] 

更多信息,请访问:https://www.ruby-forum.com/topic/192355(David A.Black的冷静回答)

最新更新