LoginSignup
4
4

More than 5 years have passed since last update.

Fileの読み書きをブロック処理にすると

Posted at

Fileの扱いについてメモ。

例えばテキストファイルオープンして処理する場合。

普通に書いた場合

file = File.open("sample.txt", "r:utf-8")
 処理
file.close

ブロック処理で書いた場合

File.open("sample.txt", "r:utf-8") do |file|
 処理
end

ブロック処理で書くと
closeメソッドを使わなくても自動でファイルを閉じてくれる。
覚えておこう。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4