且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

食谱中的cookbook_name-TypeError:没有将符号隐式转换为字符串

更新时间:2022-12-15 09:38:06

在执行 + 操作时, Ruby中的字符串,它不会将其他类型转换为字符串。如果您想从Ruby自动执行此操作,则需要进行插值,例如:

  puts ####{cookbook_name} ::#{recipe_name}#{Time.now.inspect}:开始编译阶段 

想要使用 + ,则需要提供所有变量作为字符串:

 将 ### + cookbook_name.to_s + :: + recipe_name.to_s + + Time.now.inspect +:开始编译阶段 $ p> 

我建议您使用第一种方法。


I have the following code in my recipe but it errors.

log "###" + cookbook_name + "::" + recipe_name + " " + Time.now.inspect + ": Starting execution phase"
puts "###" + cookbook_name + "::" + recipe_name + " " + Time.now.inspect + ": Starting compile phase"

The error is:

TypeError: no implicit conversion of Symbol into String
/test/chef/cookbooks/cookbook-server/recipes/setup_server.rb:10:in `+'

The issue seems to be with the cookbook_name, recipe_name etc.(the ones which are not hardcoded). Please help.

When you're doing + action with the strings in Ruby, it doesn't convert other types to strings. If you want from Ruby to do that automatically, you need to do interpolation like:

puts "### #{cookbook_name} :: #{recipe_name} #{Time.now.inspect} : Starting compile phase"

If you want to use + you need to provide all variables as strings:

puts "###" + cookbook_name.to_s + "::" + recipe_name.to_s + " " + Time.now.inspect + ": Starting compile phase"

I suggest you to use first method.