Using RVM via bash script - ruby

I wanted to write a small bash script to automatically setup the correct ruby version and gemset for a new project. For testing I have rvm installed with ruby 2.3.1 and two gemsets: foo and bar.
Even a simple test like this doesn't return what expected:
$ rvm current
ruby-2.3.1#foo
$ ./script
Using ruby-2.3.1 with gemset bar
Using /(...)/.rvm/gems/ruby-2.3.1 with gemset bar
ruby-2.3.1#bar
$ rvm current
ruby-2.3.1#foo
Contents of the script:
#!/bin/bash --login
rvm gemset use bar
rvm use 2.3.1#bar
rvm current
Other operations like installing rubies or creating gemsets seem to work, I just can't get use to modify the current version used by the script caller. Any ideas?

The answer was obviously simple, I should have called the script using
. ./script

Related

ruby: aliased to bundled_ruby

When I type in which ruby it always returns ruby: aliased to bundled_ruby
When I try which bundled_ruby it returns:
bundled_ruby () {
_run-with-bundler ruby $#
}
Then I try which _run-with-bundler and it returns:
_run-with-bundler () {
if _bundler-installed && _within-bundled-project
then
bundle exec $#
else
$#
fi
}
Where did all this come from and how do I get my which ruby to work again?
I am using OSX (v10.8.2), brew, rbenv, ruby-build, zsh
It can come from the bundler plugin of oh-my-zsh.
It uses magic to avoid typing bundle exec before the following commands: annotate berks cap capify cucumber foodcritic foreman guard jekyll kitchen knife middleman nanoc rackup rainbows rake rspec ruby shotgun spec spin spork strainer tailor taps thin thor unicorn unicorn_rails puma
You can disable this magic for some commands by defining the UNBUNDLED_COMMANDS environment variable in your ~/.zshrc before loading oh-my-zsh plugins (See oh-my-zsh pull request #2195).
export UNBUNDLED_COMMANDS=ruby
# for a list of commands
export UNBUNDLED_COMMANDS=(ruby irb rake)
Or you can simply remove the bundler plugin from you oh-my-zsh plugins.
This is part of rbenv "magic" to handle several ruby versions with several gem versions. Have a look at ~/.rbenv/shims/ruby, IIRC the script is defined there.
If you want to disable this, just unalias ruby: this deletes the generated alias.

How do I use 'rvm use' with puppet?

I'm trying to use Puppet to set up RVM on a variety of systems. Everything works fine until I try to specify which Ruby to use.
Running rvm use 1.9.3 with a Puppet exec yields an error, because 'rvm is not a function', since Puppet's exec forces all commands to be fully qualified.
How would I use Puppet to set the system Ruby through RVM? Is this even possible?
When you install rvm you need to source rvm.sh in order to get it working right away. The exact path to this file is usually disclosed in the installation messages.
You are getting good error message, it tells you that RVM can not be used interactively. This means even if RVM ignored the problem and set current ruby it would make no sense because running RVM as binary is a separate execution of shell which will not be able to set parent process (the shell / puppet) environment. To be able to set environment RVM has to be loaded as function in shell so it can change environment of current process.
So there are few ways to make it work:
subshell with multiple commands:
bash -c "source ~/.rvm/scripts/rvm ; rvm ..."
RVM set operation:
~/.rvm/bin/rvm {ruby-name} do {command}...
Some operations do not require above tricks (like setting default ruby):
~/.rvm/bin/rvm alias create default {ruby-name}
An extra explanation - default ruby is not a system ruby, it is a ruby that will be loaded when you source RVM, if you aim for availability of ruby in multiple places use alias and wrappers:
rvm alias create {my_app} {ruby-version}
rvm wrapp {ruby-version} --no-links --all
PATH=~/.rvm/environments/{my_app}:$PATH
This will create:
an alias - so it is easy to reference application ruby and no changes are needed in scripts to change ruby - just update alias
create wrappers for all gems installed in that ruby - includes wrappers for ruby and gem commands
add the PATH=... on top of any script that should work with ruby for your application.

rake sometimes won't work until "rvm use"

I've got Ruby, Rails, Rake and friends all working pretty well using RVM on OSX, but when I start a new shell (new tab in Terminal.app) rake tasks seem to "lose" their environment:
$ rake my_module:my_task
Could not find uglifier-1.2.7 in any of the sources
Run `bundle install` to install missing gems.
If I issue "rvm use" (which, I believe merely prints the current ruby, but does not alter anything) then it works fine:
$ rvm use
Using /Users/username/.rvm/gems/ruby-1.9.3-p194
$ rake my_module:my_task # works fine now!
It seems to happen when a new shell is started. I have rvm installed as a single-user installation, with the following at the bottom of my .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
I've verified that my .bash_profile is in fact executing by putting some echo statements after that line; I see the echo on the command line when I open a new tab, so it seems to be working. Any idea why rake doesn't work without "rvm use"?
Try setting rvm ruby to default ruby
$ rvm use ruby-1.9.3-p194 --default
This should use the rvm ruby-1.9.3-p194 every time as default.

rvm doesn't notify when using a new gemset

I'm using rvmrc files per project. When I cd into my project the terminal/rvm does not indicate that I'm using a new gemset. How do I set it up?
In case it helps: I'm using oh-my-zsh.
it might depend on your project files, basically rvm will not print information about using when the use keyword is omitted:
rvm 1.9.3 # will be quiet
rvm use 1.9.3 # will print: Using...
the same result will be when you put those in .rvmrc, also generating new .rvmrc with --rvmrc will behave the same way:
rvm 1.9.3 --rvmrc # `cd` will be quiet
rvm use 1.9.3 --rvmrc # `cd` will print: Using...

RVM: Create a gemset for whatever ruby is currently running

I want to use per project gemsets. But I don't want to specify the ruby version.
Something like:
#.rvmrc
rvm --create use "#project"
But this give me the following error:
error: Unknown ruby interpreter string component: 'ruby 1.9.2 p136'
info: Now using system ruby.
Is there a way to do what i want?
I'm not sure if that's the case but perhaps
rvm --create use doesn't support params without ruby version.
#.rvmrc
rvm gemset create "project"
rvm gemset use "project"
I didn't check this, just saw at:
http://rvm.beginrescueend.com/gemsets/creating/

Resources