Ruby tutorial: Hello world

This tutorial helps you get started with Ruby. It explains what you need to write your first Ruby program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a Ruby program before this is the place to start. We have created this article, so you can get started with Ruby, and move on to our sms examples. If you are already familiar with Ruby, you can jump directly to one of the following SMS projects.

Ruby sms examples:

Ruby send sms with the HTTP rest api (code sample)
Ruby send multiple sms with the HTTP rest api (code sample)
Ruby schedule sms with the HTTP rest api (code sample)
Ruby receive sms with the HTTP rest api (code sample)
Ruby delete sms with the HTTP rest api (code sample)
Download the latest Ruby sms api library from Github

What is Ruby

Ruby is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is a Ruby Hello world program

The Ruby hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Ruby

To write your first program in Ruby:

  1. Check prerequisites
  2. Setup Ruby
  3. Open Notepad
  4. Save the file with .rb file extension
  5. Write Hello World program in Ruby
  6. Run your Ruby Hello World program
  7. Check the output
  8. Handle the errors

Prerequisites

Here is what you need to get started. To write your first computer program in Ruby, you need a Windows computer and example code presented below.

  • Windows 10 computer
  • Ruby
  • Ozeki Hello World example project

Download Ruby

You may download Ruby from the following URL: https://rubyinstaller.org/downloads/

Install Ruby

To write your first Ruby program, you need to install Ruby. The following video shows you how this installation can be done.

Create your first Ruby program

After Ruby has been installed, you need to open the Notepad.

Open Notepad

To get started with coding in Ruby, you must first open Notepad. Type "notepad" into the search bar on the bottom left (Figure 1). The first result will return the Notepad application. Click on it to open.

notepad
Figure 1 - Open Notepad

Save as

Click on File, then Save as... to save your file (Figure 2). This will open up a new window.

save as
Figure 2 - Save as

Name the file

Once you clicked Save as... , a new window opens. Here you can name your file and specify a location (Figure 3). Make sure it is saved as a .rb file. Click on the Save button to save your file to the selected directory.

file name
Figure 3 - File name

Write your 'hello world' program in Ruby

The first program you write in any programming language is the 'Hello world' program. The sole aim of this program is to print the term 'Hello world' to the computer screen. In this example the program consists of two lines of code: The first line: puts "" prints the text.

puts "Hello world!"

Write the code

This piece of code has only one purpose. It displays the "Hello world!" message. In Ruby it only consists of one line. The puts command (short for put string) outputs text from a string data type. The text you wish to output will go after the puts command and inside of quotation marks (Figure 4).

write code
Figure 4 - Write the code

Run the Ruby 'hello world' program

To run the hello world program, you need to press Enter.

Run the program

Start by opening up the Command Prompt. Change the directory to where your file is located. Now type in the name of the file (FirstRubyApp.rb) and hit enter (Figure 5). You can see that the "Hello World" message has appeared, indicating that the code ran successfully.

run the program
Figure 5 - Run the Ruby Hello world program

What happens if I make a mistake in Ruby

If you make a mistake, when you write your instructions in Ruby, you will get a Syntax error. The computer will highlight the line with error in red, and it will tell you why didn't it understand the instructions. To correct the mistake you need to go back to the text editor and modify the program. Programs say they "fix the error" when they correct mistakes.

What is syntax error in Ruby?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in Ruby and the computer does not understand what you say, he will reply with "Syntax error".

In the code below we will create a mistake intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

How to handle a syntax error in Ruby?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Errors in Ruby

Now we are going to take a look at what happens when the code is not correct. Modify the code to make a mistake intentionally. Replace the "puts" command with "put". This will lead to an error when you run the program. The hello world message will not appear, but an error message gets displayed instead (Figure 6). This message is useful, because it tells you what type of error you have made. This way it is easier for the developer to find it and fix it.

error
Figure 6 - Error

Summary

This article was written to help you to get started with Ruby by teaching you how to write a basic Hello World program in this language. We have done our best to make this tutorial as clear and comprehensible as possible so that you can learn the fundamentals of coding in Ruby effortlessly. The Hello World program is just the beginning, while you follow the shown steps, you learn everything to prepare for coding any bigger Ruby program.

You can find other articles on the Ozeki website to learn more about how to create and use Ruby solutions. Continue your studies here, read the next tutorial titled How to send SMS from Ruby.

Don't waste your time, download the Ozeki SMS Gateway now!

More information