Tbpgr Blog

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

Ruby | File | extname

概要

File.extname

詳細

拡張子を取得します

サンプル

コード
# encoding: utf-8

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

filenames = ['sample1.txt', 'sample2.rb', 'no_extension']
filenames.each do |f|
  create_file f, "test"
  p File.extname(f)
end
出力
".txt"
".rb"
""