Hello World

by Curt Hibbs

I thought that "Hello World" would be very appropriate for my first post to O'Reilly's new Ruby blog.

What got me thinking about this was a very funny article I just read that showed how the form of a Hello World program changes as the developer progresses in his career. Here are a few excerpts:

High School/Jr.High


10 PRINT "HELLO WORLD"
20 END


First year in College


program Hello(input, output)
begin
writeln('Hello World')
end.


Senior year in College


(defun hello
(print
(cons 'Hello (list 'World))))


New professional


#include
void main(void)
{
char *message[] = {"Hello ", "World"};
int i;
for(i = 0; i < 2; ++i)
printf("%s", message[i]);
printf("\n");
}


After this they get too long to post here, but its really hilarious. I especially liked the "Master Programmer" example which was an MS COM/C++ program (which brings back painful memories).

As these Hello World programs got progressively more complex, it made me think about how the Ruby version returns to utter simplicity:


puts 'Hello World!'


Which is much better than the Java version (my previous language of choice):


// Must be stored in a file called "hello.java"
public class hello {
static public void main(String[] argv) {
System.out.println("Hello world!");
}
}


Even in Ruby, a Hello World program can be educational, judiciously showing off some of the language features. My favorite such Hello World program in Ruby is:


# The Greeter class
class Greeter
def initialize(name)
@name = name.capitalize
end

def salute
puts "Hello #{@name}!"
end
end

# Create a new object
g = Greeter.new("world")

# Output "Hello World!"
g.salute


That nice little piece of Ruby code was written by either John Long, Ben Giddings, or Michel Martins -- I can't remember which one, so I mention all three.

Anyway, go read the Hello World Programs page and have a little chuckle!

4 Comments

Robert Citek
2005-12-20 17:41:49
If you gave the new Greeter object a lowercase "world" then the capitalize method would have something to do. :)


For example:


g = Greeter.new("world")


BTW, I tried this out under Knoppix 4.0.2 CD and it worked just fine. Thanks for the example.


Regards,
- Robert

Curt Hibbs
2005-12-20 20:05:05
Good catch Robert... I fixed it!
Free Ringtones
2006-03-20 14:25:08
Great page! Fully enjoyed it! If you want free ringtones, visit Free Ringtones
Free Ringtones
2006-03-20 14:47:00
Great page! Fully enjoyed it! If you want free ringtones, visit Free Ringtones