Created
September 5, 2011 17:08
-
-
Save ah-itagile/1195481 to your computer and use it in GitHub Desktop.
First try to solve the ccc-kata with ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
$hostname = 'localhost' | |
rec_port = 9000 | |
$send_port = 9001 | |
rec_socket = UDPSocket.new | |
rec_socket.bind($hostname, rec_port) | |
$operators = { | |
'ADD' => :+, | |
'MULTIPLY' => :*, | |
'SUBTRACT' => :- } | |
def respond_to message, args | |
id = args.shift | |
mapped = args.map &:to_i | |
reduced = mapped.reduce(mapped.shift, &($operators[message])) | |
response = id + ':' + reduced.to_s | |
UDPSocket.new.send(response, 0, $hostname, $send_port) | |
end | |
loop do | |
data = rec_socket.recvfrom(100) | |
args = data[0].split(':') | |
message = args.shift | |
exit if message == '__SHUTDOWN__' | |
respond_to message, args | |
end | |
s.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment