是否可以将字符串转换为文件而不写入磁盘?Ruby将字符串转换为文件
我想用一个任意文件的字符串无所不在工作:
input = "123"
if (ARGV.length == 1)
input = File.open(ARGV[0])
#do stuff with input
end
我可以创建一个字符串的文件(不写入磁盘)?否则,当它是一个字符串时,我将无法执行input.readline()
。
是否可以将字符串转换为文件而不写入磁盘?Ruby将字符串转换为文件
我想用一个任意文件的字符串无所不在工作:
input = "123"
if (ARGV.length == 1)
input = File.open(ARGV[0])
#do stuff with input
end
我可以创建一个字符串的文件(不写入磁盘)?否则,当它是一个字符串时,我将无法执行input.readline()
。
StringIO
可用于给字符串提供类似文件的界面。
的StringIO的是不错,你也可以做到这一点使用块:
StringIO.open(string) do |file|
# do stuff here
end
我喜欢这个ALT超过file.close
请在您的示例结尾添加结束关键字;) – Alexey 2010-05-05 21:15:04