This is the third part of a series of articles. Part 2 can be found by clicking here.

Using the code I ended with from Part 2 (download here), I would like to embed this into a webpage so that I can begin using it. It looks like there are many different ways I can get this to work. Let’s review a few before I show you what I chose to do.

Embed Ruby into a Web Page

eRuby - appears to be one of the most common ways of embedding ruby into an HTML page. http://www.rubycentral.com/book/web.html is a great reference site showing how this can be possible. This also includes examples of how to run ruby as a CGI.

As a CGI:

#!/usr/bin/env ruby
print “HTTP/1.0 200 OK\r\n”
print “Content-type: text/html\r\n\r\n”
print “<html><body>Hello World!</html></body>\r\n”

Using eRuby:

<% ruby code %> The Ruby code between the delimiters is replaced with its output.
<%= ruby expresssion %> The Ruby expression between the delimiters is replaced with its value.
<%# ruby code %> The Ruby code between the delimiters is ignored (useful for testing).

The rubycentral site listed above also includes information on how to setup the Apache httpd.conf file to run eRuby files.

mod_ruby - is another option for embedding ruby in an html page. This option is more tightly coupled with Apache and gives a slight performance boost. The rubycentral site briefly mentions this mod_ruby approach, but more information can be found at http://www.modruby.net/en.

Each of these options seem very straight forward. I will definately need to remember each of these for a later release, but since I am using WordPressMU which is php, I am just going to use something that is built into php itself.

embed into PHP - Even though I will be embedding ruby into php, this same approach would work for any web scripting language that provides similar functions. I will be using the php system function like this:

echo system(’part2.rb’);

PHP also has exec and passthru, in addition to the system function. Each of these functions serves a slightly different purpose. Check out these links and see which one is best for your purpose.

So even though I am using the php system function, I think that I will move to the mod_ruby approach later one when I am trying to do more with the pages than just return some html.

I am still really enjoying ruby. It has been a lot of fun, and it seem very powerful. Now that I know it is so easy to embed, I am even more interested in it. Please check back for my next articles. I want to write a cache file so that I am not polling all of the RSS feeds upon every request. I will do that next. But I think after that I will start looking into Ruby on Rails and connecting to a database to store the cache.

Posted: September 9, 2006, 12:33 am by Brian Radford

Respond to this post or Trackback Link

So far none to this article

  1. So far no posts

Add your own post

You must log in in order to be able to contribute.



RSS 2.0-Feed for the comments to this article.