来自Gemfile的未初始化常量

时间:2013-03-03 18:31:11

标签: ruby require gemfile

只是玩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吗?

Ruby 1.9.3

1 个答案:

答案 0 :(得分:0)

即使gem存在于gemfile中,您也需要它,或者在ruby文件中执行以下操作,以便所有捆绑的gem都是必需的:

Bundler.require(:default)

More infos here