可以使用File:
重新打开STDINSTDIN.reopen(File.open('filename'))
但是它返回StringIO的错误:
STDIN.reopen(StringIO.new('text'))
TypeError:
no implicit conversion of StringIO into String
我应该总是使用文件重新打开,还是可以用某种方式用字符串完成?
答案 0 :(得分:0)
为什么不重新分配句柄,例如:
$stdin = StringIO.new('text')
然后你可以像常规的$stdin
句柄那样使用它:
gets
# => "text"
documentation on reopen
implies you can do this确实如此,但无论出于何种原因,StringIO都不是从IO派生的,所以它不算作文件句柄而且不能被它使用。&lt; < / p>