Skip to content

Implementing an 'after use' hook

Stefan Wrobel edited this page Nov 30, 2016 · 4 revisions

I needed an rvm-style 'after-use' hook to tweak the path after switching rubies. Here's how I did it:

# source the following code in your .bashrc before your chruby setup
# you may find it handy to use for other applications 
# h/t [http://mivok.net/2009/09/20/bashfunctionoverrist.html]

save_function()
{     local ORIG_FUNC=$(declare -f )
      local NEWNAME_FUNC="${ORIG_FUNC#}"   
      eval "$NEWNAME_FUNC"
}

# like source ~/.mybash/_functions
# then do your chruby setup..

chruby_setup_goes_here...

# then:

save_function chruby old_chruby
source chruby_override_w_after_use_hook

# like: source ~/.mybash/_chruby_override
# the new function looks like:

function chruby () {                                                                                                           
    old_chruby $*                                                                                                              
    your_after_use_hook_code_here..                              
}

ZSH 5.x

The following worked for me in ZSH 5.x:

source /usr/local/opt/chruby/share/chruby/chruby.sh
source /usr/local/opt/chruby/share/chruby/auto.sh

save_function()
{
  local ORIG_FUNC="$(declare -f $1)"
  local NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
  eval "$NEWNAME_FUNC"
}

save_function chruby old_chruby

chruby() {
  old_chruby $*
  PATH=./bin:$PATH
}

chruby ruby-2.0
Clone this wiki locally