Вопрос

update: sorry, I fixed my program:

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

Is there another beautiful way in ruby language to do the same thing ?

btw, actually. a is a ActiveRecord::Relation in my real case.

Thanks.

Это было полезно?

Решение

The problem you might have with each_cons is that it iterates through n-1 pairs (if the length of the Enumerable is n). In some cases this means you have to separately handle edge cases for the first (or last) element.

In that case, it's really easy to implement a method similar to each_cons, but which would yield (nil, elem0) for the first element (as opposed to each_cons, which yields (elem0, elem1):

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

Другие советы

you can use 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"

You can use each_cons

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

You may use 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

As you probably want to do more than puts with the duplicates, I would rather keep the duplicates in a structure:

 ### 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] 

More on at: https://www.ruby-forum.com/topic/192355 (cool answer of David A. Black)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top