Ruby Can';当表达式中存在错误时,无法使用'eval'获取stacktrace
我正在使用Ruby Can';当表达式中存在错误时,无法使用'eval'获取stacktrace,ruby,eval,Ruby,Eval,我正在使用eval对具有此救援块的大型代码块执行以下操作: rescue => e logger.error e.message e.backtrace.each { |line| logger.error line } end rescue block未提供例外情况的行号等。我只知道: undefined method `+' for nil:NilClass 当有很多代码需要评估时,这不是很有帮助。我尝试了各种方法,例如: eval(exp, binding, __FILE
eval
对具有此救援块的大型代码块执行以下操作:
rescue => e
logger.error e.message
e.backtrace.each { |line| logger.error line }
end
rescue block未提供例外情况的行号等。我只知道:
undefined method `+' for nil:NilClass
当有很多代码需要评估时,这不是很有帮助。我尝试了各种方法,例如:
eval(exp, binding, __FILE__, __LINE__)
但是他们没有提供任何关于错误所在行号的附加信息。这可能会有所帮助。这个答案在某种程度上是基于来自中国的细节。假设要在
exp
变量中计算表达式,我将作为第三个参数传递的行号值调整为String\module\u eval
begin
exp = <<EOL
a = nil
a + 10
EOL
String.module_eval(exp,__FILE__, __LINE__ - 1 - exp.split("\n").size )
rescue Exception => e
puts e
puts e.backtrace
end
开始
exp=这可能有帮助。这个答案在某种程度上是基于来自中国的细节。假设要在exp
变量中计算表达式,我将作为第三个参数传递的行号值调整为String\module\u eval
begin
exp = <<EOL
a = nil
a + 10
EOL
String.module_eval(exp,__FILE__, __LINE__ - 1 - exp.split("\n").size )
rescue Exception => e
puts e
puts e.backtrace
end
开始
exp=你的问题是什么?你的问题是什么?这没有告诉我变量exp中错误的详细位置。这是我的exp变量ar=[{“source”=>“manual”,“averageweight”=>“74.3”,“maxweight”=>“86.2”,“averageweightlastweek”=>“73.5”,“weightdiff”=>“-0.8”,“totalcount”=>“8});最大权重=[{“源”=>“手动”、“时间戳”=>“2015-11-12 04:39:00”、“最大权重”=>“86.2”}];min_weights=[{“source”=>“manual”,“timestamp”=>“2015-10-23 21:52:00”,“minweight”=>“65.8”},{“source”=>“manual”,“timestamp”=>“2015-11-06 18:37:00”,“minweight”=>“65.8”};时区='太平洋时间(美国和加拿大)'.strip;这并没有告诉我变量exp中错误的详细位置。这里是exp变量ar=[{“source”=>“manual”,“averageweight”=>“74.3”,“maxweight”=>“86.2”,“averageweightlastweek”=>“73.5”,“weightdiff”=>“-0.8”,“totalcount”=>“8}];最大权重=[{“源”=>“手动”、“时间戳”=>“2015-11-12 04:39:00”、“最大权重”=>“86.2”}];min_weights=[{“source”=>“manual”,“timestamp”=>“2015-10-23 21:52:00”,“minweight”=>“65.8”},{“source”=>“manual”,“timestamp”=>“2015-11-06 18:37:00”,“minweight”=>“65.8”};时区='太平洋时间(美国和加拿大)'.strip;