gpt4 book ai didi

Ruby NameError 未定义的局部变量或方法`e

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:57 25 4
gpt4 key购买 nike

class TwitterProfile < ActiveRecord::Base

def send_status_update(status_update)
if publish?
client = Twitter::Client.new( :oauth_token => authentication.token,
:oauth_token_secret => authentication.secret)
client.update(status_update.to_twitter_string)
end
rescue Exception => e
logger.info "Error publishing to twitter: #{e.to_s}"
end
end

after_create 中有一个 StatusUpdate 模型和一个将它们重新发布到 Twitter 的观察者。我有时遇到以下异常:

NameError (undefined local variable or method `e' for #<TwitterProfile:0x00000004e44ab8>):
app/models/twitter_profile.rb:23:in `rescue in send_status_update'
app/models/twitter_profile.rb:18:in `send_status_update'
app/models/status_update_observer.rb:6:in `block in after_create'
app/models/status_update_observer.rb:4:in `after_create'
app/models/workout_observer.rb:5:in `after_update'
app/controllers/frames_controller.rb:76:in `update'
app/controllers/application_controller.rb:24:in `call'
app/controllers/application_controller.rb:24:in `block (2 levels) in <class:ApplicationController>'
app/controllers/application_controller.rb:10:in `block in <class:ApplicationController>'

我在这里错过了什么?

最佳答案

我知道一件事,还有一件事只是胡乱猜测。

我知道的是,您不需要在整个 #{} 表达式上调用 to_s;那会自动发生。但它没有坏处。

我的大胆猜测是您的测试用例并没有真正运行您发布的代码。如果将 e 更改为 f 会发生什么情况?

我应该指出,拯救 Exception 本身通常不是一个好主意。您应该在最高级别拯救 RuntimeError 或 StandardError,最好是更具体的东西。在拯救 Exception 时,您可能会遇到相当奇怪的错误,因为您会干扰线程和解释器级别的事件。

关于Ruby NameError 未定义的局部变量或方法`e,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7223036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com