LinuxCommandLibrary

ruby

Execute Ruby scripts

TLDR

Execute a Ruby script

$ ruby [path/to/script.rb]
copy

Execute a single Ruby command in the command-line
$ ruby -e "[command]"
copy

Check for syntax errors on a given Ruby script
$ ruby -c [path/to/script.rb]
copy

Start the built-in HTTP server on port 8080 in the current directory
$ ruby -run -e httpd
copy

Locally execute a Ruby binary without installing the required library it depends on
$ ruby -I [path/to/library_folder] -r [library_require_name] [path/to/bin_folder/bin_name]
copy

Display version
$ ruby [[-v|--version]]
copy

SYNOPSIS

ruby [options] [program_file] [arguments...]
ruby [options] -e COMMAND [arguments...]
ruby [options] -c
ruby [options] -v
ruby [options] -h

PARAMETERS

-e COMMAND
    Executes the Ruby code specified in COMMAND as a string.

-c
    Checks the syntax of the Ruby script without executing it. Returns 0 if syntax is OK, 1 otherwise.

-w
    Enables verbose warning messages during execution. Useful for debugging and identifying potential issues.

-v, --version
    Prints the Ruby interpreter version number and exits.

-h, --help
    Displays a summary of command-line options and exits.

-r LIBRARY
    Requires the specified LIBRARY (e.g., a gem or a Ruby file) before executing the script.

-I DIRECTORY
    Adds DIRECTORY to the `$LOAD_PATH` ($:), where Ruby searches for required files.

-S
    Looks for the program using the system's `$PATH` environment variable. Useful for shebang-less scripts.

-i [EXTENSION]
    Enables in-place editing of ARGF files. A backup is created if EXTENSION is provided (e.g., -i.bak).

-d
    Turns on debug mode, which can provide more detailed output for debugging purposes.

-n
    Causes Ruby to loop over standard input line by line, executing the script body for each line. Does not print lines by default.

-p
    Similar to -n, but also prints each line after executing the script body.

DESCRIPTION

The ruby command is the official interpreter for the Ruby programming language. It executes Ruby source code directly from a file, standard input, or from a string provided on the command line.

Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. The ruby interpreter can be used for a wide range of tasks, including web development (with frameworks like Ruby on Rails), scripting, data processing, automation, and system administration.

Beyond executing scripts, the command can also be used to check Ruby code for syntax errors without running it, print the Ruby version, or configure its execution environment with various command-line options like warning levels and load paths.

CAVEATS

While highly productive, Ruby's performance can sometimes be a consideration for extremely CPU-bound applications compared to compiled languages like C or Java, though recent JIT compilers have significantly improved this.

Managing Ruby versions and gem dependencies can be complex, often requiring tools like RVM, rbenv, or `chruby` to ensure projects use the correct Ruby version and associated libraries. Incompatible Ruby versions or gem conflicts can lead to unexpected runtime errors.

ENVIRONMENT VARIABLES

The behavior of the ruby command can be influenced by several environment variables:

  • RUBYOPT: Specifies command-line options that are processed before any explicit options.
  • RUBYLIB: Specifies additional directories to be added to the `$LOAD_PATH` before any -I options.
  • PATH: Used with the -S option to locate the Ruby program file.

SHEBANG (#! )

Ruby scripts often start with a 'shebang' line, such as #!/usr/bin/env ruby or #!/usr/bin/ruby, which allows the script to be executed directly as an executable without explicitly calling ruby beforehand (e.g., ./myscript.rb instead of ruby myscript.rb).

EXIT STATUS

The ruby command typically exits with a status of 0 upon successful execution. A non-zero exit status usually indicates an error, such as a syntax error (when using -c), a runtime exception that was not handled, or the script exiting with a non-zero value.

HISTORY

The Ruby programming language was created by Yukihiro 'Matz' Matsumoto in Japan, with the first public release in December 1995. Matz aimed to create a language that was powerful, object-oriented, and focused on programmer happiness and productivity.

Ruby gained significant global popularity in the mid-2000s, largely due to the emergence of the Ruby on Rails web application framework. This framework showcased Ruby's strengths in rapid development and elegant syntax, leading to widespread adoption in the startup community and beyond.

Since its initial release, Ruby has undergone continuous development, with major versions introducing new features, performance improvements (like the Ruby 3x3 initiative, aiming for 3x performance increase over Ruby 2.0), and syntax enhancements.

SEE ALSO

irb(1), gem(1), bundle(1), rake(1), python(1), perl(1)

Copied to clipboard