简体   繁体   中英

Read a file from github

I want to read a file from github repository in my ruby script. Say I want to read Gemfile from my repo on github, URL for which would be like: " http://www.github.com/myrepo/blob/master/Gemfile ".

I tried using File.readLink("http://www.github.com/myrepo/blob/master/Gemfile") but this gives me error saying "'readlink': No such file or directory @ rb_readlink" .

How do I read a file using the github URL?

You should try to fetch raw content from github files like:

require 'net/http'

uri = "https://raw.githubusercontent.com/username/myrepo/master/Gemfile"
uri = URI(uri)
file = Net::HTTP.get(uri)

With the below code, I was able to read the content of the file.

require 'open-uri'
raw_url = "https://raw.githubusercontent.com/username/myrepo/master/Gemfile"
open(raw_url) {|f|
  f.each_line {|line| p line}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM