我正在尝试从脚本初始化 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 1.9.3p194(2012-04-20 修订版 35410)[x86_64-linux]
irb 是:
/usr/bin/irb1.9.1 --版本 irb 0.9.6(09/06/30)
我也尝试过要求“rubygems”,但据我所知,Ruby 1.9 不需要它。我是不是忘记了什么?