Skip to content

Implementing an 'after use' hook

zhando edited this page Dec 14, 2012 · 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..                              
}
Clone this wiki locally