This is a card in Dave's Virtual Box of Cards.

Ruby CGI

Created: 2022-07-25

For quick CGI scripts, Ruby easily holds its own. Example: todo-cgi

Make a directory listing:


source_dir = 'foo/bar/'
web_path   = '/my/foo/bar/'

puts "Content-Type: text/html"
puts ""
puts "<html><body>"

Dir.entries(source_dir).sort.each do |fname|
    fpath = "#{web_path}#{fname}"
    puts "<a href=\"#{fpath}\">#{fname}</a><br>"

puts "</body></html>"

And there’s a CGI module built right into the standard library:

But you don’t even need that for environment variables, which is why CGI is so dang sweet.