Christopher Swasey
3/1/2008 1:26:00 AM
On 2/29/08, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Trans wrote:
> > How to remove the first occurrence of something?
> >
> > a=[1,2,3,4]
> > a.find!{ |e| a==2 } #=> 2
> >
> > But there is no #find!, how to define it? My solutions seem unduly
> > complex.
> >
> > Thanks,
> > T.
>
>
> More complex than the following?
>
> arr = [1, 2, 3, 2, 2, 2]
>
> arr.each_with_index do |elmt, i|
> if elmt == 2
> arr.delete_at(i)
> break
> end
> end
Why not just...
arr = [1, 2, 2, 2, 3]
arr.delete_at(arr.index(2))
arr => [1, 2, 2, 3]
Christopher