如何从不同的配方调用方法?
How to call a method from a different recipe?
我正在尝试从食谱 "a" 的食谱 'A' 中访问食谱 "b" 的食谱 'B' 中的方法。我使用 include_recipe 'cookbook::recipe'.
将食谱 'B' 包含在食谱 'A' 中
#cookbook Flower
#chef recipe 'Rose' DSL
def method_to_be_called
do something
end
#cookbook Animal
#chef recipe 'Tiger' DSL
include_recipe "Flower::Rose"
#call method_to_be_called of 'Rose' recipe
end
我正在学习 Ruby 和 Chef DSL,因此我不知道我想要实现的目标是否可行。如果是,我该如何调用该方法?
提前致谢。
Chef 并不是这样工作的。您可以将您的方法包装在一个库中并以这种方式调用它。
例如,把你的方法放在:
# cookbook/rose/libraries/helper.rb
class Rose
def self.method_to_be_called
end
end
# cookbook/tiger/recipes/default.rb
Rose.method_to_be_called()
我正在尝试从食谱 "a" 的食谱 'A' 中访问食谱 "b" 的食谱 'B' 中的方法。我使用 include_recipe 'cookbook::recipe'.
将食谱 'B' 包含在食谱 'A' 中 #cookbook Flower
#chef recipe 'Rose' DSL
def method_to_be_called
do something
end
#cookbook Animal
#chef recipe 'Tiger' DSL
include_recipe "Flower::Rose"
#call method_to_be_called of 'Rose' recipe
end
我正在学习 Ruby 和 Chef DSL,因此我不知道我想要实现的目标是否可行。如果是,我该如何调用该方法? 提前致谢。
Chef 并不是这样工作的。您可以将您的方法包装在一个库中并以这种方式调用它。
例如,把你的方法放在:
# cookbook/rose/libraries/helper.rb
class Rose
def self.method_to_be_called
end
end
# cookbook/tiger/recipes/default.rb
Rose.method_to_be_called()