Loaderror and some other stuff I can't figure out


I just started to learn Ruby (absolutely love it!), so most basics I
read up on. I gave myself a simple project, but I got stuck.

trying to build:
program that creates 5 .txt files in another dir, while creating this
dir in the process.


Dir.mkdir(File.join(’’, ‘temp_dir’))
Dir.chdir(File.join(’’, ‘temp_dir’))

@num = 0
@num = @num.to_i

loop do |file|
@num = @num.to_s
filenew = “blabla” + @num + “.txt”
fileinstance = File.new(filenew, ‘w’)
@num = @num.to_i
@num += 1
break if
@num >= 5

When i execute this via cmd (windows 7) then irb: load ‘test.rb’
it all works fine. Its probably not the shortest way to do it, but it
got me jumping out of my chair after hours and hours :stuck_out_tongue:

Though, when i run it again, 2 things happen.
1st: my irb return:
LoadError: cannot load such file – test.rb
from (irb):2:in ‘load’
from (irb):2
from C:/rubystack/ruby/bin/irb:12:in ‘’

It seems stuck or something, like I didn’t .close something… but I
did, I think…
So, when i type quit, open irb again it seems to execute it, but gives
me problem #2:
Errno::EEXIST: File exists - /temp_dir
etc etc

Well, its cause it already created the directory right? So, I’ve been
trying to write code that works around this:

Dir.foreach(’.’) do |dirname|
if Dir.exist?(“temp_dir”)
Dir.chdir(File.join(’’, ‘temp_dir’))
Dir.mkdir(File.join(’’, ‘temp_dir’))
Dir.chdir(File.join(’’, ‘temp_dir’))

I have been trying so many things here, this is just where I am at right

Hopefully someone here can tell me whats wrong here… this |dirname|
bugs me specifically, cause its just there, but has no use anywhere
thanks in advance!

Is there any reason to use instance variable “@num” instead of normal
variables “num” ?

Why would you run an *rb file from irb? You can just run it from the cli
with “ruby test.rb”. The error “LoadError: cannot load such file” you
get is most likely because you changed the current working directory
with “Dir.chdir”, which does not contain the ruby file “test.rb”.

As you pointed out correctly, Dir.mkdir fails if the directory exists
already. You can try checking if the directory exists already, but there
are several other reasons why Dir.mkdir might fail (eg. insufficient
permissions), so it’s best to catch the error it might throw. (see eg.

As for your second code snippet, the line

Dir.foreach(’.’) do |dirname|


iterates over all directories/files in the given directory (here: “.”).
But you already know what directory you want, so you’re right, that’s
pretty much pointless. I suppose you took the code from some tutorial?

With that in mind, let’s shorten/clean up the code a bit:

Main directory, where we want to create a new folder.

main_dir = “.”

Name of the temporary directory to create.

temp_dir = “temp_dir”

Expand the path so that it contains no relative references.

main_dir = File.expand_path(main_dir)

Get the absolute path of the new directory.

new_dir = File.join(main_dir,temp_dir)

Create the new directory. Raise an error only if the directory does

not exists after attempting to create it.

rescue StandardError => e
raise e unless Dir.exist?(new_dir)

Create 5 new files.

5.times do |i|
new_file = File.join(new_dir,“blabla_#{i}.txt”)
rescue StandardError => e
raise e unless File.exist?(new_file)

The above code should work even if the directory of the files exist

yay its works! Thank you very much.

Didn’t know about this ‘rescue’ code, smart way of using it. Feels
strange to use an expected error for file exist building your code
around though, had no idea this was even possible.

I took some of the code from the course I was following at Lynda, which
was great to be honest. I am working on another project atm and each bit
of code takes me multiple hours to get working. But I guess that’s not
rare… at least I understand the basics good enough to know where to
look if it fails and then lookup how to work the methods properly. Most
of the time that is :slight_smile:

And ye, I totally forgot that if I run it in irb it will change my .pwd
and therefor cant run(find) it again. What would be a good way to tackle
this in the future? Or will this just never be rly needed?

Hardest part for me so far seems to keep track of my objects, variables
and if I can use them only inside blocks/classes/instances etc.
Specially if I need a value to be set by a def and then use this value
Also loops make my head hurt. I feel like I write way to much code for
what I am trying to do, but that’s oke, at least it makes it easier for
me to understand.

But overall, a lot of fun. I spent insane hours on it atm. Thanks again
for the explanation!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs