Introduction To Ruby
Hello World

The traditional way to begin with any new programming language is to write a program that outputs the phrase 'Hello World'.

Puts

Assuming that you have installed Ruby on the computer you are using, open your favourite text editor and type the following,

puts "Hello World"
gets

The first line outputs the text in quotes and adds a line break. The gets statement on the second line is an input statement. It makes the program wait for a key to be pressed. If you don't do this, your program will close when finished and you won't see the output.*

The file needs to be saved with a .rb extension. If you are using Notepad on a Windows machine, you will need to fill in the save dialog with something like,

Saving A Ruby Script

The red box shows the key areas. If you don't change the second dropdown to All Files, your file will be saved with a .txt extension and will not be run by the Ruby interpreter. Locate the file you saved and double click on it. You should see the following,

Running A Ruby Script

*   This isn't strictly true. If you have access to the Command Prompt, you can navigate to the folder where the script is stored using cd folder, type the name of script file, press enter and then you don't need the gets statement.

Print

You can also use the print statement. The difference is that no line break is placed at the end. The next time you use the print statement, the output continues from the last point.

print "Hello"
print " World"
gets

Two print statements are used here to illustrate the point about line breaks. If you run this side-by-side with the first program, you can see that, in the first, the cursor blinks on the second line. In this program, the cursor is blinking at the end of the line of output.

Challenges

You haven't a lot to challenge yourself with yet. Writing a few programs that output amusing words to the screen would be a good way to make sure you have got the hang.