Overview
The `ruby` command is an interpreter that executes programs written in the Ruby language. It can be used to directly execute script files or run short code snippets from the command line. Additionally, it provides utility functions such as checking the Ruby version and validating syntax.
Key Features
- Execute Ruby script files
- Execute Ruby code directly from the command line
- Syntax checking and debugging support
- Usable on various operating systems
Key Options
These are the main options that can be usefully employed when running the Ruby interpreter.
Execution and Information
Environment Configuration
Generated command:
Try combining the commands.
Description:
`ruby` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Examples demonstrating various scenarios using the Ruby command.
Execute Ruby Script
ruby hello.rb
Executes the `hello.rb` file. Assumes the file content is `puts "Hello, Ruby!"`.
Check Ruby Version
ruby --version
Checks the version of the currently installed Ruby interpreter.
Execute Code from Command Line
ruby -e 'puts "Current time: #{Time.now}"'
Executes Ruby code directly using the `-e` option.
Check Script Syntax
ruby -c my_app.rb
Checks only for syntax errors in the `my_app.rb` file without executing it.
Execute Script After Loading Library
ruby -r json my_script_using_json.rb
Executes the script after loading the JSON library. The `JSON` module will be available in the script.
Installation
Ruby is often not included by default in most Linux distributions, or an older version might be installed. This section guides you on how to install the latest version or manage multiple versions.
Installation using Package Manager (Recommended)
Most Linux distributions allow easy installation of Ruby through their package managers. The `ruby-full` package installs Ruby along with development tools.
Debian/Ubuntu
sudo apt update
sudo apt install ruby-full
Installs Ruby using the APT package manager.
CentOS/RHEL/Fedora
sudo dnf install ruby
Installs Ruby using the DNF or YUM package manager.
Installation using Version Management Tools (Advanced)
For using multiple Ruby versions concurrently or installing a specific version, it is recommended to use Ruby version management tools like `rbenv` or `rvm`. These tools allow you to set up independent Ruby environments without conflicts with the system Ruby.
Key Version Management Tools
- `rbenv`: Lightweight and flexible, allows setting Ruby versions globally or per project.
- `rvm` (Ruby Version Manager): Offers more features beyond version management, including gemset management.
Tips & Notes
Useful tips and points to note when developing and using Ruby.
Utilizing the Interactive Interpreter (IRB)
For testing simple Ruby code or for learning, using the `irb` command is very convenient. `irb` is Ruby's interactive shell.
- Command: irb
Gem Package Management
Ruby libraries (Gems) are installed, updated, and managed using the `gem` command. You can easily add necessary functionalities.
- Install Gem: gem install [gem_name]
- List Gems: gem list
Using Bundler
It is common practice to use `Bundler` for managing dependencies in Ruby projects. It installs and manages Gems defined in the `Gemfile`.
- Install Bundler: gem install bundler
- Install Dependencies: bundle install
Script Execution Permissions
To execute Ruby scripts directly like `./script.rb` instead of `ruby script.rb`, you need to grant execute permissions to the script file (chmod +x script.rb) and add a shebang line at the beginning (`#!/usr/bin/env ruby` or `#!/usr/bin/ruby`).