How to Execute Ruby Code

Today's TipsMake will show you how to execute Ruby code on Windows, macOS, Ubuntu and Debian Linux. To run a Ruby program, the Ruby software must be pre-installed on the computer. Although macOS and most Linux distributions come with Ruby pre-installed, you need to check to make sure the current version is up to date before proceeding with the program. Also, if the Ruby code to deploy is written by you in a text editor or developer environment, save it as a .rb file so you can execute it from the command line.

On macOS

How to Execute Ruby Code Picture 1How to Execute Ruby Code Picture 1

Open the Terminal application. Macs come with the Ruby interpreter pre-installed in the operating system, so running the Ruby script is easy. To open Terminal, you need:

Click the Launchpad icon in the Dock (multicolored squares).

Type terminal in the search field.

Click the Terminal icon.

How to Execute Ruby Code Picture 2How to Execute Ruby Code Picture 2

Install the latest version of Ruby. It is possible that the version that came with the Mac is old and not installed with system updates. Follow these steps to install the latest version:

If you don't have Homebrew, you'll need to type /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" and press Return to install Homebrew.

Type brew install ruby ​​and press Return.

Type open -e ~/.zshrc then press Return to open the shell configuration file in TextEdit.

Add the following lines to the end of the file if your Mac uses an Intel-based chip:

if [ -d "/usr/local/opt/ruby/bin" ]; then

export PATH=/usr/local/opt/ruby/bin:$PATH

export PATH=`gem environment gemdir`/bin:$PATH

fi

Add the following lines to the end of the file if your Mac uses an Apple silicon chip:

if [ -d "/opt/homebrew/opt/ruby/bin" ]; then

export PATH=/opt/homebrew/opt/ruby/bin:$PATH

export PATH=`gem environment gemdir`/bin:$PATH

fi

Save and close the file.

Close and reopen the Terminal window.

Enter the command brew pin ruby ​​and then press Return.

How to Execute Ruby Code Picture 3How to Execute Ruby Code Picture 3

Use the cd command to go to the appropriate directory. When opening Terminal, the default location will be the home directory. To run Ruby code, you need to open the folder where the Ruby script is saved. For example, if the script is on the desktop, you need to type cd Desktop and press Return.

You can see the list of files in the current directory by typing ls -a and then pressing Return.

How to Execute Ruby Code Picture 4How to Execute Ruby Code Picture 4

Type ruby ​​scriptname.rb and press ⏎ Return. Remember to replace scriptname.rb with the actual name of the Ruby script you want to run. At this point, the Ruby script will be deployed.

On Windows

How to Execute Ruby Code Picture 5How to Execute Ruby Code Picture 5

Install Ruby on PC. If you don't have Ruby on your computer, you'll need to install the Windows version at https://rubyinstaller.org/downloads. The installation process is very simple, you just need to double click on the downloaded file and follow the on-screen instructions to install.

If you're not sure which version to download, look in the right column of the installer's website for recommended options.

During the installation process, keep the default settings (unless you know what you need to change). The default settings will add the Ruby directory to the system path so that you can execute ruby ​​code from the command prompt.

How to Execute Ruby Code Picture 6How to Execute Ruby Code Picture 6

Open the Start Command Prompt with Ruby application. You will find it in the Start menu after installing Ruby.

You can also click the Search bar (or magnifying glass icon) next to the Start button, type Command, and then click Start Command Prompt With Ruby from within the search results.

How to Execute Ruby Code Picture 7How to Execute Ruby Code Picture 7

Use the cd command to switch to the directory containing the Ruby script. When you open Command Prompt, you will be in your home directory (usually C:/Users/yourname). If the Ruby script is on the desktop, you need to type cd Desktop or C:/Users/yourname/Desktop and press Enter.

How to Execute Ruby Code Picture 8How to Execute Ruby Code Picture 8

Type ruby ​​scriptname.rb and then press ⏎ Return. Don't forget to replace scriptname.rb with the actual name of the Ruby script you want to execute. At this point, the Ruby script will start running.

Debian and Fedora Linux

How to Execute Ruby Code Picture 9How to Execute Ruby Code Picture 9

Open a command line window. You can do this by pressing the Control + Alt + T key combination or clicking the Terminal icon in the application list.

How to Execute Ruby Code Picture 10How to Execute Ruby Code Picture 10

Type ruby ​​-v and press ↵ Enter. This command will check the Ruby version. If the current version is older than 2.7.1, you should consider upgrading.

How to Execute Ruby Code Picture 11How to Execute Ruby Code Picture 11

Install or update Ruby if necessary. If you don't have Ruby or are using an older version, do:

Type sudo apt-get update and then press Enter to update the package list.

Type sudo apt-get install ruby-full and press Enter to install the latest version of Ruby.

How to Execute Ruby Code Picture 12How to Execute Ruby Code Picture 12

Use the cd command to change to the directory containing the Ruby script. For example, if the script is in the code folder in the home directory, type cd code and press Enter.

Type ls -a and then press Enter to view the files in the current directory.

How to Execute Ruby Code Picture 13How to Execute Ruby Code Picture 13

Type ruby ​​scriptname.rb and press ↵ Enter. Remember to replace scriptname.rb with the actual name of the Ruby script you want to run. The Ruby script will begin to be deployed.

5 ★ | 1 Vote