NLis NLis - 1 month ago 21
Ruby Question

YAML.load_file issue

I have a bestiary.yml and test.rb in one directory, and when I try loading my .yml file like that

got_data_1 = YAML.load_file('bestiary.yml')


it gives me a

/usr/lib/ruby/2.3.0/psych.rb:471:in `initialize': No such file or directory @ rb_sysopen


error. But when I try launching this

got_data = YAML.load(File.open(File.join(File.dirname(__FILE__), 'bestiary.yml')))


everything works fine. Why load_file method doesn't work and what's the difference between this two methods?
Thank you

Answer

I suppose both files are in the same directory but you are not launching the rb file from that directory. If I'm correct try this as well

got_data_1 = YAML.load_file(File.join(File.dirname(__FILE__), 'bestiary.yml'))

This work just if the yml file is in the same directory AND you launch the rb file from that directory also

got_data_1 = YAML.load_file('bestiary.yml')