Ruby (with examples)
Ruby is a popular programming language known for its simplicity and readability. It is widely used for web development, scripting, and automation tasks. The Ruby interpreter allows you to execute Ruby scripts or individual Ruby commands from the command-line.
In this article, we will explore various use cases of the ruby
command, along with code examples and explanations for each. Whether you want to execute a Ruby script, check for syntax errors, start a local server, or execute a binary file without installation, the ruby
command has you covered.
So let’s dive into the different use cases of the ruby
command:
Use Case 1: Execute a Ruby Script
To execute a Ruby script, we use the following command:
ruby script.rb
Motivation: Executing a Ruby script allows you to run a series of instructions written in Ruby. This is useful when you have a standalone Ruby script that performs a specific task or solves a particular problem.
Explanation: The ruby
command is followed by the name of the Ruby script file you want to execute. The file must have the .rb
extension. The Ruby interpreter will read the script file and execute its contents.
Example Output:
Suppose we have a script file called hello.rb
with the following contents:
puts "Hello, World!"
Executing the command ruby hello.rb
will output:
Hello, World!
Use Case 2: Execute a Single Ruby Command in the Command-Line
To execute a single Ruby command from the command-line, we use the following command:
ruby -e command
Motivation: Executing a single Ruby command in the command-line is useful for quickly testing code snippets or performing simple tasks without the need to create a separate script file.
Explanation: The -e
flag followed by the command
argument allows you to provide a Ruby command directly on the command-line. The Ruby interpreter will execute the command and output the result.
Example Output:
Executing the command ruby -e "puts 3 + 4"
will output:
7
Use Case 3: Check for Syntax Errors on a Given Ruby Script
To check for syntax errors on a given Ruby script, we use the following command:
ruby -c script.rb
Motivation: Checking for syntax errors is essential to ensure that your Ruby script is free of any syntax issues before executing it. This helps in identifying and fixing errors early on, saving you time and preventing unexpected behavior.
Explanation: The -c
flag followed by the script file name (script.rb
) allows the Ruby interpreter to check the syntax of the given script without actually executing it. If there are no syntax errors, it will output “Syntax OK.” Otherwise, it will provide an error message indicating the syntax error and its location.
Example Output:
Suppose we have a script file called invalid.rb
with the following contents:
puts "Hello, World!
Executing the command ruby -c invalid.rb
will output:
invalid.rb:1: syntax error, unexpected end-of-input, expecting ')'
puts "Hello, World!
^
Use Case 4: Start the Built-in HTTP Server on Port 8080
To start the built-in HTTP server on port 8080 in the current directory, we use the following command:
ruby -run -e httpd
Motivation: Starting the built-in HTTP server allows you to quickly serve static files locally for testing or development purposes. It eliminates the need for a separate web server installation and configuration.
Explanation: The -run -e httpd
flags are used together to instruct the Ruby interpreter to run the built-in HTTP server. By default, it will serve files from the current directory on port 8080. You can access the served files by opening a web browser and navigating to http://localhost:8080
.
Example Output:
Executing the command ruby -run -e httpd
will output:
[2022-01-01 00:00:00] INFO WEBrick 1.7.0
[2022-01-01 00:00:00] INFO ruby 3.0.2 (2021-07-07) [x86_64-linux]
[2022-01-01 00:00:00] INFO WEBrick::HTTPServer#start: pid=12345 port=8080
Use Case 5: Locally Execute a Ruby Binary Without Installing Required Libraries
To locally execute a Ruby binary without installing the required library it depends on, we use the following command:
ruby -I path/to/library_folder -r library_require_name path/to/bin_folder/bin_name
Motivation: Sometimes, we may want to execute a Ruby binary that depends on specific libraries without installing those libraries globally. This allows us to test or run the binary in isolation without affecting the system-wide Ruby environment.
Explanation: The -I
flag followed by the path/to/library_folder
allows you to specify the location of the required library folder. The -r
flag followed by library_require_name
instructs the Ruby interpreter to require the specified library before executing the binary. Finally, path/to/bin_folder/bin_name
represents the path to the Ruby binary you want to execute.
Example Output:
Executing the command ruby -I lib -r my_library app.rb
will execute the app.rb
binary inside the bin
folder, requiring the my_library
located in the lib
folder.
Use Case 6: Show the Version of Ruby
To show the version of Ruby you are using, we use the following command:
ruby -v
Motivation: Knowing the version of Ruby you are using is crucial for compatibility and troubleshooting purposes. Different Ruby versions may have different syntax or behavior, and some libraries may require specific versions. By checking the Ruby version, you can ensure that your code runs on the intended version.
Explanation: The -v
flag stands for “version” and simply outputs the version of Ruby you have installed.
Example Output:
Executing the command ruby -v
will output something like:
ruby 3.0.2p123 (2021-07-07 revision 12345) [x86_64-linux]
Conclusion
In this article, we have explored various use cases of the ruby
command. From executing Ruby scripts to starting a local server and checking for syntax errors, the ruby
command is a versatile tool for working with Ruby code from the command-line. Understanding these different use cases allows you to leverage the power of Ruby for your projects and tasks.
Remember to consult the official Ruby documentation for more details on the ruby
command and its various options. Happy Ruby programming!