Tbpgr Blog

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

Ruby | File | zero?

概要

File.zero?

詳細

ファイルサイズがzeroかどうか判定します。

サンプル

コード
# encoding: utf-8
require 'tbpgr_utils'

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

create_file 'hoge.txt', "test"
create_file 'hige.txt', ""

bulk_puts_eval binding, <<-EOS
File.zero?('hoge.txt')
File.zero?('hige.txt')
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
File.zero?('hoge.txt') # => false
File.zero?('hige.txt') # => true