Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Tempfile vs File

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?

like image 505
HXH Avatar asked May 14 '26 23:05

HXH


1 Answers

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.

like image 50
Martin Avatar answered May 18 '26 15:05

Martin