The “Hello World!” application is one of the first things that a budding computer programmer learns. It teaches the programmer how to generate output, a very important part of the programming process, and gives them the visual encouragement that they need to continue their programming career by showing that their programming effort has accomplished something -- no matter how menial it seems to seasoned developers.
Ruby is a very high level scripting language (VHLL). It’s primary use is behind-the-scenes work. By default, there is no graphical user interface. Ruby has a wide variety of uses. It has been used to do intense mathematical work; as the glue that holds compiled parts of applications together; and even as a server-side scripting engine for many professional websites.
Attempts have been made to make Ruby into a a viable graphical design platform -- most notably with the development of the persona whytheluckystiff, but lost steam once _why, the main developer, mysteriously disappeared from the Internet.
This article shows you how to create a simple “Hello World!” script in Ruby.
1) The first thing you want to do is open the Interactive Ruby (IRB) -- the interactive Ruby interpreter.
IRB allows you to test parts of Ruby script in real-time and see their results. Compared to a traditional scripting language like PHP, IRB is a real time-saver. With PHP you would have to write the code in a text editor, save it as a .php file, upload the .php file to your web server, open your web browser and navigate to the .php file to test it. With Ruby you can just copy and paste the code into IRB on your local machine.
To start IRB type in irb in to your shell or command prompt if you are on Windows. On Windows, if you installed RubyInstaller, you can also locate IRB by going to Start Menu -> All Programs -> Ruby 1.x.x -> Interactive Ruby.
2) In Ruby, unlike C, there doesn't need to be any sort of main function defined to start the script. The interpreter will read the script as a flat-file, line-by-line, executing the code. Of course, Ruby can include other .rb files, and jump around in the execution process, but the mechanics are still the same.
To output text in Ruby to the screen, the command you use is called puts.
Puts does, well... It puts the text on the screen. The proper syntax for the puts command is: puts "example". You can also surround the argument supplied to puts in parenthesis like this: puts ("example") but rarely are parenthesis used like that in Ruby.
To produce our "Hello World!" script, we simply need to pass the phrase "Hello World!" as an argument to puts.
3. Your IRB prompt should look like this:
Type the following and hit Enter:
puts "Hello World!"
IRB should interpret your command and display the output like so:
Join the Conversation