Ruby 厨师库助手方法未定义

Ruby 厨师库助手方法未定义,ruby,chef-infra,lwrp,Ruby,Chef Infra,Lwrp,我正在尝试编写一个LWRP,它将根据条件从助手库调用方法。我在让提供程序读取外部方法时遇到语法问题 提供者非常简单 # providers/provider.rb require_relative '../libraries/acx_shared' include Acx action :create do Chef::Log.debug('{jefflibrary->lwrp->user} - start') if @new_resource.shared == true Acx

我正在尝试编写一个LWRP,它将根据条件从助手库调用方法。我在让提供程序读取外部方法时遇到语法问题

提供者非常简单

# providers/provider.rb require_relative '../libraries/acx_shared' include Acx action :create do Chef::Log.debug('{jefflibrary->lwrp->user} - start') if @new_resource.shared == true Acx::User::Shared.true_shared() else Acx::User::Shared.false_shared() end if @new_resource.sudo == true Chef::Log.error('I HAVE THE POWER') else Chef::Log.error('my power is weak and feeble') end if @new_resource.password == true Chef::Log.error('the secret password is 12345') else Chef::Log.error('I will never tell you the secret to the airlock') end Chef::Log.debug('{jefflibrary->lwrp->user} - end') end #providers/provider.rb 需要相对“../libraries/acx\u shared” 包括Acx 操作:创建do Chef::Log.debug(“{jefflibrary->lwrp->user}-start”) if@new_resource.shared==true Acx::User::Shared.true\u Shared() 其他的 Acx::User::Shared.false_Shared() 结束 if@new_resource.sudo==true Chef::Log.error('我有权') 其他的 厨师:Log.error('我的力量很弱') 结束 if@new_resource.password==true Chef::Log.error('密码是12345') 其他的 厨师:Log.error('我永远不会告诉你气闸的秘密') 结束 Chef::Log.debug(“{jefflibrary->lwrp->user}-end”) 结束 以及帮助程序库

#libraries/acx_shared.rb module Acx module User module Shared def true_shared #puts blah Chef::Log.error('I am pulling this from a library reference') end def false_shared Chef::Log.error('I am not very good at sharing') end end end end #图书馆/acx_shared.rb 模块Acx 模块用户 模块共享 def true_共享 #胡说八道 Chef::Log.error('我正在从库引用中提取此') 结束 def false_共享 Chef::Log.error('我不太擅长分享') 结束 结束 结束 结束 但每当我尝试运行它时,不管设置了什么资源属性,我都会得到

NoMethodError ------------- undefined method `false_shared' for Acx::User::Shared:Module 命名者 ------------- Acx::User::shared:模块的未定义方法“false\u shared”
在编写助手库的文档中,我显然遗漏了一些东西,但我不确定是什么。试着把一些东西搬来搬去,但开始没有主意了

尝试删除包含Acx的

可能发生的情况是,因为您正在这样做,它实际上正在Acx中查找另一个模块名Acx。 因此,要么删除include,要么删除Acx::from对*共享方法的调用。 您也不能在尝试时直接从模块调用实例变量。您需要有一个包含模块的类,然后调用该类对象上的方法。
作为替代方案,您可以将这些方法升级为类方法(self.),并且可以直接调用它们

比如:

# providers/provider.rb
require_relative '../libraries/acx_shared'

action :create  do
  Chef::Log.debug('{jefflibrary->lwrp->user} - start')

acx = Class.new.exted(Acx::User::Shared)
if @new_resource.shared == true
  acx.true_shared()
else
  acx.false_shared()
end

if @new_resource.sudo == true
  Chef::Log.error('I HAVE THE POWER')
else
  Chef::Log.error('my power is weak and feeble')
end
if @new_resource.password == true
  Chef::Log.error('the secret password is 12345')
else
  Chef::Log.error('I will never tell you the secret to the airlock')
end

  Chef::Log.debug('{jefflibrary->lwrp->user} - end')
end


acx_是否通过require在helper_库中共享拉入?helper_库在技术上被命名为acx_shared.rb,但我将其标记为helper_库,以便于理解。它应该是provider.rb拉入带有require和include Acx模块的helper库。然而,它并没有引入任何方法。这就是我所缺少的语法。这并不能使它更容易理解。请尝试删除include Acx。可能发生的情况是,因为您正在这样做,它实际上在Acx中查找另一个模块名Acx。因此,要么删除include,要么删除对*shared methods hanks的调用中的Acx::from,以便后续操作。不幸的是,我尝试了这两种方法,但仍然有可能发生通常很隐秘的错误。NoMethodError-------未定义Acx::User::shared:类的“true\u shared”方法,没错。您不能直接调用它,因为它是一个实例方法。您需要将模块包含到一个类中,如果您想直接调用它,您需要将其升级到一个类var(self.in front)。代码示例已更新。运行良好。诀窍是在调用include时一定要以模块结束。
#libraries/acx_shared.rb
module Acx
  module User
    module Shared
    def self.true_shared
      #puts blah
      Chef::Log.error('I am pulling this from a library reference')
    end
    def self.false_shared
      Chef::Log.error('I am not very good at sharing')
    end
  end
end
end