How do I determine the location of my ruby gems?
you can try
gem which rails
to fetch location for particular gem, or
echo $GEM_HOME
to fetch home dir of your gems
ENV['GEM_HOME']
–
Swampy ENV['GEM_HOME']
return nil
in irb
–
Multifaceted rbenv
, my gems are stored in ~/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems
. –
Dorindadorine gem environment
...should give you all the info you need.
With budler you can do:
bundle info <gem>
Result:
* pg (1.4.5)
Summary: Pg is the Ruby interface to the PostgreSQL RDBMS
Homepage: https://github.com/ged/ruby-pg
Path: /home/user/.rvm/gems/ruby-3.1.3/gems/pg-1.4.5
bundle info
instead of bundle show
–
Weatherworn The trivial solution would be:
gem environment
However, it shows a large output with many informations that we do not want to see, such as RUBYGEMS VERSION
, RUBY VERSION
, INSTALLATION DIRECTORY
, USER INSTALLATION DIRECTORY
, RUBY EXECUTABLE
, GIT EXECUTABLE
, EXECUTABLE DIRECTORY
, SPEC CACHE DIRECTORY
, SYSTEM CONFIGURATION DIRECTORY
, RUBYGEMS PLATFORMS
, GEM CONFIGURATION
, REMOTE SOURCES
and SHELL PATH
.
We just want the GEM PATH
!
I have a solution for this with grep
+ awk
.
gem environment | grep -A 1 "GEM PATHS:" | awk 'NR==2 { sub(/^[[:space:]]+- /, ""); print }'
Explanation 💡
gem environment
: This command is used to display information about the RubyGems environment, including details about gem installations, paths, and configurations.grep -A 1 "GEM PATHS:"
: This part of the command is used to search for the line containing "GEM PATHS:" in the output ofgem environment
and include the line that follows it (-A 1
) in the output. In other words, it extracts the line with "GEM PATHS:" and the line immediately below it.awk 'NR==2 { sub(/^[[:space:]]+- /, ""); print }'
:awk
: This is a powerful text processing tool that allows you to manipulate and process text files line by line.'NR==2'
: This is an AWK condition that specifies that the following actions should be performed only for the second line of input (the line below "GEM PATHS:").{ sub(/^[[:space:]]+- /, ""); print }
:sub(/^[[:space:]]+- /, "")
: This AWK function (sub
) is used to substitute (replace) a pattern with another pattern. In this case, it's used to remove the leading spaces and hyphen (^[[:space:]]+-
) from the line.print
: This command is used to print the modified line to the standard output.
So, in summary, the command extracts the line below "GEM PATHS:" from the gem environment
output, removes the leading spaces and hyphen from that line, and then prints the resulting file path.
© 2022 - 2024 — McMap. All rights reserved.