From: usa@... Date: 2016-07-05T14:03:50+00:00 Subject: [ruby-dev:49717] [Ruby trunk Bug#12554] yamlに渡すファイルポインタを自分でcloseしないと動作が変 Issue #12554 has been updated by Usaku NAKAMURA. 西行寺 うゆ wrote: > rubyのファイルポインタは、スコープを抜けたら処理系側でcloseされているものと思ったんですが それは誤解です。 参照されなくなったFileオブジェクトは、GCされる時にcloseされる可能性はありますが、 それはいつになるのか普通は予測困難ですので、用が済んだらちゃんと閉じられるように コードを書いてください。 ---------------------------------------- Bug #12554: yamlに渡すファイルポインタを自分でcloseしないと動作が変 https://bugs.ruby-lang.org/issues/12554#change-59511 * Author: 西行寺 うゆ * Status: Open * Priority: Normal * Assignee: * ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [i386-mswin32_100] * Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- rubyのファイルポインタは、スコープを抜けたら処理系側でcloseされているものと思ったんですが yamlで読み書きを行う場合に動作がおかしいような気がします このスクリプトを実行すると、それなりの確率で以下のエラーが出て、 test.yamlのデータが変です ~~~ require"yaml" def func file = "test.yaml" data = YAML.load_file( file ) r = rand(99999) data ||={} data[r] ||=[] data[r] << rand(99999) # 問題個所 YAML.dump( data, open( file , 'w') ) # 自分でcloseすると平気 # # f = open( file , 'w') # YAML.dump( data , f ) ; # f.close end 1000.times do |n| print n , " " func end ~~~ ~~~ C:/Ruby/lib/ruby/2.3.0/psych.rb:377:in `parse': (test.yaml): did not find expected key while pa rsing a block mapping at line 2 column 1 (Psych::SyntaxError) from C:/Ruby/lib/ruby/2.3.0/psych.rb:377:in `parse_stream' from C:/Ruby/lib/ruby/2.3.0/psych.rb:325:in `parse' from C:/Ruby/lib/ruby/2.3.0/psych.rb:252:in `load' from C:/Ruby/lib/ruby/2.3.0/psych.rb:471:in `block in load_file' from C:/Ruby/lib/ruby/2.3.0/psych.rb:471:in `open' from C:/Ruby/lib/ruby/2.3.0/psych.rb:471:in `load_file' from -:4:in `func' from -:11:in `block in
' from -:10:in `times' from -:10:in `
' ~~~ ---Files-------------------------------- test.yaml (270 Bytes) -- https://bugs.ruby-lang.org/