Ruby-Nomethoderor,但为什么只是有时候?

Ruby-Nomethoderor,但为什么只是有时候?,ruby,Ruby,我正在编写一个脚本,为subreddit提取注释,分解出单个单词,计算它们的数量,并对它们进行排序。大约70%的时间我都会遇到这样的错误: in `<main>': undefined method `map' for nil:NilClass (NoMethodError) Did you mean? tap nil:NilClass(NoMethodError)的“`”:未定义的方法“map”是什么意思?水龙头 大约30%的时间,脚本按预期工作。为什么会这样?你会如何解决?

我正在编写一个脚本,为subreddit提取注释,分解出单个单词,计算它们的数量,并对它们进行排序。大约70%的时间我都会遇到这样的错误:

in `<main>': undefined method `map' for nil:NilClass (NoMethodError) Did you mean?  tap
nil:NilClass(NoMethodError)的“`”:未定义的方法“map”是什么意思?水龙头
大约30%的时间,脚本按预期工作。为什么会这样?你会如何解决?我是编程新手,所以如果问题是基本的,我不会感到惊讶。这是我的密码:

require 'net/http'
require 'rubygems'
require 'json'

# Pull json file, parse out comments
url = 'https://www.reddit.com/r/askreddit/comments.json?sort=top&t=all&limit=100'
uri = URI(url)
response = Net::HTTP.get(uri)
json = JSON.parse(response)

comments = json.dig("data", "children").map { |child| child.dig("data", "body") }

#Split words into array
words = comments.to_s.split(/[^'\w]+/)

words.delete_if { |a,_| a.length < 5}

#count and sort words
count = Hash.new(0)
words.each { |word| count.store(word, count[word]+1)}
count.delete_if { |_,b| b < 4}
sorted = count.sort_by { |word,count| count}.reverse
puts sorted
需要“net/http”
需要“rubygems”
需要“json”
#提取json文件,解析注释
url='1〕https://www.reddit.com/r/askreddit/comments.json?sort=top&t=all&limit=100'
uri=uri(url)
response=Net::HTTP.get(uri)
json=json.parse(响应)
comments=json.dig(“data”,“children”).map{child | child.dig(“data”,“body”)}
#将单词拆分为数组
words=comments.to_.s.split(/[^'\w]+/)
words.delete_如果{a,{a.length<5}
#对单词进行计数和排序
计数=散列。新建(0)
words.each{word | count.store(word,count[word]+1)}
count.delete_如果{| |,b | b<4}
sorted=count.sort_by{| word,count | count}.reverse
排序

看起来您的
json.dig(“数据”、“子项”)
偶尔会返回
nil
。优雅处理此问题的一种方法是使用:


Reddit方面基本上存在一个错误。我已经写了一份工作,但有时会有一点延迟;它反复尝试,直到成功编辑以(大部分)匹配原始代码。

require 'net/http'
require 'rubygems'
require 'json'

# Pull json file, parse out comments
url = 'https://www.reddit.com/r/askreddit/comments.json?sort=top&t=all&limit=100'
uri = URI(url)
error = true
while error
   response = Net::HTTP.get(uri)
   json = JSON.parse(response)
   error = json["error"]
end

comments = json.dig("data", "children").map { |child| child.dig("data", "body") }

#Split words into array
words = comments.to_s.split(/[^'\w]+/)

words.delete_if { |a,_| a.length < 5}

#count and sort words
count = Hash.new(0)
words.each { |word| count.store(word, count[word]+1)}
count.delete_if { |_,b| b < 4}
sorted = count.sort_by { |word,count| count}.reverse
puts sorted
需要“net/http”
需要“rubygems”
需要“json”
#提取json文件,解析注释
url='1〕https://www.reddit.com/r/askreddit/comments.json?sort=top&t=all&limit=100'
uri=uri(url)
错误=真
当错误
response=Net::HTTP.get(uri)
json=json.parse(响应)
error=json[“error”]
结束
comments=json.dig(“data”,“children”).map{child | child.dig(“data”,“body”)}
#将单词拆分为数组
words=comments.to_.s.split(/[^'\w]+/)
words.delete_如果{a,{a.length<5}
#对单词进行计数和排序
计数=散列。新建(0)
words.each{word | count.store(word,count[word]+1)}
count.delete_如果{| |,b | b<4}
sorted=count.sort_by{| word,count | count}.reverse
排序

这意味着并不总是有任何数据被
挖掘
。有时,
json.dig(“数据”,“子项”)==nil
。为什么呢?我不知道。调查看。也许,例如(这只是一个猜测!!)reddit限制了你的速率,所以如果你太频繁地请求数据,你会得到一个空白响应。我不确定为什么它只是偶尔发生。每次查看响应时,我都会得到正确的结果或
{“message”:“请求太多”,“error”:429}
。我下面的解决方案会起作用,但只是因为它只是在Reddit合作之前重试。
require 'net/http'
require 'rubygems'
require 'json'

# Pull json file, parse out comments
url = 'https://www.reddit.com/r/askreddit/comments.json?sort=top&t=all&limit=100'
uri = URI(url)
error = true
while error
   response = Net::HTTP.get(uri)
   json = JSON.parse(response)
   error = json["error"]
end

comments = json.dig("data", "children").map { |child| child.dig("data", "body") }

#Split words into array
words = comments.to_s.split(/[^'\w]+/)

words.delete_if { |a,_| a.length < 5}

#count and sort words
count = Hash.new(0)
words.each { |word| count.store(word, count[word]+1)}
count.delete_if { |_,b| b < 4}
sorted = count.sort_by { |word,count| count}.reverse
puts sorted