Loaderror and some other stuff I can't figure out

Hi,

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.

Code0:

Dir.chdir(’…’)
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’)
fileinstance.close
@num = @num.to_i
@num += 1
break if
@num >= 5
end

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:
code1:

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

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

Hopefully someone here can tell me whats wrong here… this |dirname|
bugs me specifically, cause its just there, but has no use anywhere
else?
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.
http://rubylearning.com/satishtalim/ruby_exceptions.html)

As for your second code snippet, the line

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

end

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.

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

Create 5 new files.

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

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

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
elsewhere.
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