另一個似乎很愚蠢的問題,但還沒有找到一個明確的例子。如何從文件創建IO對象?
我需要一個IO對象作爲參數的函數,actually its the new function in this class:
我用這樣的方式,但似乎有些問題,使用文件描述符當作爲IO寫的東西后:
irb(main):001:0> f= File.open("result.txt","w")
=> #<File:result.txt>
irb(main):002:0> i=IO.new(f.to_i,"w")
=> #<IO:0x3b5cb90>
irb(main):003:0> i.write "hello the world"
=> 15
irb(main):004:0> i.close
=> nil
irb(main):005:0> f.close
Errno::EBADF: Bad file descriptor - result.txt
from (irb):5:in `close'
from (irb):5
from :0
所以我只需要關閉我或f一次?或者有一個標準的方法來做到這一點?
謝謝,我很愚蠢......爲什麼在看到它後忘了這個例子......謝謝! – 2012-03-04 03:00:50