Skip to content

Instantly share code, notes, and snippets.

@wayneeseguin
Forked from jimweirich/gist:343526
Created March 25, 2010 13:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wayneeseguin/343545 to your computer and use it in GitHub Desktop.
Save wayneeseguin/343545 to your computer and use it in GitHub Desktop.
# Switch to Ruby 1.8.7
rvm use 1.8.7
# Print out the ruby version
ruby -v
# But the output of this is:
#
# $ ./tryrvm
# <i> Now using ruby 1.8.7 p249 </i>
# ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-darwin9.8.0]
#
# Why?
#!/usr/bin/env bash
# rvm essentially has 2 methods of invocation:
#
# 1. script, ~/.rvm/bin/rvm is a *script* to execute rvm commands outside of the env
# environment. This method is an 'external script' and thus RVM would not be able
# to manage the parent shell's environment.
#
# 2. function, sourcing scripts/rvm loads rvm() as a shell *function* into a
# specific environment. Doing this will allow RVM to manage the shell environment.
#
# If the above does not make sense to you (I am assuming that several people will read
# this) please hop on IRC and let's chat.
#
# As an additional note, rvm has two verbosity modes
#
# $ rvm use X # This will output 'now using X' and is
# # and is not necessairly meant for scripting.
#
# $ rvm X # This will use X but not output anything
# # and is meant for use with scripting.
#
source $HOME/.bashrc # if rvm is sourced there.
# alternatively you can: source $HOME/.rvm/scripts/rvm
# Select 1.8.7
rvm use 1.8.7
# Output the current ruby version
ruby -v
# Display the information about the current environment, within this script.
rvm info
# Display the current rubygems directory for environment, within the script.
rvm gemdir
# List installed rubies
rvm list
@pboling
Copy link

pboling commented Dec 4, 2013

Hi @wayneeqeguin, question about using rvm in scripts. I am building some simple scripts to setup a stack (I know you have a tool for this and hope to get to the point where I can use yours soon). I am using rvm because the stack runs several things in several different rubies. In getting each ruby environment setup I need to do things like script the installation of some gems. Those commands are done in sub-shells of the script being run, and always pickup my default Ruby I think, instead of the ruby I tell it to use on the line just prior.

I have read about how to use rvm in scripts, and am sourcing rvm properly I think. I am just not sure why rvm use 1.8.7 becomes 2.0.0 on the next line when I do a gem install in a sub shell.

I am using bsfl (my own heavily modified fork) to run the commands and get nice feedback printed back to screen on success/fail. I don't think it is doing any magic that would make it different from just running

$(gem install htmlentities)

in a script.

This is the underlying function that is running the gem install commands:

cmd () {
    COMMAND="$@"

    msg "RUN -> $COMMAND"

    RESULT=$("$@" 2>&1)
    ERROR=$?

    check_status " ${COMMAND}" "${ERROR}"

    die_if_false $ERROR "$RESULT"

    return $ERROR
}

@pboling
Copy link

pboling commented Dec 4, 2013

OK I tried your example and modified it a bit, and understand why it doesn't work.

# First sourced my bsfl and bsfl_steps scripts, then:
source $HOME/.rvm/scripts/rvm

# Select 1.8.7
rvm use 1.8.7

# Output the current ruby version
try -d ruby -v # => 1.8.7

# try to switch rubies
try -cmd rvm use 2.0.0

# The ruby in this shell hasn't changed
ruby -v # => 1.8.7

try -cmd rvm use 2.0.0

# There is no way to get into the context where the Ruby is set to 2.0.0,
# because it was done in a subshell whose context is now gone.
ruby -v # => 1.8.7
try -cmd ruby -v # => 1.8.7
try -d ruby -v # => 1.8.7

# Now doing it in the same process:
rvm use 2.0.0

# Since we switched to ruby 2.0.0 in this context, the subshells will have it as well.
ruby -v # => 2.0.0
try -cmd echo `ruby -v` # => 2.0.0
try -d echo `ruby -v` # => 2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment