2016-10-07 8 views
0

私は、次のラムダを使用していますが、Resque::MailerREADMEhttps://github.com/zapnap/resque_mailer)から収集:Use a guard clause instead of wrapping the code inside a conditional expressionこのラムダをガード句でどのように書き直すことができますか?

残念ながら
 
    Resque::Mailer.error_handler = lambda { |mailer, _message, error, action, args| 
    # Necessary to re-enqueue jobs that receive the SIGTERM signal 
    if error.is_a?(Resque::TermException) 
     Resque.enqueue(mailer, action, *args) 
    else 
     raise error 
    end 
    } 

を、コードベースで有効になってrubocopは、エラーで、この方法のif..else性質について不平を言います。

ここでunlessを設定しようとしましたが、これまで設定した順序に関係なくraise errorが実行されました。ここでガード句を使用する正しい方法は何ですか?

私がこれを尋ねる理由は、次の例は、私がunlessで明白なことを行うと、エラーが常に発生することを示しているようです。私はこれを間違って理解していますかunlessに状態を変換

 
Development [10] (main)> class Foo 
Development [10] (main)* def bar 
Development [10] (main)*  puts "Outside unless getting exec" unless 1 == 2 
Development [10] (main)*  puts "After unless getting exec" 
Development [10] (main)* end 
Development [10] (main)* end 
:bar 
Development [11] (main)> Foo.new.bar 
Outside unless getting exec 
After unless getting exec 
nil 

答えて

1

は動作、およびrubocopを満たすべきである:

Resque::Mailer.error_handler = lambda { |mailer, _message, error, action, args| 
    # Necessary to re-enqueue jobs that receive the SIGTERM signal 
    raise error unless error.is_a?(Resque::TermException) 
    Resque.enqueue(mailer, action, *args) 
    } 
+0

Iは実際に見えたその一部[例えば、コード(http://pastebin.com/aG7Hxd8f)を実行しようとしましたこれを行うと、エラーが常に発生することを示します。明確にできますか? –

+0

これは完全に正しいわけではありません。エラーは 'Resque :: TermException'の場合にのみ発生します。 – eugen

+0

さて、私の質問も更新されました。なぜ 'puts'が違う振る舞いをするのか説明できますか? –

関連する問題