Create a File

Open up a text editor. I like using Sublime Text, but you can use any kind. There are several free text editors for download online. Or you can use without saving.

Create a new file and type in the method we wrote earlier.

def hello
  puts "Hello World!"

Save this file as hello.rb to your desktop. Now you can run this file from your terminal. In your terminal, navigate to the Desktop directory using cd, the change directory command.

If you don’t know how to find your file from the terminal console, I can help you with a simple trick. Just type the following. (This will take you to your root folder and then to your desktop).

Your-Computer-Name:$ cd
Your-Computer-Name:$ cd ~/Desktop
Your-Computer-Name:Desktop $

Now that we are in the Desktop directory, where our hello.rb file is stored, we can run our Ruby file using the ruby command! Type ruby hello.rb into the console. It may look something like this.

Your-Computer-Name:Desktop $ ruby hello.rb
Your-Computer-Name:Desktop $

If you had no errors and no output, then your program ran as expected. Great job!

But wait, why didn’t it put "Hello World!" to the screen? Well, this is because our hello method was not called. We simply ran a program that contained our hello method, but we didn’t specifically call upon that method to be run by the computer. We can fix this by updating our hello.rb file.

def hello
  puts "Hello World!"


Now when we run hello.rb, our hello method is called on line 5. We call a method by simply writing the name of the method. The parenthesis are optional, because this method has no arguments. If there were arguments, or inputs into our method, we would put them inside the parenthesis. Now when we run our program, we should see output like this:

Your-Computer-Name:$ ruby hello.rb
Hello World!

Congratulations! You just ran your first Ruby program with its very own method. It ran successfully and outputted "Hello World!" to the screen using Ruby’s built in puts method.

Now let’s try writing a method that takes an argument.