From: Matt Brown Date: 2006-06-07T10:44:37+09:00 Subject: Re: What's going on here? (Newbie question) ------=_Part_52089_31608476.1149644674608 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Exactly what I was looking for, thank you. On 6/6/06, Mike Stok wrote: > > > On 6-Jun-06, at 9:36 PM, MB wrote: > > > In irb: > > > > f = File.new('numbers.txt') > > => # > > > > f.each {|line| puts line} > > 123 > > 456 > > 789 > > => # > > > > f.each {|line| puts line} > > => # > > > > Why doesn't the second f.each work? > > because as each iterates through the file it "consumes" the file > contents (well, it remembers how far it has got.) You can usually > rewind a file to the beginning, so: > > irb(main):001:0> f = File.new('numbers.txt') > => # > irb(main):002:0> f.each {|line| puts line} > 123 > 456 > 789 > => # > irb(main):003:0> f.pos > => 12 > irb(main):004:0> f.each {|line| puts line} > => # > irb(main):005:0> f.pos > => 12 > irb(main):006:0> f.rewind > => 0 > irb(main):007:0> f.each {|line| puts line} > 123 > 456 > 789 > => # > > Note that some types of file can't be rewound. > > Take a look at what the pos method does, and see if this makes sense. > > Hope this helps, > > Mike > > -- > > Mike Stok > http://www.stok.ca/~mike/ > > The "`Stok' disclaimers" apply. > > > > > > ------=_Part_52089_31608476.1149644674608--