open-uri is part of the standard Ruby library, and it will redefine the behavior of open
so that you can open a url, as well as a local file. It returns a File
object, so you should be able to call methods like read
and readlines
.
require 'open-uri'
file_contents = open('local-file.txt') { |f| f.read }
web_contents = open('http://www.stackoverflow.com') {|f| f.read }
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…