From: William James Date: 2007-01-04T04:35:11+09:00 Subject: Re: Delete line from file dblack@wobblini.net wrote: > Hi -- > > On Wed, 3 Jan 2007, William James wrote: > > > > > Jules wrote: > >> Hi, > >> > >> Given a line number, what is the best way to delete this line from a > >> file? > >> > >> And given an array of line numbers, what is the best way to delete > >> these lines from a file? > >> > >> Thanks, > >> > >> Jules > > > > lines = IO.readlines('junk1') > > [1,3,5].each{|n| lines.slice!(n) } > > That's going to have a side-effect problem: > > irb(main):004:0> a = %w{ a b c d e f } > => ["a", "b", "c", "d", "e", "f"] > irb(main):005:0> [1,3,5].each {|e| a.slice!(e) } > => [1, 3, 5] > irb(main):006:0> a > => ["a", "c", "d", "f"] True. a = %w(zero one two three four five) [1,3,5].each{|i| a[i]=nil} a.compact!