Sarah Sarah -4 years ago 212
Ruby Question

Ruby: 'bundle exec' throws error for all shell commands

In my project, I have a Gem that contains a shell script in its bin folder, let's call the script actually executes a ruby script using Jruby command, the script is called ruby_script.rb.

I am trying to call from my project using:

bundle exec

it keeps throwing errors for all shell commands in the script. I erased all the contents of the script and added only one line "echo 'Hello'" and it is still throwing the following error

NoMethodError: undefined method
echo' for main:Object

All the files has execute permission.

I also tried to put ruby_script.rb in the bin directory where are files are executables (according to a rule in the gemspec file) and tried calling

bundle exec ruby_script.rb

I get the error "bundler: command not found: ruby_script.rb"

bundle exec ruby ruby_script.rb

I get the error "ruby: No such file or directory -- ruby_script.rb (LoadError)"

Why am I getting this error and how can I solve it? I want to be able to either run or ruby_script.rb. Right now, ruby_script.rb is not recognised and does not recognise the commands.

Answer Source

Ok, finally after two days, I was able to make it work.

bundle install installs my GEM and creates a ruby wrapper by default to sh files in the 'bin' directory. So, when I call my script using bundle exec the ruby wrapper gets called first and it calls my script using a ruby 'load' function. This load function expects a ruby file and that is why all my shell script in the file threw errors, because they were expected to be in ruby.

A workaround to this is installing my gem before 'bundle install' using the following command:

gem install --no-wrapper my-gem

Apparently this command disabled creating the ruby wrapper and I was finally able to call the script using bundle execute

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download