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'
ました。変わりはない。
Gemfile を使用するように ruby ファイルを誤って構成しているだけですか?
ルビー1.9.3