Skip to content

Instantly share code, notes, and snippets.

@Peranikov
Created November 22, 2017 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peranikov/3680bff5573832f436423ce74f5c24ae to your computer and use it in GitHub Desktop.
Save Peranikov/3680bff5573832f436423ce74f5c24ae to your computer and use it in GitHub Desktop.
5–7-2 Fileオブジェクトの操作
# ファイルのロック
File.open 'counter', File::RDWR | File::CREAT do |f|
f.flock File::LOCK_EX
count = f.read.to_i
f.rewind
f.write count.succ
end
# ファイルに関する情報の取得
File.open 'counter' do |f|
# 最後にアクセスした日時
p f.atime
# 最後に状態を変更した日時
p f.ctime
# 最後に更新した日時
p f.mtime
# ファイルサイズ
p f.size
end
# ファイルの情報
stat = File.open('counter', &:stat)
# ファイルタイプ
stat.ftype
stat.file?
stat.directory?
stat.symlink?
stat.pipe?
stat.socket?
# ファイルの状態
stat.writable?
stat.readable?
stat.executable?
# 自身がオーナーか
stat.owned?
# ファイルオーナーの情報
stat.gid
stat.uid
# inode番号
stat.ino
# デバイス番号
stat.dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment