Ubuntu – How to get “ruby” to do what “ruby1.9.1” does

command lineruby

I want to start off by saying… I really don't need to be using 1.9.2. I understand that you don't ever, ever use Python 3.2, so if the common advice is that I should use 1.8, I'll do that. But don't tell me to do that because it's easier.

Whatever version I should be using, though, the question is still relevant: What's a good way to make one command do another command?

(I suppose I could write a c program to launch ruby1.9.1, call the executable ruby, and put it in my bin, but this seems like less than a good idea)

Best Answer

  • Well to start with, you wouldn't need to write a C application - a simple bash ditty would do you fine.

    I think the cleanest possible solution would involve the update-alternatives system. This is how, for example, Ubuntu manages to keep various Java virtual machines separated while installed alongside each other. The problem is you need to set it up yourself.

    I just came across a mailing-list post that does seem to do most of the heavy lifting for you. You might need to change the version numbers slightly but other than that, you should get the idea.

    For posterity (in case Google nixes the URL or the thread), I'll copy in the business end now but I take no credit for writing it.

    If any of you are using Ubuntu this is a pretty nice way to manage multiple 
    ruby interpreters. 
    
    It has the advantage of managing the manpages, ri, and irb as "slaves", so 
    they change when a new interpreter is selected. 
    
    here's the code: 
    
    # become root 
    su 
    
    # make sure the packages are installed for 1.8 & 1.9 
    aptitude install -s  ~n^ruby1.[89]$ ~n^irb1.[89]$ ~n^ri1.[89] 
    
    # install ruby1.8 & friends with priority 500 
    # so this will be the default "auto" choice 
    update-alternatives --install /usr/bin/ruby ruby /usr/bin/ruby1.8 500 \ 
                        --slave   /usr/share/man/man1/ruby.1.gz ruby.1.gz \ 
                                      /usr/share/man/man1/ruby.1.8.gz \ 
                        --slave   /usr/bin/ri ri /usr/bin/ri1.8 \ 
                        --slave   /usr/bin/irb irb /usr/bin/irb1.8 
    
    # install ruby1.9 & friends with priority 400 
    update-alternatives --install /usr/bin/ruby ruby /usr/bin/ruby1.9 400 \ 
                        --slave   /usr/share/man/man1/ruby.1.gz ruby.1.gz \ 
                                       /usr/share/man/man1/ruby.1.9.gz \ 
                        --slave   /usr/bin/ri ri /usr/bin/ri1.9 \ 
                        --slave   /usr/bin/irb irb /usr/bin/irb1.9 
    
    # choose your interpreter 
    # changes symlinks for /usr/bin/ruby , 
    # /usr/bin/irb, /usr/bin/ri and man (1) ruby 
    update-alternatives --config ruby 
    
    for those with additional interpreters in say /usr/local/bin, other Debian 
    variants, or managing other tools, vary as required. 
    
    % man update-alternatives 
    
    hope wrapping didn't mangle it too much, and that someone finds this useful 
    ... 
    -- 
    cheers, 
    David Lee