【问题标题】:Ruby returns uninitialized constant error when trying to include a module within a chef recipe尝试在厨师食谱中包含模块时,Ruby 返回未初始化的常量错误
【发布时间】:2018-10-16 05:20:54
【问题描述】:

我有一个 java/recipes/windows 配方,它使用一种名为 win_friendly_path 的方法,但它不起作用,因为尚未定义 win_friendly_path

win_friendly_path../windows/libraries/windows_helper.rb 中定义如下:

module Windows
  module Helper
def win_friendly_path(path)
      path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR || '\\') if path
    end

我已经在 java (./) 食谱中设置了 berksfilemetadata.rb 以依赖于 windows 食谱。

我不确定如何包含此模块,所以现在我尝试在 java/cookbook/windows 配方中使用 include WindowsHelper 并收到此错误:

uninitialized constant #<Class:#<Chef::Recipe:0x00000000029a2188>>::WindowsHelper

我已经尝试了几种变体,现在感觉我花了太多时间来解决问题,所以感谢任何帮助。

更新:将这一行 ::Chef::Resource.send(:include, Windows::Helper) 插入我的 java/recipes/windows 配方会给我以下错误:

Chef::Exceptions::ValidationFailed
       ----------------------------------
       value is a required property

【问题讨论】:

    标签: ruby chef-infra test-kitchen


    【解决方案1】:

    试试这个

    include Windows::Helper
    

    【讨论】:

    • 我在添加该行时收到以下错误:未定义方法 `include' for cookbook: java, recipe: windows :Chef::Recipe
    • 有趣。这个怎么样:Chef::Resource.send(:include, Windows::Helper)
    • 我编辑了我的问题以包括以下内容:我已经尝试过 Chef::Resource.send(:include, Windows::Helper) 和 ::Chef::Resource.send(:include, Windows ::Helper) 导致以下错误:value is a required property
    【解决方案2】:

    插入以下行为我解决了这个问题:

    ::Chef::Recipe.send(:include, Windows::Helper)

    这允许我使用 windows 食谱中的模块以下变量:

    module Windows
      module Helper
    ...
    {Variable}
      {Other_variable}
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多