Jordan Scales
Jordan Scales

Reputation: 2717

YAML.load(file) returning false?

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

Answers (2)

amit bhosale
amit bhosale

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

Chirantan
Chirantan

Reputation: 15634

That's probably because YAML.parse("") returns false. So I suppose this is the expected behavior.

Upvotes: 6

Related Questions