Created
March 5, 2014 02:03
-
-
Save YuheiNakasaka/9359856 to your computer and use it in GitHub Desktop.
Rails4.0.1,Ruby1.9.3のとあるメソッド内にて、Tempfileでcloseしなくてもあとでまとめてdeleteすればいいかと思ってこんなコードを書いたらtempfileに何も書き込まれてなくてはまった...
This file contains hidden or 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
require 'tempfile' | |
chars = (('a'..'z').to_a + ("A".."Z").to_a + (0..9).to_a ) * 1000 | |
words = [chars.join()] * 11 | |
files = [] | |
paths = [] | |
words.each do |word| | |
file = Tempfile.new(['hoge','jpg'], :encoding => 'ascii-8bit') | |
file.write(word) | |
files << file | |
paths << file.path | |
end | |
files.each do |file| | |
puts file.size | |
end | |
# railsではこのへんでraiseさせてた。作成されたtempfileは削除されておらず、 | |
# そのファイルサイズを確認すると0バイトになってた。 | |
paths.each do |path| | |
File.delete(path) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment