Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
04-13-2017, 09:23 AM #1
- Join Date
- Apr 2017
- Thanked 0 Times in 0 Posts
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.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
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:
LoadError: cannot load such file -- test.rb from (irb):2:in 'load' from (irb):2 from C:/rubystack/ruby/bin/irb:12:in '<main>'
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
trying to write code that works around this:
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
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!