Search code examples
rubyapachecgi-bin

Ruby equivalent of PHP's $_GET


What is the bare minimum I need to require in Ruby (not Rails or any other framework) to easily obtain request data like GETs?

I would like to avoid having to use ruby on rails or any other framework to get this data, so the ideal answer wouldn't have a framework dependency.

My current setup has a ruby file (script) in a cgi-bin on apache (site.com/script)

#!/usr/bin/env ruby

# bare minimum to output content
puts "Content-type: text/html"
puts

puts "it works!" #works fine.

# how would I obtain request data like GET parameters here
# for a url like site.com/script?hi=there
# in PHP, I would $_GET['hi']

Solution

  • Ah, solved my problem using the CGI class.

    http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html

    require 'cgi'
    
    cgi = CGI.new
    
    cgi.params.each do |key, val|
       puts key
       puts val
    end
    

    so site.com/script?hi=there would output hi\nthere