Sunday, March 22, 2009

Ruby: My first Ruby CGI program


Share at Facebook

Lets do some CGI programming using Ruby this time. But first of all if you don't know how to setup your apache with Ruby for CGI, here is a small suggestion about how to setup an environment to run a Ruby based CGI script on your apache. Remember this is only for Apache.

1) Download load Ruby from http://www.ruby-lang.org/en/downloads/, and install it at your c:\ruby folder

2) Download EasyPHP from http://sourceforge.net/project/showfiles.php?group_id=14045, and install it.

That's It. Everything is set up. Now write the below hello world Ruby CGI script inside your hello.rb script.

#!c:/ruby/bin/ruby

## Below line for HTTP HEADER
print "Content-Type: text/html\n\n";

print "<html>";
print "<head><title>Hello World</title></head>";
print "<body>";
print "<h3>My First Hello Ruby! Program</h3>";
print "</body>";
print "</html>";

From the above code, the first line means where is your RUBY exe is located.
The Content-Type is defining the type of your content for browser to display.
Others are the printing HTML codes using Print command of Ruby. You can use puts command instead of Print.

Now, save the hello.rb script inside Program Files\EasyPHP x.x\cgi-bin folder.

Start your apache, and browse the ruby cgi script using the below URL
http://localhost/cgi-bin/hello.rb

It will display My First Hello Ruby! Program at your browser.

That's enough for today. I'll discuss more about this things soon in future. Keep in touch. Thanks to all.




1 comment:

Bob said...

Isn't it ironic that the example you give is also valid if done with Perl, and even works in PHP if you use it like that.

The world really is amazing ;)