You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Install chruby and ruby-build with git. I keep their repo at ~/.chrb, their installation inside ~/local and my rubies at ~/.chrb/rubies.
Copy this in your ~/.mkshrc file
# chruby + ruby-build
chrb() {
source ~/local/share/chruby/chruby.sh
unset RUBIES
RUBIES=$(ls -d ~/.chrb/rubies/* 2> /dev/null)
CHRUBY_SOURCE_DIR=~/.chrb/chruby/
RUBY_BUILD_SOURCE_DIR=~/.chrb/ruby-build
case "$1" in
-h|--help)
chruby -h
echo
ruby-build -h
;;
-v|--version)
chruby -v
echo
ruby-build 2>&1 | head -1
;;
-l|--list-definitions)
# Get the current names of rubies to get a more usefull output
# Installed ruby will be prefixed with "[*]", no installed ruby with "[ ]"
_INSTALLED_RUBIES="$(ls ~/.chrb/rubies | sed 's:$:\$:' | xargs | sed 's: :\\|:g')"
if [ -n "$_INSTALLED_RUBIES" ]; then
ruby-build --definitions | \
sed "s:\($_INSTALLED_RUBIES\):[*] \1:" | \
sed '/\[/!s/^/[ ] /g' | COLUMNS=100 column
else
ruby-build --definitions | sed 's/^/[ ] /' | COLUMNS=100 column
fi
;;
-i|--install-definition)
(
# Temporary directory to build ruby
export TMPDIR=/tmp/rubies
# Use the same directory to keep the source code
export RUBY_BUILD_CACHE_PATH=$TMPDIR
# For a CPU with 4 cores
export MAKE_OPTS="-j 5"
# Rubies will be installed inside ~/.chrb/rubies/
ruby-build -v $2 ~/.chrb/rubies/$2
)
;;
-u|--update-definitions)
(
# Updates ruby-build and chruby from git because ... YOLO
cd $RUBY_BUILD_SOURCE_DIR
echo "Updating " $(pwd)
git pull
# Delete all ruby-build definitions
rm -rf ~/local/share/ruby-build/*
PREFIX=~/local ./install.sh
cd $CHRUBY_SOURCE_DIR
echo "Updating " $(pwd)
git pull
PREFIX=~/local make install
)
;;
-d|--delete-definition)
(
# Actually i just move it to other place... not so YOLO
mkdir -p /tmp/rubies
mv ~/.rubies/$2 /tmp/rubies/$2
)
;;
-c|--changelog-definitions)
(
# To see what i've just updated
_NUMBER_OF_COMMITS=10
cd $RUBY_BUILD_SOURCE_DIR
echo "ruby-build: last $_NUMBER_OF_COMMITS commits:"
git log --oneline -$_NUMBER_OF_COMMITS --parents
echo
cd $CHRUBY_SOURCE_DIR
echo "chruby last $_NUMBER_OF_COMMITS commits:"
git log --oneline -$_NUMBER_OF_COMMITS --parents
)
;;
*)
if [ -z $@ ]; then
# If there is a .ruby-version file in the current working
# directory check if there is a ruby installed with *exactly* the
# same name. This will disable the "Fuzzy matching of Rubies by name."
# feature in chruby... that's the way, aha, i like it.
_CWD=$(pwd)
_RUBY_VERSION_FILE="$_CWD/.ruby-version"
_SELECTED_RUBY=""
if [ -f "$_RUBY_VERSION_FILE" ]; then
echo "Detected $_RUBY_VERSION_FILE"
_RUBY_VERSION=$(head -1 "$_RUBY_VERSION_FILE" | cut -d ' ' -f 1)
for ruby in $(ls ~/.chrb/rubies); do
if [ "$_RUBY_VERSION" == "$ruby" ]; then
echo "Recognized $ruby"
_SELECTED_RUBY="$ruby";
break
fi
done
if [ -z "$_SELECTED_RUBY" ]; then
echo "$_RUBY_VERSION is not installed at ~/.chrb/rubies"
echo "Installed rubies:"
fi
fi
chruby $_SELECTED_RUBY
else
chruby $@
fi
;;
esac
}