Prefer block forms to close opened files

This commit is contained in:
Nobuyoshi Nakada 2021-02-03 17:34:41 +09:00
parent d05a268adc
commit b4eba8dfee
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6

View File

@ -510,22 +510,24 @@ if defined? Zlib
gz = Zlib::GzipWriter.new(t) gz = Zlib::GzipWriter.new(t)
gz.print("foo") gz.print("foo")
gz.close gz.close
t = File.open(t.path, 'ab') File.open(t.path, 'ab') do |f|
gz = Zlib::GzipWriter.new(t) gz = Zlib::GzipWriter.new(f)
gz.print("bar") gz.print("bar")
gz.close gz.close
end
results = [] results = []
t = File.open(t.path, 'rb') File.open(t.path, 'rb') do |f|
Zlib::GzipReader.zcat(t) do |str| Zlib::GzipReader.zcat(f) do |str|
results << str results << str
end
end end
assert_equal(["foo", "bar"], results) assert_equal(["foo", "bar"], results)
t.close
t = File.open(t.path, 'rb') results = File.open(t.path, 'rb') do |f|
assert_equal("foobar", Zlib::GzipReader.zcat(t)) Zlib::GzipReader.zcat(f)
t.close end
assert_equal("foobar", results)
} }
end end