gpt4 book ai didi

ruby - 在本地扩展 Faker gem 未初始化常量 Faker::xxx

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:04 29 4
gpt4 key购买 nike

我想扩展 faker gem for rails 以生成其他随机日期(在我的例子中是电脑游戏名称)

#lib/extended_faker.rb
require 'faker'
require 'extended_faker/game'

#lib/extended_faker/game.rb
Module Faker
class Game < Faker::Base
class << self
def name
fetch('game.name')
end
end
end
end

#config/locals/faker_en.yml
en:
faker:
game:
name: ["a", "b", "c"]

#config/application.rb
...
config.autoload_paths += Dir["#{config.root}/lib/**/"]
...

然后当我在 Rails 控制台中运行它时,我得到以下信息

Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
LoadError: Expected /home/enermis/School/Projects/IG/test/lib/extended_faker/game.rb to define Game
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

当我将 lib/extended_faker/game.rb 文件更改为这个时

require 'faker'
require 'extended_faker/game'
include 'extended_faker/item'
include 'extended_faker/team'

我在控制台中出现奇怪的行为

Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
=> "b"
1.9.3p194 :002 > Faker::Game.name
NameError: uninitialized constant Faker::Game
from (irb):2
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我不明白的奇怪事情是,我第一次运行生成器时,我得到了一个有效结果,但第二次我得到了一个错误...

我错过了什么?

最佳答案

你应该把它放进去
lib/faker/game.rb
从 lib 目录开始,rails 约定是最外层的模块名称 -> in 是文件夹名称。然后实际的类/模块名称是文件名,下划线为驼峰式。
另一个例子

module Foo
module Bar
class BazParty
def self.hello
puts "hello"
end
end
end
end

将进入 lib/foo/bar/baz_party.rb

关于ruby - 在本地扩展 Faker gem 未初始化常量 Faker::xxx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14221223/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com