Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a ruby script within bundler context?

Tags:

ruby

bundle

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.

like image 541
Michiel de Mare Avatar asked Oct 31 '11 19:10

Michiel de Mare


People also ask

How do I run a Ruby script from the shell?

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.

How do I make a Ruby script executable?

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.

What is bundle exec rspec?

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 .


2 Answers

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
like image 158
Dave Newton Avatar answered Oct 08 '22 06:10

Dave Newton


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.

like image 6
Dorian Avatar answered Oct 08 '22 06:10

Dorian