【问题标题】:on_message and on_private_messageon_message 和 on_private_message
【发布时间】:2017-08-13 22:24:19
【问题描述】:

我目前在 Cloud9 上使用 XMPP4R。

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 都作为 1 而不是上面显示的 2。

我尝试过这样的事情(如下),但它只工作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  
}

【问题讨论】:

    标签: ruby-on-rails ruby c9.io xmpp4r


    【解决方案1】:

    据我了解,目的是干燥您的代码。可能值得创建一个 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) }
    

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多