I am currently using XMPP4R on Cloud9.
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
is the conference's message from a chat, and conference.on_private_message
is the conference's private message chat.
I want to make both on_message and on_private_message to function as 1 instead of 2 shown above.
I tried something like this (below), but it worked only conference.on_private_message
. How can I make it possible?
(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
}
As I understand the purpose is to DRY your code. It might be worth creating a Proc object and sending it to both functions.
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)
You could also try using #send method.
[:on_message, :on_private_message].each { |m| conference.send(m, &proc) }