Tbpgr Blog

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

Ruby | File | split

概要

File.split

詳細

引数にファイルパスを指定することで、ディレクトリ部とファイル部をリストで返却する。

サンプル

コード
# encoding: utf-8

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

create_file 'hoge.txt', "test"
Dir.mkdir 'tmp' unless Dir.exists? 'tmp'
Dir.chdir 'tmp'
p File.split('../hoge.txt')
出力
["..", "hoge.txt"]