ruby SourceGraph |用Ruby编写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby SourceGraph |用Ruby编写相关的知识,希望对你有一定的参考价值。

#!/usr/bin/ruby
# based on code published by Satish Talim in his booklet titled 'How do I use Sourcegraph with Ruby?'
# under a Creative Commons Attribution 3.0 Unported License
# made available at http://leanpub.com/howdoiusesourcegraphwithruby
# a couple of lines of code have been added by nick3499 to include source's title and permalink
# ----------
# may need to run script multiple times
# expect to see error messages like this:
# sourcegraph.rb:12:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

require 'json'
require 'net/http'

# removed conditional statement

url = "http://www.reddit.com/r/ruby.json"
uri = URI.parse(url)

res = Net::HTTP.get_response(uri)
result = JSON.parse(res.body)

for x in 0..(result['data']['children'].length-1)
  puts "Author: "      + result['data']['children'][x]["data"]["author"]
  # following line added by nick3499
  puts "Title: "       + result['data']['children'][x]["data"]["title"]
  puts "Article URL: " + result['data']['children'][x]["data"]["url"]
  # following line added by nick3499
  puts "PermaLink: "   + "https://www.reddit.com" + result['data']['children'][x]["data"]["permalink"]
  puts " --------------------- "
end

以上是关于ruby SourceGraph |用Ruby编写的主要内容,如果未能解决你的问题,请参考以下文章

Ruby学习-1.学习对象

在编写rspec测试用例时如何覆盖ruby方法的局部变量?

用纯 Ruby 编写 Rails 模板

Ruby 等价于 python nonlocal

如何在Ruby中编写微服务?

如何在Ruby中编写微服务?