
A Useful Command to Check If a Gem Is Loaded in Your Bundler Environment
Whether you’re debugging a dependency issue, checking for an optional gem, or just curious about what’s loaded in your Ruby app, there’s a handy command that can help:
puts Gem.loaded_specs.keys.grep(/your_pattern_here/)
In this article, we will discuss what this command does, what exactly it’s checking for, and its limitations.
Recently while working on upgrading our FastRuby.io web application, we ran the following command, after encountering an error regarding the mutex_m
gem requirement, to confirm whether the gem was already loaded in the project.
bundle exec ruby -e "puts Gem.loaded_specs.keys.grep(/mutex_m/)”
Since it was not, nothing was printed, indicating that the gem does need to be installed to satisfy the requirement. After doing so, we ran the command again which printed the name of the gem, confirming it was now loaded.
So what does Gem.loaded_specs.keys.grep do?
Gem.loaded_specs
Gem.loaded_specs will return a hash of all gems currently loaded in the environment.
Gem.loaded_specs.keys
Calling the .keys
on this response will extract just the gem names from that hash.
aysanisayo@MacBook-Pro:~/fastruby.io|main⚡ ⇒ bundle exec ruby -e "puts Gem.loaded_specs.keys"
pathname
rake
base64
benchmark
bigdecimal
concurrent-ruby
…
Gem.loaded_specs.keys.grep(/your_pattern_here/)
You can replace /your_pattern_here/
with any regex that matches the gem name you’re looking for.
And finally, calling for example .grep(/mutex_m/)
on the list of gem names from the hash will filter that list of names to only include those matching the regular expression.
In this example, we will use /mutex_m/
:
aysanisayo@MacBook-Pro:~/fastruby.io|main⚡ ⇒ bundle exec ruby -e "puts Gem.loaded_specs.keys.grep(/mutex_m/)"
mutex_m
To summarize, this command will print the name of any gem currently loaded in your project that matches the name pattern passed in as an argument.
What Is It Actually Checking?
This command is checking whether a gem matching the regex argument is currently loaded in the Bundler environment of the project.
You might use this:
-
To check if a gem like
mutex_m
is present (and thus potentially influencing threading behavior). -
As part of a script to confirm certain dependencies are in use.
-
During debugging, to see if a gem has been pulled into the environment unexpectedly.
The Limitations
This command is handy — but it does come with some limitations. Here’s why:
-
It Only Shows Loaded Gems: It won’t tell you if the gem is installed but not loaded. If the gem is in your system but isn’t required in your app or hasn’t been pulled in by another gem, this command won’t detect it. To check for installed gems (whether loaded or not), use:
gem list | grep mutex_m
-
It Matches on the Gem Name Only: This command filters by gem name using a regular expression. It won’t detect gems that use
mutex_m
internally but aren’t named after it. -
It Doesn’t Tell You How the Gem Is Used: Even if a gem matching
mutex_m
is present, this command doesn’t give you insight into how it’s being used. Is it actually running any mutex-related logic? That’s something you’d need to dig into in the gem’s code or in runtime behavior.
When to Use It
This command is useful when:
-
You’re troubleshooting: “Is this gem even loaded right now?”
-
You’re in a new codebase and want to quickly scan what’s active
-
You’re writing a script or Rake task that needs to check for optional gems
-
You want to compare environments (e.g., dev vs. prod)
Conclusion
Running the command bundle exec ruby -e "puts Gem.loaded_specs.keys.grep(/your_pattern/)"
is a fast and flexible way to search for loaded gems by name using regular expressions. It’s a great tool to have in your Ruby debugging or scripting toolkit — just keep in mind that it only shows what’s active in the current process, not everything installed.
Whether you’re looking for mutex_m
, debug
, rails
, or any other gem, just drop in the regex you want, and get quick feedback.
Need help with your Rails application? Whether it’s general maintenance, upgrading or feature development, talk to us!