Skip to content

Instantly share code, notes, and snippets.

@ah-itagile
Created September 5, 2011 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ah-itagile/1195481 to your computer and use it in GitHub Desktop.
Save ah-itagile/1195481 to your computer and use it in GitHub Desktop.
First try to solve the ccc-kata with ruby.
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