我有以下代码可从github作业API编译作业。每次呼叫一个新城市时,如何将计数器重置为0?我试过将它放在几个不同的地方,没有运气。
def ft_count_and_percentage
@@url += @city
uri = URI(@@url)
response = Net::HTTP.get(uri)
result = JSON.parse(response)
result.each do |job|
if job["type"] == "Full Time"
@@fulltime_count += 1
end
end
puts "Total number of jobs in #{@city}: #{result.length}"
if @@fulltime_count > 0
puts ("full time percent ") + "#{(@@fulltime_count/result.length) * 100}"
else
puts "No FT Positions"
end
end
@@ fulltime_count在此方法之外定义为从0开始。目前,正如预期的那样,每次添加新城市时,计数器只会不断添加工作。
boston = Job.new("Boston")
boston.ft_count_and_percentage
sf = Job.new("San Francisco")
sf.ft_count_and_percentage
la = Job.new("Los Angeles")
la.ft_count_and_percentage
denver = Job.new("Denver")
denver.ft_count_and_percentage
boulder = Job.new("Boulder")
boulder.ft_count_and_percentage
chicago = Job.new("Chicago")
chicago.ft_count_and_percentage
ny = Job.new("New York City")
ny.ft_count_and_percentage
答案 0 :(得分:0)
您可能需要在Job init中将其重置
class Job
def initialize
@@count = 0
end
def ft_count_and_percentage
#the blah you already have
end
end