From: Josh Cheek Date: 2011-07-14T06:07:50+09:00 Subject: Re: Creating a range of directories --90e6ba53aa70f6ff0004a7f9ce42 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jul 13, 2011 at 3:55 PM, Simon Harrison wrote: > I'm wanting to create a range of directories with a prefix, like the > following: > > /prefix-001 > /prefix-002 > > etc. > > The following works, but seems to smell a bit. > > *************************** > > require 'fileutils' > include FileUtils > > var = 1..100 > dirs = var.map { |n| "%03d" % n } > dirs.each { |n| n.insert(0, "Prefix-") } > > mkdir(dirs) > > **************************** > > Any better ways to accomplish this? And, does anyone know of a good > tutorial/book for working with files and directories? > > Cheers > > -- > Posted via http://www.ruby-forum.com/. > > That's basically what I'd do, but the steps in the middle are executed curiously. I'd do it like this: dirs = (1..100).map { |i| "Prefix-%03d" % i } Or if you dislike like the % operator: dirs = (1..100).map { |i| sprintf "Prefix-%03d", i } --90e6ba53aa70f6ff0004a7f9ce42--