Ruby/Rails多版本环境下:which rails、rails -v等命令作用问询
Ruby/Rails Multi-Version Environment Questions Answered
Hey there! Since you're just a month into Ruby/Rails and prepping to handle multi-version setups, let's break down each of your questions clearly:
1. What does which ruby return, and does it determine the Ruby version used to run .rb files?
which rubyoutputs the full path of the Ruby interpreter that’s currently active in your shell session—this is the version that will be called when you runrubycommands without specifying an absolute path. It’s not necessarily the system’s default Ruby (unless you haven’t set up a version manager like RVM or rbenv).- When you run a
.rbfile withruby your_script.rb, yes, it will use the Ruby version pointed to by the path fromwhich ruby. If you want to use a different version, you’d need to call it directly via its absolute path (e.g.,/usr/bin/ruby your_script.rb).
2. Does ruby -v show the currently active Ruby version (matching rvm use)?
Absolutely! ruby -v displays the exact Ruby version that’s active in your current shell session, and it directly reflects the version you’ve switched to with rvm use ruby-#.#.#. For example:
- Run
rvm use ruby-3.2.2 - Then
ruby -vwill outputruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22](or your system’s equivalent) - If you haven’t run
rvm useyet, it will show RVM’s default version (set withrvm default).
3. When multiple Rails versions are installed, which one does rails -v show?
This depends on your context:
- Inside a Rails project directory:
rails -vwill read the Rails version specified in the project’sGemfileand display that version—provided you’ve runbundle installto set up the project’s dependencies. Bundle ensures your project uses the exact Rails version defined, even if other versions are installed globally. - Outside a project directory: It will show the Rails version that’s globally installed in your currently active Ruby environment (the one you installed with
gem install rails). If you’re using RVM gemsets, switching to a different gemset (withrvm gemset use [gemset-name]) will change therails -voutput to match the Rails version installed in that gemset.
内容的提问来源于stack exchange,提问作者S. Harper
相关产品推荐
相关产品推荐

