Created
January 24, 2018 11:01
-
-
Save Peranikov/59699431b7207a645d34dfc414393b7a to your computer and use it in GitHub Desktop.
5-7-3 ファイルの操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 属性や状態の取得 | |
fname = '/etc/hosts' # Windowsでは 'C:¥Windows¥System32¥drivers¥etc¥hosts' | |
File.atime(fname) # 最終アクセス日時 => 2018-01-24 19:28:45 +0900 | |
irb(main):004:0> File.ctime(fname) # 最後に状態を変更した日時 => 2017-12-09 12:23:36 +0900 | |
irb(main):005:0> File.mtime(fname) # 最終更新日時 => 2017-12-04 19:03:18 +0900 | |
# ファイルの存在 | |
File.exists?(fname) # => true | |
# ファイルの状態 | |
File.owned?(fname) # => false | |
File.executable?(fname) # => false | |
File.readable?(fname) # => true | |
File.writable(fname) # => false | |
# ファイルのタイプ | |
File.ftype(fname) | |
File.file?(fname) | |
File.directory?(fname) | |
File.pipe?(fname) | |
File.socket?(fname) | |
File.symlink?(fname) | |
# ファイルサイズ | |
File.size(fname) | |
File.zero?(fname) | |
# より詳しい情報を得る | |
File.stat(fname) | |
# ファイルパスの操作 | |
fname = '/etc/resolv.conf' | |
# ファイルのあるディレクトリのパス | |
File.diraname(fname) # => "/etc" | |
# ファイル名 | |
File.basename(fname, '.*') # => "resolv.conf" | |
# 拡張子を除いたファイル名 | |
File.extname(fname) # => ".conf" | |
# ファイルパスを連結する | |
File.join('/usr/local', 'bin/ruby') # => "/usr/local/bin/ruby" | |
# なおWindowsでも '/' で連結されてしまう? | |
File.join("C:¥Windows", "notepad.exe") # => "C:¥Windows/notepad.exe" | |
File.join("C:", "Windows", "notepad.exe") # => "C:¥Windows/notepad.exe" | |
# dirnameとbasenameの配列 | |
File.split('/usr/local/bin/ruby') # => ["/usr/local/bin", "ruby"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment