Open an IO stream from a local file or url

前端 未结 1 1666
深忆病人
深忆病人 2020-11-28 21:02

I know there are libs in other languages that can take a string that contains either a path to a local file or a url and open it as a readable IO stream.

Is there a

1条回答
  •  时光取名叫无心
    2020-11-28 21:09

    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 }
    

    0 讨论(0)
提交回复
热议问题