Saturday, January 17, 2009

Ramaze Hello World

It wouldn't be right to start out learning about any computer language without beginning with the ubiquitous "hello world" program. I first saw it with Kernighan and Richie's "The C Programming Language", but my guess is it predates them by quite a bit. The idea is that if you can get a program to run that does nothing but print out "hello world" to the screen, you have actually accomplished quite a bit. In the case of programming languages, you have used an editor to compose the program, the language's compiler to compile it to machine code, the operating system's linker to make the program runnable, and finally, you were able to actually run the program and see something.

Here's the "hello world" program for ramaze:

require 'rubygems'
require 'ramaze'

# you can access it now with http://localhost:7000/
# This should output
# Hello, World!
# in your browser

class MainController < Ramaze::Controller
def index
"Hello, World!"


This is straight from the site but also appears in your examples/basic directory. On my Ubuntu machine it is:


There are other examples in there that may also be of interest.

So, what's going on here? The first couple of lines are bringing in required "libraries" including the ramaze "library" (I'm using the word library here loosely as they aren't compiled libraries as you would normally think of them). The next few lines beginning with a "#" are comments. If you didn't recognize them, you might want to have a basic Ruby book on hand to follow along with. A lot of this won't make sense if you don't know ruby. The next line starts the MainController (we actually could have called it anything) class and tells us it is derived from the Ramaze::Controller class. All of our controllers will be derived from this class. The next three lines define a method index for our class that returns the text string "Hello World!". Finally, we close out the class and start Ramaze.

If you copy this code into a text editor, save it as hello.rb, and then do either:
ruby hello.rb
ramaze hello.rb
you should start up the webrick web server. Point your browser to:
and you should see Hello World! there.

We're using a few defaults here to get this to display. Notably the fact that if we don't tell Ramaze anything else, it will use the index method as the default to display. One other item is the ::7000 which is the port to use. 7000 is the default for Ramaze and in a future post, we'll discuss how to change this.. Rails uses 3000 as the default for comparison.

Post any questions or comments on this in the comments section.

No comments:

Post a Comment