Reputation: 2717
I have a file object which points to an empty .yml
file. When I try to load this file with YAML.load
I get false
instead of an empty hash. Is this the expected behavior?
file.read
is returning ""
as expected.
Upvotes: 3
Views: 2609
Reputation: 482
You can modify load_file
function as below:
Instead of
data_hash = YAML.load_file(yaml_file)
To
data_hash = YAML.load_file(yaml_file, {})
So when empty YAML
file is provided to the load_file
function, it will return an empty hash (my Ruby version is Ruby 2.4.0p0)
Example: empty file : /Users/batman/config/config.yml
irb:
2.4.0 :001 > require 'yaml'
=> true
2.4.0 :006 > YAML.load_file("/Users/batman/config/config.yml",{})
=> {}
Upvotes: 4
Reputation: 15634
That's probably because YAML.parse("")
returns false
. So I suppose this is the expected behavior.
Upvotes: 6