Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | File | absolute_path

概要

File.absolute_path

詳細

第1引数に指定したパス名を絶対パス名に変換します。
第2引数を指定した場合は、指定したパスを基準とします。

サンプル

コード
# encoding: utf-8

def create_file(filename, contents)
  File.open(filename, 'w:utf-8') do |f|
    f.print contents
  end
end

filename = 'sample.txt'

create_file filename, <<EOS
内容1_1
内容1_2
内容1_3
EOS
puts File.absolute_path('sample.txt')
Dir.mkdir('test') unless File.exists? ('test') 
p Dir.pwd
Dir.chdir('test')
p Dir.pwd

create_file filename,<<EOS
内容2_1
内容2_2
内容2_3
EOS

puts File.absolute_path('sample.txt', '../')
出力
C:/sample.txt
"C:/"
"C:/test"
C:/sample.txt