Skip to content

Instantly share code, notes, and snippets.

@borgand
Forked from mislav/config.ru
Created October 17, 2011 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borgand/1292211 to your computer and use it in GitHub Desktop.
Save borgand/1292211 to your computer and use it in GitHub Desktop.
Shortest simplest sweetest web "hello {NAME}" in Ruby

This is the shortest "hello {NAME}" Ruby program I could make. It fits in a tweet!

  1. Save it as "config.ru"
  2. gem install rack
  3. Run rackup
  4. Visit http://localhost:9292/NAME
run ->(e){ n=e['PATH_INFO'][1..-1]; [200, {'Content-type'=>'text/html'}, ["Hello #{n}!"]] }
# Of course, the same thing in Sinatra is way shorter! But I went for minimum dependencies above.
require 'sinatra'; get('/:name'){|n| "Hello #{n}!" }
# WEBrick equivalent, if we're limiting ourself to only Ruby stdlib and no dependencies
require 'webrick'
server = WEBrick::HTTPServer.new :Port => 8000
server.mount_proc('/') {|req, res| res.body = "Hello #{req.query['name']}!" }
trap('INT') {server.shutdown}
server.start
# Just ruby, not even WEBrick!
# Submitted by https://github.com/reu
require "socket"
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
socket.bind(Socket.pack_sockaddr_in(3000, "localhost"))
socket.listen(1)
loop do
connection = socket.accept[0]
connection.gets.match /name=(.+) (.+)/
connection.write "#{$2} 200 OK\nContent-Type: text/html\n\nHello #{$1}"
connection.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment