插件停止执行代码而不会引发错误

时间:2016-07-25 23:54:19

标签: ruby-on-rails ruby ruby-on-rails-4

我最近开始为我的一个项目制作一个gem,但是当它在运行时到达某个部分时它将停止执行并直接到达结尾并返回一个空数组。问题出在parse_results函数

require(boot)
require(robust)
data(stackloss)

stack.rob <- lmRob(stack.loss ~ ., data = stackloss)

lmRob.coef <- function(data, y, pred) {
    mod <- lmRob(formula = as.formula(eval(paste(y,"~", paste(pred,collapse="+")))) , data = data,  control = lmRob.control(mxr = 1000, mxf = 1000, mxs = 1000))
    coef(mod)
}

lmRob.results <- function(data, y, pred) {
    mod <- lmRob(formula = as.formula(eval(paste(y,"~", paste(pred,collapse="+")))) , data = data)
    data.frame(fitted = fitted(mod), residuals = resid(mod))
}

fit.dat <- lmRob.results(data = stackloss, y = "stack.loss", pred = c("Air.Flow", "Water.Temp", "Acid.Conc."))

model.fun <- function(data, i, y, pred, fitted.results) {
    dat <- cbind(data, fitted.results)
    dat[, y] <- dat$fitted + dat$residuals[i]
    lmRob.coef(data = dat, y = y, pred = pred)
}

lmRob.boot <- boot(data = stackloss, statistic = model.fun, R = 999, y = "stack.loss", pred = c("Air.Flow", "Water.Temp", "Acid.Conc."), fitted.results = fit.dat)

boot.ci(boot.out=lmRob.boot, type="bca", index=4)

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates

CALL : 
boot.ci(boot.out = lmRob.boot, type = "bca", index = 4)

Intervals : 
Level       BCa          
95%   (-0.2644,  0.2963 )  
Calculations and Intervals on Original Scale

在此之前,即使阵列设置正常,它也会在尝试执行def parse_results(result) puts result result_hash = Hash.new result_hash["url"] = result.id result_hash["score"] = result.rule_groups["SPEED"].score result_hash["rule_results"] = Hash.new # it cuts out right as it tries this puts command below, nothing is output puts result_hash["rule_results"]["AvoidLandingPageRedirects"].localized_rule_name rule_result_names = ["AvoidLandingPageRedirects", "EnableGzipCompression", "LeverageBrowserCaching", "MainResourceServerResponseTime", "MinifyCss", "MinifyHTML", "MinifyJavaScript", "MinimizeRenderBlockingResources", "OptimizeImages", "PrioritizeVisibleContent"] rule_result_names.each do |rule| puts rule result_hash["rule_results"][rule] = Hash.new result_hash["rule_results"][rule]["name"] = result.formatted_results.rule_results[rule].localized_rule_name result_hash["rule_results"][rule]["impact"] = result.formatted_results.rule_results[rule].rule_impact result_hash["rule_results"][rule]["advice"] = result.formatted_results.rule_results[rule].format end puts result_hash return result_hash end 部分时切断。有关为什么会发生这种情况的任何线索?网址和得分都很好。它从Google传递了一个Json / hash对象,这是有效的。这也是不寻常的,因为当我使用pry进行调试时,这些命令工作正常,至少是现在代码切断的puts命令。如果还有其他需要,请告诉我。提前谢谢!

2 个答案:

答案 0 :(得分:0)

没有输出任何内容,因为result_hash["rule_results"]是一个空哈希。

实际上,这就是发生的事情:

2.2.2 :001 > some = Hash.new
 => {} 
2.2.2 :002 > puts some['foo'].bar
NoMethodError: undefined method `bar' for nil:NilClass

您可能没有看到异常,因为您的某些代码或宝石正在吞噬异常并返回一个空数组。

答案 1 :(得分:0)

问题最终成为调用此问题的函数。给定的功能依赖于批量调用google的apis。它会尝试更新我给它的块中的结果,并导致此parse_results函数中的错误