只是在玩 Ruby 并有这个非常基本的例子
runner.rb
:
require "bundler/setup"
include Mongo
mongo_client = MongoClient.new("localhost", 27017)
#mongo_client.database_names # lists all database names
#mongo_client.database_info.each { |info| puts info.inspect }
db = mongo_client.db("james_safety")
coll = db.collection("safety")
puts "above"
10.times { |i| coll.insert("i" => i) }
puts "Hello World"
Gemfile
:
source :rubygems
gem 'mongo'
bundle
输出:
Using bson (1.8.2)
Using mongo (1.8.2)
Using bundler (1.2.2)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
当我尝试运行时ruby runner.rb
,我收到此错误:
/Users/Tyler/Development/Ruby/JamesSaftey/runner.rb:2:in `<top (required)>': uninitialized constant Mongo (NameError)
from -e:1:in `load'
from -e:1:in `<main>'
如果我require 'mongo'
在runner.rb
,它工作得很好(我在 Gemfile 之外安装了 gem)。
我也试过了require 'rubygems'
。没有不同。
我只是错误地配置了我的 ruby 文件以使用 Gemfile 吗?
红宝石 1.9.3