2017-03-22 2 views
0

私は現在Cloud9でXMPP4Rを使用しています。on_messageとon_private_message

conference.on_message {|time, nick, text| 
    case text 
     when /regex/i 
      #Same Command as on_private_message 
     end 
    end 
} 

conference.on_private_message {|time,nick, text| 
    case text 
     when /regex/i 
      #Same Command as on_message 
     end 
    end 
} 

conference.on_messageチャットから会議のメッセージであり、conference.on_private_messageは、会議のプライベートメッセージチャットです。

on_messageとon_private_messageの両方を上記の2の代わりに1として機能させたいと思います。

私はこれを以下のように試しましたが、それはconference.on_private_messageのみでした。どうすればそれを可能にすることができますか?

(conference.on_message || conference.on_private_message) { |time, nick, text| 
    case text 
     when /regex/i 
      #Same Command on both on_message and on_private_message 
     end 
    end 
} 

答えて

0

私の目的はあなたのコードをDRYすることです。 Procオブジェクトを作成して両方の関数に送る価値があるかもしれません。

proc = Proc.new { |time, nick, text| 
case text 
    when /regex/i 
     #Same Command on both on_message and on_private_message 
    end 
end 
} 
conference.on_message(&proc) 
conference.on_private_message(&proc) 

また、#sendメソッドを試すこともできます。

[:on_message, :on_private_message].each { |m| conference.send(m, &proc) } 
関連する問題