How to run a ruby script within bundler context?

后端 未结 3 1661
深忆病人
深忆病人 2021-02-01 12:04

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

相关标签:
3条回答
  • 2021-02-01 12:45

    You can just make it a script - add

    #!/usr/bin/env ruby
    

    to the start of the file, and make it executable. Then bundle exec foo.rb will work as expected.

    (This is on unix or OSX - not sure about Windows)

    See http://bundler.io/v1.15/man/bundle-exec.1.html#Loading

    Also see https://coderwall.com/p/kfyzcw/execute-ruby-scripts-directly-without-bundler-exec for how to run ruby scripts with bundled dependencies, without needing bundle exec

    0 讨论(0)
  • 2021-02-01 12:53

    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
    
    0 讨论(0)
  • 2021-02-01 13:00

    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.

    0 讨论(0)
提交回复
热议问题