另一个似乎很愚蠢的问题,但还没有找到一个明确的例子。如何从文件创建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