TRAVEL

Surfing in Maldives

It’s windy. The cool breeze of the ocean. It gives, a sense of beauty, in motion. All is flowing, rushing and tide-And I sit in wonder, dreaming beside.

LEISURE

Outdoor
Experience

It’s windy. The cool breeze of the ocean. It gives, a sense of beauty, in motion. All is flowing, rushing and tide-And I sit in wonder, dreaming beside.

jun 14, 2017

How to Run A Ruby File In Terminal | On A Mac

Where is my Ruby file and how have i forgotten how to run a .rb file in terminal already?

learn to code

Bash: “command not found”

Where is my Ruby file and how have i forgotten how to run a .rb file in terminal already?

After having taken a few days off programming and learning Ruby to do some freelance marketing work something terrible seems to have happened. I have forgotten the most simple of terminal commands … How to run a Ruby file.

So in getting myself quickly up to scratch again I thought I would write a quick guide for everyone who is getting started (or started again in my case) with learning Ruby and Terminal.

Now, if you are a complete beginner, you may be thinking:

“What is ‘Terminal’ & why are you telling me to use it? I thought I was supposed to be using ‘Command Line’? “

Most Ruby scripts do not have graphical user interfaces then they are ran using the command line or command prompt. The command line or prompt as far as I am aware, can only be accessed though the terminal.

How To Find The Terminal On A Mac

  1. Click on the search icon on the top right hand corner of your screen. The Spotlight search bar will then appear.


2. Type ‘Terminal’ into the search bar.

3. The terminal will pop up.

Alternatively:
1. Click on ‘Finder’ icon in the dock.


2. Go to the ‘Applications’ folder on the right hand hand side.

3. Search in this folder for the Terminal.

Creating Your Ruby File

At this point I am going to assume you have been learning ruby so have written a few ruby documents before. However if not follow the simple steps below:

1. Download or open your text editor. I use Sublime text editor, which is a good one for beginners like myself.

2. Create a ruby file by typing into your text editor:
puts “Hello Wold”

3. Save this file as ex1.rb — the important part of this is the .rb this must be at the end of any file for your computer to recognise it as a ruby document.

Installing Ruby To Your Mac

On a Mac Ruby should be pre-installed. Check this by typing into the terminal

:ruby -v (the space after ruby before ‘-‘ is important)

You should get a message similar to:

ruby 2.4.1p111 (2017–03–22 revision 58053) [x86_64-darwin16]

2.4.1 may not be the same number you will see as the pre-installed ruby does not tend to be the latest edition. I advise updating your ruby and checking you are running the latest version. How to do that can be found in these guides:

This is the guide I used for installing Ruby & Rails.This is a simple guide for Ruby — For beginners stop just before the installation of a database as that isn’t necessary for you for now.

Running Your Ruby File


When you open your terminal you should find some script already there. It will most likely contain information on your computer and username then be followed with your prompt.

The prompt is usually a single character: $ or #.

1st | Make sure you are in the folder that your .rb file is saved in. To do this input:

pwd (print working directory)

to show the folder you are in.

I save all my .rb files in a folder I have named “Ruby work”. This is usually not the folder my command line starts in so I change it to this folder (or directory) using the command:

cd (change directory)

I type into the command line: cd Ruby work

2nd | (Optional) Check the your .rb file is in the folder (directory) you have changed to by typing:

ls (list directory)

into your command line. This will give you the full list of all the files in the folder.

3rd | Run your ruby file by typing into your command line:

ruby ex1.rb

Do not forget to type in ‘ruby’ before your file name. This is because you need to tell the terminal that you are wanting to use the Ruby programming framework to run your file.

If you have used the example .rb file you should see the following script appear in your terminal:

Hello world.

Getting An Error Message?

If you are getting an error message, something along the lines of:

syntax error, unexpected keyword_end, expecting end-of-input

that means there is an issue with your code in your .rb file, not the terminal or the running of the file itself.

How To Stop Terminal Running Your Script

If you have gone wrong somewhere you may need to stop the terminal or go back up to where you started. You can do this simply by closing and re-opening the terminal.

Or you can force a quit “kill” by pressing:

“Ctrl” & “C” at the same time.

That is all for me for now. Hope this helped you get started or get back to running ruby files within your terminal. If you have any questions while learning ruby then send me a message.

Thanks for read 🙂