Tuesday, February 28, 2006

My first Ruby

Well, I have been playing with Ruby for a few days now. I finally found something at work that I could legitimately use it for, so I had to give it a shot. To demonstrate its power, here is the code for a web server. Now, I don't have to install one each time someone wants to download a file from me since GAIM's file transfer mode seems to be broken. Anyways, try and you will like...GUARANTEED!

require 'webrick'
include WEBrick

if ARGV.length == 1
s = HTTPServer.new(:Port => ARGV[0], :DocumentRoot => Dir::pwd)
trap ("INT", proc{s.shutdown})
s.start
else
puts "Usage: ruby webserver.rb port"
end

For some reason, alot of the sources I found online didnt mention calling the proc function with the shutdown block. Without this, I was getting a segfault in the Ruby shipped with Ubuntu 5.10. Go figure. Maybe some day I'll be knowledgable enough to track down whats going on?

No comments: