How to Execute Ruby Script in Windows?

Ruby is a dynamic, object-oriented programming language and a Ruby script is a plain text file containing code written in the Ruby programming language. Ruby scripts are saved with a .rb extension. Ruby scripts can be used to write programs as simple as “Hello, World!”, to complex applications, such as web servers, automation scripts, or data processing tools.

To execute the Ruby Script in Windows, you need to make sure that Ruby is installed in the system and that its path is added to the system’s environment variables.

Check if Ruby is installed and the path is added to the environment variable by typing this command in your command prompt or Powershell:

ruby -v

Output should look like this, version number can differ in your system so do not worry about it:

In case any other output or error shows then read this article to install and set up it: How to install Ruby on Windows?

Ways to execute Ruby Scripts in Windows

In Windows, you can execute Ruby scripts using various methods but these are the most common:

  • Using Command Prompt or Powershell
  • Using Ruby Interactive Shell (IRB)

Using Command Prompt or Powershell

Create a file with any name and save it with “.rb” extension and write your ruby script in that file then open the Command Prompt or Powershell and locate it to the directory where you saved your Ruby Script.

Here in this example, we are executing “test.rb” script:

Ruby
puts "Hello Beginner"
puts "How are you?"

This up here is our ruby script and it can be executed with the command “ruby filename.rb”, in our case its “ruby test.rb”:

Using Ruby Interactive Shell (IRB)

The Ruby Interactive Shell, usually referred to as IRB (Interactive Ruby), is a command-line tool that allows you to interactively execute Ruby code. It provides a REPL (Read-Eval-Print Loop) environment where you can type Ruby code, and it will be executed immediately, displaying the results back to you.

You can start an IRB in your Command Prompt or Powershell just by typing “irb”:

You can execute code directly in Ruby Interactive Shell like this:

You can even run the Ruby Script file directly in Ruby interactive shell by loading it with load command: