Ruby/Basic CGI

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
** Fast CGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Basic CGI.Using fastCGI

To quote Wikipedia, "CGI is a protocol for interfacing external applications to web servers. CGI applications run in a separate process, which is created at the start of each request and torn down at the end."

You can get yourself more familiar with the concepts here if it's the first time you hear about CGI and you can read more about fastCGI here

The Ruby class CGI offers support for writing CGI scripts.An interesting implementation of cgi in Ruby is the eRuby ERB templating system. Without the templating system, if you were to create a web page using regular cgi, things could easily get out of control because of the large amounts of code you need to write.

Here's how a simple cgi created html page could look:

#!/usr/bin/ruby
# test.cgi
puts "Content-Type: text/html"
puts
puts "<html>"
puts "<body>"
puts "<h1>Hello World!</h1>"
puts "</body>"
puts "</html>"

Here's how a simple cgi created html page could look when created in Ruby:

#!/usr/bin/ruby
# test.cgi
 
require 'cgi'
 
cgi = CGI.new("html4")
 
cgi.out {
 cgi.html {
   cgi.body {
     cgi.h1 { "Hello World!" }
  }
 }
}

After a few more lines like these you'll have problems reading and figuring out your own code; the main problem is the fact that it's written in a very systematic, unnatural way...it doesn't seem to be easy to interpret as a language.

Here's how a smarte cgi script saved in a Ruby script file can get you out of trouble:

#!/usr/lib/ruby/
require 'erb'
require 'cgi'
cgi = CGI.new("html4")
print cgi.header("type"=>"text/html")
headline = cgi.h1 { "Hello world!" } + "\n" + cgi.hr
input = File.read('test.eruby')
eruby = ERB.new(input) 
.................
puts eruby.result(binding())

So here are some basic concepts of cgi in Ruby:

escaping/unescaping special characters:

require 'cgi'
puts CGI.escape("black/white")

A few more other samples:

Image:Escape.gif

Previous Next