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:

#!/usr/bin/ruby

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>"
end

puts "</body></html>"

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

https://ruby-doc.org/stdlib-2.5.3/libdoc/cgi/rdoc/CGI.html

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