I want to know the difference between Tempfile and File.
I found that :
require 'open-uri'
open('c:/boot.ini'){|file|
puts file.class #File
}
open('http://coderlee.cnblogs.com'){|file|
puts file.class #Tempfile
}
and when I save the stream to a remote storage server,the Tempfile will cause an error,It seems that the reason is the encoding is not ASCII-8BIT why?
In the first case, you are loading a file from your file system. This create a File object, using the file name (it has one).
In the second case, you are opening a stream toward a remote file. There is no associated file on your file system, yet you need one if you want to make any operation on it. Thus, Ruby creates a Tempfile for you with a unique filename that you don't even need to know (as the resource does not have a name itself). It then behave exactly like a File object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With