Chapter 3: Your First Ruby Program
Now that we have installed Ruby on the computer, the next step is to write a simple program and run it.
To write code, we’ll need a code editor. I recommend using Visual Studio Code. Unless you already have your favorite editor, download VS Code from its website.
We’re now ready to start writing awesome Ruby code.
Let’s create a new directory called ‘learn-to-code’ on your computer and open it in VS Code.
$ mkdir learn-to-code
Now create a new file in this directory called code.rb
. This is the file where our Ruby code will live.
Let’s write the first line of Ruby code that prints a greeting when executed.
print 'Hello World'
That’s it. We’re done. This is a complete Ruby programin. Nothing else is needed. No require
or include
statements, no public static void main()
, no semi-colons, no indentations, nothing.
Let’s run it.
In the terminal window, navigate to your directory and run the following command.
$ ruby code.rb
Ruby will execute our program and print ‘Hello World’ on the screen. Amazing, right?
Congratulations, that was your first Ruby program.