0

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

NLis
  • 53
  • 2
  • 8
  • See also [*Ruby loading config (yaml) file in same dir as source*](https://stackoverflow.com/questions/8878389/ruby-loading-config-yaml-file-in-same-dir-as-source). – Franklin Yu Dec 07 '18 at 04:30

1 Answers1

2

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')
Ursus
  • 29,643
  • 3
  • 33
  • 50