How to Execute Ruby Code
On macOS
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.
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.
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.
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
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.
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.
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.
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
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.
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.
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.
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.
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.
You should read it
- Ruby programming language for beginners
- Why Web Designer should learn Ruby on Rails?
- Learn Ruby programming from 0
- 10 most exotic waterfalls in the world
- 'Father' of the 10 most popular programming languages in the world - Who are they?
- Beginners of computer programming need to focus on what?
- Strangely, the catfish from the sky to the pool is still alive
- Statistics of the most 'hated' programming languages
- 16 programming languages will change your luck
- Do you know the 15 hottest programming languages on this GitHub?
- Why are flagship smartphones more expensive?
- 16 most popular, easy-to-find programming languages