Ruby MongoClient:从irb初始化成功,从脚本初始化失败

时间:2013-08-19 00:20:55

标签: ruby mongodb

我正在尝试从脚本初始化MongoClient但我失败了。 我简单的做法是:

require 'mongo'
include Mongo
client = MongoClient.new("localhost", 27017)

并且失败并出现以下错误:

/var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/cursor.rb:43:in `initialize': undefined method `connection' for nil:NilClass (NoMethodError)
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/db.rb:564:in `new'
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/db.rb:564:in `command'
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:591:in `block in check_is_master'
    from /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:590:in `check_is_master'
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:474:in `connect'
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:698:in `setup'
    from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:155:in `initialize'
    from ./test.rb:52:in `new'
    from ./test.rb:52:in `<main>'

但是,当我从irb尝试完全相同的东西时,它可以工作。

Ruby版本是:

  

ruby​​ 1.9.3p194(2012-04-20修订版35410)[x86_64-linux]

irb是:

  

/usr/bin/irb1.9.1 --version   irb 0.9.6(09/06/30)

我也尝试过'rubygems',但据我所知,Ruby 1.9并不需要它。 我忘记了什么吗?

1 个答案:

答案 0 :(得分:0)

通过重新安装ruby解决了此问题。我从未发现问题是什么以及导致问题的原因。

感谢大家的帮助。