How to Execute Ruby Files and Commands in Console

MD Aminul Islam Feb 15, 2024
  1. Execute a Ruby Program in the Console
  2. Install Ruby in Your System
How to Execute Ruby Files and Commands in Console

You may be found some online interpreters to code, compile and run Ruby codes online, but if you are willing to have Ruby in your system terminal, you need to install it first.

In this article, we will discuss how we run a Ruby program in the system console and install Ruby in our system if necessary.

We will see an example relevant to the topic to make the topic easier. Also, we are going to see the installation process of Ruby in local systems so that you can take a look at it if Ruby is not present in your system.

Execute a Ruby Program in the Console

To execute a Ruby program in your console, follow the example below. In our below example, we are going to run a Ruby file named example.rb, and the command will look like this:

ruby example.rb

And it will show you the output of the example.rb file.

DelftStack

Interactive Ruby Environment

If you want to code directly in the console, then you can start the Interactive Ruby Environment by the following command:

irb

Now you can run the Ruby command directly in your console like the below:

irb(main):001:0> puts "DelftStack"
DelftStack
=> nil
irb(main):002:0>

Install Ruby in Your System

You cannot run the Ruby commands or files in your system if your system doesn’t contain Ruby. To install Ruby in your system environment, follow the below steps.

  • Download the RubyInstaller

    Before we start, we need to download the RubyInstaller from this page.

    Here you will have two steps, the first one is with Devkit, and the second one is without Devkit. Please note that if you are willing to work on a Ruby on Rails app, you must install the Devkit.

    RubyInstaller

  • Installing the RubyInstaller

    Now when you are done with the first step, you need to open the installer and install it step by step like the below images:

    Accept the license and click Next.

    Accept the license

    Specify the path to install and click Next.

    Specify the path

    Select components and click Next.

    Select components

  • Setting up MSYS2 toolchain

    When you are done with the installation process, an important checkbox on the final screen, Run 'ridk install' to set up MSYS2 will appear. You need to check it, and it will send you to a terminal window asking you to select a component to install.

    You need to hit Enter, which will install the base MSYS2 system, check for the updates, and finally install the development toolchain.

    development toolchain

    cmd

  • Check if Ruby was installed successfully

    To check if Ruby has been installed successfully in your system, open your terminal and type the below command.

    ruby -v
    

    If everything is fine, you will get the below output in your console.

    ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x64-mingw-ucrt]
    

    Now Ruby is available in your system, and you can run any Ruby program using your terminal.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn