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
}发布于 2017-03-22 18:51:37
据我所知,目的是让你的代码干涸。创建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) }https://stackoverflow.com/questions/42940912
复制相似问题