Gem gem install 提示安装成功 运行代码 提示 cannot load such file 是什么原因

tablecell · 2020年08月14日 · 最后由 tablecell 回复于 2020年08月17日 · 1648 次阅读
 gem install   dm-sqlite-adapter
Successfully installed dm-sqlite-adapter-1.2.0
Parsing documentation for dm-sqlite-adapter-1.2.0
Installing ri documentation for dm-sqlite-adapter-1.2.0
Done installing documentation for dm-sqlite-adapter after 0 seconds
1 gem installed

test.rb

require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'

#DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
DataMapper.setup(:default, 'sqlite:test.db')

ruby test.rb

Traceback (most recent call last):
        2: from test.rb:4:in `<main>'
        1: from  Ruby27/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
 Ruby27/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- dm-sqlite-adapter (LoadError)
...

包名和 require 的名称不一定一样的,建议多找找文档,或者直接搜索 gem 目录下相关文件,在 irb 里 require 测试一下

用 ruby2.6 可以
参考的这个例子 https://www.cnblogs.com/lizunicon/p/3604472.html 但是 id 字段用 Serial 提示 constant ::Fixnum is deprecated


class Task
    include DataMapper::Resource  
    property :id,Serial
    property :name,String
    property :completed_at,DateTime
end

DataMapper.finalize
DataMapper.auto_upgrade!

 ruby test.rb
 Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/pooling.rb:149: warning: constant ::Fixnum is deprecated
 Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/connection.rb:79: [BUG] Segmentation fault
ruby 2.6.6p146 (2020-03-31 revision 67876) [x64-mingw32]

-- Control frame information -----------------------------------------------
c:0017 p:---- s:0091 e:000090 CFUNC  :initialize
c:0016 p:0017 s:0086 e:000085 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/connection.rb:79
c:0015 p:0064 s:0080 e:000079 BLOCK   Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/pooling.rb:181 [FINISH]
c:0014 p:---- s:0077 e:000076 CFUNC  :synchronize
c:0013 p:0017 s:0073 e:000072 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/pooling.rb:176
c:0012 p:0039 s:0068 e:000067 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/pooling.rb:121
c:0011 p:0290 s:0063 e:000062 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/data_objects-0.10.17/lib/data_objects/connection.rb:68
c:0010 p:0016 s:0052 e:000051 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:251
c:0009 p:0004 s:0048 e:000047 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276
c:0008 p:0038 s:0042 e:000041 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:141
c:0007 p:0024 s:0032 e:000030 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/dm-core-1.2.1/lib/dm-core/repository.rb:162
c:0006 p:0040 s:0026 e:000025 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/dm-core-1.2.1/lib/dm-core/collection.rb:1118
c:0005 p:0004 s:0018 e:000017 METHOD  Ruby26-x64/lib/ruby/gems/2.6.0/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:275 [FINISH]
c:0004 p:---- s:0014 e:000013 CFUNC  :puts
c:0003 p:---- s:0011 e:000010 CFUNC  :puts
c:0002 p:0076 s:0006 e:000005 EVAL   test.rb:19 [FINISH]
c:0001 p:0000 s:0003 E:001530 (none) [FINISH]
3 楼 已删除

原因找到了 ruby 太新 gem 太旧 回退到 2.5 就可以了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号