Ruby on rails Rails在shell中执行的文件的精华必须在Gemfile中
如果我从rails运行一个脚本,而该脚本需要一个gem,但在Gemfile中找不到它,那么它就不起作用,但是执行的脚本与rails没有任何关系,它的行为应该与执行其他任何东西(例如ls)时的行为相同 示例:/tmp/fichero.rbRuby on rails Rails在shell中执行的文件的精华必须在Gemfile中,ruby-on-rails,rubygems,Ruby On Rails,Rubygems,如果我从rails运行一个脚本,而该脚本需要一个gem,但在Gemfile中找不到它,那么它就不起作用,但是执行的脚本与rails没有任何关系,它的行为应该与执行其他任何东西(例如ls)时的行为相同 示例:/tmp/fichero.rb #! /usr/bin/env ruby #encoding: utf-8 require "rubygems" require 'choice' require 'fileutils' ...
#! /usr/bin/env ruby
#encoding: utf-8
require "rubygems"
require 'choice'
require 'fileutils'
...
...
Gemfile
...
...
#gem 'choice'
...
...
控制器:
stmt = "ruby /tmp/fichero.rb -p hola"
stdout, stderr, status = Open3.capture3(stmt)
或
两者:
`require':无法加载此类文件--选项(LoadError)
如果我更改文件:
...
...
gem 'choice'
...
...
它能用,但我不想把这个宝石放在我的档案里
提前感谢
任何打开子shell(如system、backticks或%x{})的Ruby代码都将自动使用当前的Bundler环境。如果需要使用不属于当前捆绑包的Ruby命令,请使用带有块的with_clean_env方法。在块内创建的任何子shell都将在激活绑定器之前提供存在的环境。例如,自制命令运行Ruby,但在捆绑包中不起作用:
Bundler.with_clean_env do
`brew install wget`
end
Bundler.with_clean_env do
`brew install wget`
end