0

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?

Nate Rubin
  • 21
  • 4
  • Why do you want to `chruby` within a Ruby script? Even if you `chruby` inside, the outer Ruby version will not change. – Franklin Yu Oct 05 '18 at 03:56
  • @FranklinYu I want to set the system wide ruby to a new version via chruby. I don't need the ruby version of the script itself to change. Does that answer your question? – Nate Rubin Oct 05 '18 at 13:23
  • So even after this Ruby script exits, you want the default Ruby version for new shell sessions to be the new Ruby? – Franklin Yu Oct 05 '18 at 13:52
  • Correct. OSX comes with ruby built in. However, it's frequently not up to date. With chruby I can easily get the most up to date ruby version, and even use different versions in different directories. – Nate Rubin Oct 05 '18 at 20:17
  • AFAIK this is not what `chruby` does. For example, if you want to change the default Ruby to 2.5.1, add `chruby 2.5.1` to your user profile. Running this command in a process will only change the Ruby version in *that process* (and sub-processes of course). – Franklin Yu Oct 07 '18 at 00:04

1 Answers1

0

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
Nate Rubin
  • 21
  • 4