Executable file in Ruby

Merce Bauza
2 min readDec 18, 2018

In order to run a program, the computer needs to execute it following precise instructions specified in a file in the application.

In a Ruby project, this file contains commands for the Bash to execute the file, followed by the scripts of the starting point of the application.

Location

The location of the file is usually in a folder called “bin”.

Name

The executable file won’t have .rb at the end. It only needs to have its name specified.

Instructions

In order to be an executable file, the following instructions for the bash have to be included on the top of the file.

#!/usr/bin/env ruby
$: << File.join(File.dirname(__FILE__), ‘..’, ‘lib’)

#! is called a shebang and it tells the shell what program to interpret the script with, when executed.

/usr/bin/env ruby is the path to the env command. env helps to find the correct interpreter for a script, ruby in this case, when the ruby may be in different directories on different systems.

$: is the default search path, a predefined variable in Ruby.

File.join(File.dirname(__FILE__), '..', 'lib') takes the path of the executable file and joins them with ‘ /’ with '..' and 'lib'. Resulting in '/location-of-the-file/../lib'

Scripts

After the instructions on how to execute the file, the code to run the application is found.

It will contain the least amount of code possible to start the application because the scripts in the executable file are not testable.

Permissions

Since we want to execute our project, we’re going to have to grant it execute permissions.

Firstly, we will check the permissions:

ls -l ttt_ruby

To be able to execute the program, a x (executable) needs to be in the line.

drwxr-xr-x 3 user_name staff 96 18 Dec 09:21 bin

As seen above, I have the permissions, but if it is not the case, you would need to run the following command to add them.

chmod 755 ttt_ruby

After adding your permissions and your executable file, you should be able to run your program by getting in the bin file and running:

Ruby name_executable_file

--

--