在Ruby中以块的forms读取文件

我需要读取MB块中的文件,有没有更简洁的方法在Ruby中执行此操作:

FILENAME="d:\\tmp\\file.bin" MEGABYTE = 1024*1024 size = File.size(FILENAME) open(FILENAME, "rb") do |io| read = 0 while read < size left = (size - read) cur = left < MEGABYTE ? left : MEGABYTE data = io.read(cur) read += data.size puts "READ #{cur} bytes" #yield data end end 

改编自Ruby Cookbook第204页:

 FILENAME = "d:\\tmp\\file.bin" MEGABYTE = 1024 * 1024 class File def each_chunk(chunk_size = MEGABYTE) yield read(chunk_size) until eof? end end open(FILENAME, "rb") do |f| f.each_chunk { |chunk| puts chunk } end 

免责声明:我是一个ruby新手,并没有测试过这个。

或者,如果您不想monkeypatch File

 until my_file.eof? do_something_with( my_file.read( bytes ) ) end 

例如,将上传的临时文件流式传输到新文件中:

 # tempfile is a File instance File.open( new_file, 'wb' ) do |f| # Read in small 65k chunks to limit memory usage f.write(tempfile.read(2**16)) until tempfile.eof? end 

您可以使用IO#each(sep, limit) ,并将sep设置为nil或空字符串,例如:

 chunk_size = 1024 File.open('/path/to/file.txt').each(nil, chunk_size) do |chunk| puts chunk end 

如果您查看ruby文档: http : //ruby-doc.org/core-2.2.2/IO.html,有一行如下:

 IO.foreach("testfile") {|x| print "GOT ", x } 

唯一需要注意的是。 由于此进程可以比生成的流IMO更快地读取临时文件,因此应该引入延迟。

 IO.foreach("/tmp/streamfile") {|line| ParseLine.parse(line) sleep 0.3 #pause as this process will discontine if it doesn't allow some buffering } 
 FILENAME="d:/tmp/file.bin" class File MEGABYTE = 1024*1024 def each_chunk(chunk_size=MEGABYTE) yield self.read(chunk_size) until self.eof? end end open(FILENAME, "rb") do |f| f.each_chunk {|chunk| puts chunk } end 

它有效, mbarkhau 。 我只是将常量定义移动到File类,并为了清晰起见添加了一些“self”。