How To Check Ruby Version

Ruby is a highly popular and versatile programming language used for web development, automation, data analysis, and more. It’s essential to know the version of Ruby you are using, as different versions might have different features, syntax, and compatibility with various libraries and gems.

In this blog post, we’ll show you how to check the version of Ruby installed on your system in just a few easy steps. This is useful if you need to know which version you’re using, whether you’re a beginner learning Ruby or an experienced developer working on a project.

Checking Ruby Version via Command Line

The simplest way to check your Ruby version is by running a single command in your terminal or command prompt. The command for this is ruby -v or ruby –version. Both commands will give you the same result.

Open your terminal or command prompt and type in the following command:

    ruby -v
    

After running the command, you should see output similar to this:

    ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux]
    

The output tells you the version of Ruby installed on your system (in this case, it’s 2.7.4), as well as some additional details about the build and platform.

Checking Ruby Version Within a Ruby Script

If you want to check the Ruby version within a Ruby script, you can use the RUBY_VERSION constant. This constant is a global variable that returns a string representing the current version of Ruby.

To use RUBY_VERSION, simply include it in your Ruby script like this:

    puts "This script is running on Ruby #{RUBY_VERSION}"
    

When you run your script, it should output something like this:

    This script is running on Ruby 2.7.4
    

This method is particularly useful if you want to ensure that your script is compatible with specific Ruby versions or if you want to display the version information as part of your application’s output.

Conclusion

Knowing your Ruby version is important for ensuring compatibility and functionality with various gems, libraries, and applications. Now you know how to easily check your Ruby version either through the command line or within a Ruby script. Happy coding!