Dear Arup,

Your code:

def del_first_three(a)
  num_to_del = a.find { |e| a.count(e) >= 3 }
  return a if num_to_del.nil?
  3.times do
    ind = a.index { |e| e == num_to_del }
    a.delete_at(ind)
  end
  a
end

First step:

def del_first_three(a)
  num_to_del = a.find { |e| a.count(e) >= 3 }
  return a if num_to_del.nil?

  a.delete_if { |e| e == num_to_del }
 end

# All the part from 3.times do block on got substituted.
# Now is easy to figure out that the first return is also redundant

def del_first_three(a)
  num_to_del = a.find { |e| a.count(e) >= 3 }
  a.delete_if { |e| e == num_to_del }
end

I hope it helps.

Best regards,
Abinoam Jr.


On Sun, Feb 9, 2014 at 7:13 AM, Arup Rakshit <lists / ruby-forum.com> wrote:
> I want to delete the first 3 duplicate entries -
>
> I wrote a code :
>
> def del_first_three(a)
>   num_to_del = a.find { |e| a.count(e) >= 3 }
>   return a if num_to_del.nil?
>   3.times do
>     ind = a.index { |e| e == num_to_del }
>     a.delete_at(ind)
>   end
>   a
> end
>
> del_first_three([3,4,5,3,3])
>
> But at the method end I put `a`, to return the resultant array, which I
> don't like. So I took the help of `#tap` as below :
>
> def del_first_three(a)
>   num_to_del = a.find { |e| a.count(e) >= 3 }
>   return a if num_to_del.nil?
>   3.times do
>     ind = a.index { |e| e == num_to_del }
>     a.tap { |ob| ob.delete_at(ind) }
>   end
> end
>
> del_first_three([3,4,5,3,3]) # => 3
>
> But it is also helpless, as `Integer#times` returns `self`. Is there any
> method to meet my need. I am actually looking for a method, which work
> as `File:open` with block.
>
> --
> Posted via http://www.ruby-forum.com/.