Simplest webserver in Ruby

A server is to serve requests at a place (localhost) of designated port (3000).

By using Socket we can implement a server easily:

require "socket"

server = TCPServer.new "localhost", 3000
server.listen 10000

response = "Hello from Ruby!"

loop do
  io = server.accept
  io << "HTTP/1.1 200 OK
Content-Type: text/html; utf-8
Connection: close
Content-Length: #{response.size}

#{response}"
  io.close
end

Now visit http://localhost:3000, you should see

Hello from Ruby!