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

Picture 1 of How to Execute Ruby Code

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.

Picture 2 of How to Execute Ruby Code

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.

Picture 3 of How to Execute Ruby Code

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.

Picture 4 of How to Execute Ruby Code

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

Picture 5 of How to Execute Ruby Code

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.

Picture 6 of How to Execute Ruby Code

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.

Picture 7 of How to Execute Ruby Code

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.

Picture 8 of How to Execute Ruby Code

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

Picture 9 of How to Execute Ruby Code

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.

Picture 10 of How to Execute Ruby Code

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.

Picture 11 of How to Execute Ruby Code

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.

Picture 12 of How to Execute Ruby Code

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.

Picture 13 of How to Execute Ruby Code

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.

Update 17 March 2022
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile