views:

190

answers:

2

Is it possible to convert a string to a file without writing it to disk?

I would like to work ubiquitously with either a string of a file:

input = "123"
if (ARGV.length == 1)
   input = File.open(ARGV[0])

#do stuff with input

Can I create a file from a string (without writing to disk)? Otherwise, I would not be able to do input.readline() when it's a string.

+3  A: 

StringIO can be used to give a file-like interface to strings.

Brian Young
+9  A: 

You can use StringIO to create an IO object (that is, an object that acts like a file) out of a string:

file = StringIO.new("123")
line = file.readline
file.close
Brian Campbell