# Problems loading yaml file with Rake

**URL:** <https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551>\
**Category:** Development\
**Created:** [May 13, 2020, 1:34am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551 "2020-05-13T01:34:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![khomoritous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@khomoritous](https://www.ruby-forum.com/u/khomoritous)\
**Post date:** [May 13, 2020, 1:34am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/1 "2020-05-13T01:34:39Z")

</div>

Hi,  
I have problems loading a yaml file with Rake when testing and getting the following error;  
Errno::ENOENT: No such file or directory @ rb\_sysopen  
But there are no problems when testing from within the test directory. The Rakefile is located at the root of the project.

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 13, 2020, 9:50pm UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/2 "2020-05-13T21:50:36Z")

</div>

Hard to say without seeing your rakefile, but my first guess would be a path problem. When you try to load the yaml file, does rake know its location? e.g. if it is in the test directory?

---

<div class="post-metadata">

**Author:** ![khomoritous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@khomoritous](https://www.ruby-forum.com/u/khomoritous)\
**Post date:** [May 14, 2020, 11:44am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/3 "2020-05-14T11:44:38Z")

</div>

Rakefile content in test directory:

require ‘rake/testtask’

task :default =\> :test

Rake::TestTask.new do |task|  
task.pattern = ‘test\_\*.rb’  
task.warning = true  
end

No problems loading yaml file.

Moving Rakefile to root directory of project:

require ‘rake/testtask’

task :default =\> :test

Rake::TestTask.new do |task|  
task.pattern = ‘lib/language\_quiz/test\_\*.rb’  
task.warning = true  
end

---

<div class="post-metadata">

**Author:** ![khomoritous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@khomoritous](https://www.ruby-forum.com/u/khomoritous)\
**Post date:** [May 14, 2020, 11:46am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/4 "2020-05-14T11:46:14Z")

</div>

continued:

Errno::ENOENT: No such file or directory @ rb\_sysopen

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 17, 2020, 9:15pm UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/5 "2020-05-17T21:15:24Z")

</div>

This does not mention the yaml file.

Where is the yaml file being loaded? and how?

Remember that the ‘base folder’ that files are searched on will change depending on where you run the Rakefile from.

---

<div class="post-metadata">

**Author:** ![khomoritous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@khomoritous](https://www.ruby-forum.com/u/khomoritous)\
**Post date:** [May 18, 2020, 3:52am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/6 "2020-05-18T03:52:56Z")

</div>

All files, including the yaml file are lying in the same directory(language\_quiz) for now. The yaml file is loaded via a quiz-class in a quiz.rb file through its initializer i.e @data = YAML.load(File.read(“path to yaml file”))

---

<div class="post-metadata">

**Author:** ![khomoritous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@khomoritous](https://www.ruby-forum.com/u/khomoritous)\
**Post date:** [May 18, 2020, 5:25am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/7 "2020-05-18T05:25:48Z")

</div>

I don’t know if this helps;  
But if I put the load path to the yaml file in the quiz.rb file relative to where the rake file is, “the base folder”, then it finds it.

class Quiz(in quiz.rb)  
.  
.  
.

def initialize  
begin  
$LOG.debug(“Test loading yaml data…”)  
@data = YAML.load(File.read(“path to yaml file relative to the rakefile in the base folder”))  
end  
.  
.  
.

```
end

```

But then I can’t run the test from within the “language\_quiz” folder properly where I find the quiz.rb and the test\_quiz.rb file

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 19, 2020, 10:30am UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/8 "2020-05-19T10:30:13Z")

</div>

Probably your best solution is to find the directory of the file in which you are loading the file, e.g. by:

```auto
CWD = File.expand_path(File.dirname( __FILE__ ))

```

Then you can write:

```auto
YAML.load(File.read(File.join(CWD, 'yaml-file')))

```

You can then name the ‘yaml-file’ relative to the file in which the call is.

---

<div class="post-metadata">

**Author:** ![khomoritous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@khomoritous](https://www.ruby-forum.com/u/khomoritous)\
**Post date:** [May 19, 2020, 12:30pm UTC](https://www.ruby-forum.com/t/problems-loading-yaml-file-with-rake/259551/9 "2020-05-19T12:30:03Z")

</div>

That did it! Great! Thanks! 😃
