我已经使用捆绑程序“ bundle install --path vendor / bundle”在我的项目中安装我的gem mysql2。
我的Gemfile就是这样
source 'https://rubygems.org'
gem 'mysql2', '~> 0.3.20'
现在,我想在我的ruby脚本中要求此gem,但它不起作用。始终显示“没有此类文件mysql2”。
我的代码就是这样
require 'json'
load_paths = Dir.pwd + "/vendor/bundle/ruby/2.5.0/gems/"
$LOAD_PATH.unshift(*load_paths)
require 'mysql2'
def lambda_handler(event:, context:)
# # TODO implement
@db_host = "host"
@db_user = "user"
@db_pass = "pass"
@db_name = "db"
client = Mysql2::Client.new(:host => @db_host, :username => @db_user, :password => @db_pass, :database => @db_name)
@cdr_result = client.query("SELECT count(*) from table_name")
puts @cdr_result
{ statusCode: 200, body: JSON.generate('Hello from Lambda!') }
end
这样的错误即将来临
Traceback (most recent call last):
2: from replaceFile.rb:4:in `<main>'
1: from /home/vagrant/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/home/vagrant/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- mysql2 (LoadError)
我正在使用ruby版本= ruby 2.5.0p0(2017-12-25修订版61468)[x86_64-linux]
答案 0 :(得分:0)
使用选项还不够:您需要配置bundler以便它记住它。 documentation说的是:
Flags passed to bundle install or the Bundler runtime, such as --path foo or
--without production, are not remembered between commands. If these options
must be remembered, they must be set using bundle config (e.g., bundle config
path foo).
在执行此操作之前,捆绑程序将忽略您所做的本地安装,并且仍然尝试像通常那样(即不在vendor/bundle
路径中)获取gem。因此失败