How to Write a Simple "Hello World!" Script in Ruby

0 Comments
Join the Conversation
Hello Ruby. - Peter Cooper
Hello Ruby. - Peter Cooper
Introduction to the Ruby programming language, teaching the reader how to write a simple "Hello World!" script.

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:

irb(main):001:0> _

Type the following and hit Enter:

puts "Hello World!"

IRB should interpret your command and display the output like so:

irb(main):001:0> puts "Hello World!"
Hello World!
gt; nil
Congratulations. You've just executed your first Ruby script.
The "=> nil" output is IRB debugging information and means that the puts command returned a nil value, which is good. If an exception had occurred while executing a command, it would appear in place of nil.
One last thing to remember is that everything that you type in IRB can be saved in an .rb text file and executed as a stand-alone script in Ruby without using IRB. You can test this by creating an .rb file, such as test.rb and executing the command ruby test.rb in your shell or command prompt.
Richard and Amanda Silvers, Author

Richard Silvers - My wife and I are practitioners of a paleolithic diet. Much of my writing covers the topics of health, nutrition, fitness, and weight ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 2+1?
Advertisement
Advertisement