I have a Ruby script called foo.rb, and I want to run it within the context of the bundler environment. How?
bundle exec foo.rb
doesn't work, because exec expects a shell script.
Now, to run the program, simply enter the command ./test. Whether you invoke the Ruby interpreter manually with the Ruby command or run the Ruby script directly is up to you.
You can make the script executable with the following command: chmod +x hello. rb . chmod is a shell command that allows us to change the permissions for a file. The +x specifies that the script should be executable.
bundle exec changes your $PATH , or %PATH% in case of Windows. As a result, by using bundle exec rspec you're calling the RSpec's version which is specified in your Gemfile . rspec ran without Bundler executes the one in your $PATH .
Pass the script name to the ruby
command:
bundle exec ruby script_name
If you also want the Rails environment:
bundle exec rails runner script_name
For instance, I wanted to use the same version of Rubocop as my Rails app and not the latest system one, so doing this in a script:
require 'bundler'
Bundler.require
# ...
Allowed me to use my app's version of rubocop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With