Reputation: 94
I am learning ruby and fairly new to coding in it. Below is the chef recipe that I wrote to merge two hashes and write to YAML file.
yaml_string = "AVG_METRICS:
get_requests:
- OneMinuteRate
- FifteenMinuteRate"
trimmed_custom_hash = YAML.load(yaml_string)
ruby_block do
merger = proc do |key,v1,v2|
Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
end
block do
if test_string.start_with? "7."
default_hash = YAML.load_file(File.join('/opt/tests/metrics','metrics_test.yml'))
default_hash.merge!(trimmed_custom_hash, &merger)
File.open("/opt/tests/metrics/metrics_test.yml", "w") { |file| file.write(default_hash.to_yaml)}
end
end
end
I am unable to understand why the implicit conversion error occurs. I have come up with questions when considering possibilities of where it might be failing.
I have been trying to figure this out for a while. It would be great if you could point me to where the error might be. Thanks!
Upvotes: 0
Views: 1863
Reputation: 106872
An error like this is raised when merge
is called on a hash with false
as an argument:
{}.merge(false)
#=> TypeError (no implicit conversion of false into Hash)
I would expect the error to be in the line v1.merge(v2, &merger)
or in default_hash.merge!(trimmed_custom_hash, &merger)
. Why does it happen? Hard to tell without knowing how trimmed_custom_hash
is defined.
It is a common Ruby idiom that methods ending with an ?
should return a boolean. But I would rely on this because it is just a convention and it isn't enforced by the language itself.
Upvotes: 5