Need help with a loop

I am trying to make a loop that will make X amount of files and count up
and I cant figure out the syntax.

Pretty much create file1.txt and do it 50 times so I get file1.txt
file2.txt and so on till I reach file file50.txt.

Joel P. wrote in post #1179796:

num_files.times { |num| File.write( “File number #{num}.txt”, num ) }

Only that creating filenames with a space in the name puts you in a
state of sin… :wink:

As a simple example, this worked for me:

def write_files( num_files )
num_files.times { |num| File.write( “File number #{num}.txt”, num ) }
end

write_files 5

#> File number 0.txt
File number 1.txt
File number 2.txt
File number 3.txt
File number 4.txt

Ronald F. wrote in post #1179832:

Joel P. wrote in post #1179796:

num_files.times { |num| File.write( “File number #{num}.txt”, num ) }

Only that creating filenames with a space in the name puts you in a
state of sin… :wink:

Meh, I’m using Windows. Can’t get much more sinful!

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