From: Robert Klemme Date: 2008-01-02T20:19:58+09:00 Subject: Re: using reg expr with array.index On 02.01.2008 05:40, Esmail wrote: > MonkeeSage wrote: >> >> Ps. ruby will normally close open file handles on garbage collection >> or in finalization, but just in case of some catastrophic failure >> (what, I'm not sure), it's usually considered good practice to close >> file handles manually: >> >> ... >> file=File.open(ARGV[0]) >> data=file.readlines >> file.close > > Ooops, absolutely, that sort of houskeeping needs to be done, I'm good > doing with C and Java .. not sure why I didn't think about this with Ruby. > > By the way, I know if I do something like > > File.open("bla").each { |line| > stuff > } > > the file gets automagically closed at the end of the block. I'm sorry, but this is wrong. You either need to do File.open("bla") do |io| io.each do |line| # stuff end end or File.foreach "bla" do |line| # stuff end > What about the example you gave, data=IO.read("bla") ... I assume the > file gets opened, read and closed in one fell swoop, correct? This is correct. Kind regards robert