rubyshellchruby

How to call chruby from a ruby script


I'm writing a ruby script to bootstrap a new macbook computer. I installed chruby and ruby-install via homebrew. If I call chruby from the command line it works.

$ chruby
* ruby-2.4.1

But if I call it from a ruby script like so.

def failing_function
  `chruby`
end

failing_function

I get this error

 No such file or directory - chruby (Errno::ENOENT)

As a test I tried this

def successful_function
    `ruby-install`
end

successful_function

And I get the same output for ruby-install in the script as I do the command line.

Anyone have any ideas of what I'm doing wrong?


Solution

  • After looking deeper into this, turns out that chruby is not a proper binary but instead is a bash function that's added to the environment by calling source /usr/local/share/chruby/chruby.sh. I ran a quick test by running echo $SHELL in a ruby console and it did show that it was using my zsh shell, so not sure why the full config was not loaded. In any case, I got it to work by doing this

    def successful_function
      `source /usr/local/share/chruby/chruby.sh && chruby`
    end
    
    successful_function