2017-03-22 16 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

나는 귀하의 코드를 건조시키는 목적으로 알고 있습니다. 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) }