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.

Code:
Code:
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 :P

Though, when i run it again, 2 things happen.
1st: my irb return:

Code:
Code:
LoadError: cannot load such file -- test.rb
        from (irb):2:in 'load'
        from (irb):2
        from C:/rubystack/ruby/bin/irb:12:in '<main>'
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:

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

Code:
Code:
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!