How to Execute Ruby Code

On macOS

How 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 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 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 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 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 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 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 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 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 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 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 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 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

May be interested

  • How to Start Learning Computer ProgrammingPhoto of How to Start Learning Computer Programming
    programming is fun and incredibly useful. with programming, you will be creative with many open job opportunities. if you want to learn how to program, read the guide below to understand where you need to go and what you need to learn.
  • How to Compare Two Dates in JavaPhoto of How to Compare Two Dates in Java
    there are many ways to compare dates in java. in essence, the date represents a (long) point in time and is written as the number of milliseconds that have passed since 1/1/1970. in java, date is an object with many comparison methods. any method that compares two dates in essence
  • How to Learn Web DesignPhoto of How to Learn Web Design
    web design is a very valuable skill, especially in today's world where everything revolves around the internet. if you're really good at it, you can make money from customers, create a membership page (a website that restricts guests' access to content), or pursue other monetization projects. and if you really want to know how to learn web design then this article is for you!
  • The easiest way to Create EXE filePhoto of The easiest way to Create EXE file
    this wikihow teaches you how to create a basic exe file on a windows computer, as well as how to create a container for that file to install on another computer.
  • How to Compile and Execute Java Using Command PromptPhoto of How to Compile and Execute Java Using Command Prompt
    while many programming environments allow you to compile and execute a program within that program, you can also compile and execute a program using the command prompt command prompt.
  • The Easiest Way to Create an iPhone AppPhoto of The Easiest Way to Create an iPhone App
    the app market is constantly changing and shifting, and the success stories are big enough to catch anyone's attention. do you think you have the next big idea for an iphone app?