open-uri是标准Ruby库的一部分,它会重新定义open的行为,使您可以打开url和本地文件。 它返回一个File对象,因此您应该能够调用像read和readlines这样的方法。require 'open-uri' file_contents = open('local-file.txt') { |f| f.read } web_contents = open('http://www.stackoverflow.com') {|f| f.read }
content = open("http://example.com").read
。 - Automaticoopen-uri
无法 流式传输 文件,因此您无法读取其前4k内容。在打开文件时,open-uri
将整个文件读入内存中。 - zed_0xffKernal.open
,同时避开 Rubocop 的安全规则,可以使用URI.parse('http://www.stackoverflow.com').open { |f| f.read }
。 - User128848244