Creating a range of directories

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

On Wed, Jul 13, 2011 at 10:55 PM, Simon H.
[email protected] wrote:

mkdir(dirs)


Any better ways to accomplish this?

require ‘fileutils’

(1…100).each do |num|
FileUtils.mkdir(“prefix-%03d”, num)
end

Note: I’ve only proven the code correct, not tested it.


Phillip G.

twitter.com/phgaw
phgaw.posterous.com

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

On Wed, Jul 13, 2011 at 3:55 PM, Simon H.
[email protected]wrote:



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 }

On Wed, Jul 13, 2011 at 3:55 PM, Simon H.
[email protected]wrote:



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

Also in this case, where you use a FileUtils method only once, including
it
seems to be more overhead than benefit. But if you do prefer inclusion,
then
in cases like this, where you only ever call its methods from main, I
think
it is better to extend rather than include. This way you don’t pollute
Object with all of its methods.

On Wed, Jul 13, 2011 at 11:51 PM, Simon H.
[email protected] wrote:

Thanks a lot. I didn’t realise that FileUtils.mkdir accepted a format
string.

A String is a String is a String. :wink:

Should this, contrary to expectations, not work, just create the
string before you pass it to #mkdir within the block. :slight_smile:


Phillip G.

twitter.com/phgaw
phgaw.posterous.com

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

On Jul 13, 2011, at 14:51 , Simon H. wrote:

Thanks a lot. I didn’t realise that FileUtils.mkdir accepted a format
string.

It doesn’t. Philip’s code is wrong.

10000 % ruby
require ‘fileutils’

(1…100).each do |num|
FileUtils.mkdir(“prefix-%03d”, num)
end
^d

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1436:in
`dup’: can’t dup Fixnum (TypeError)

What you really want to execute is:

(1…100).each do |num|
FileUtils.mkdir “Prefix-%03d” % num
end

which is just syntactic sugar for:

On Thu, Jul 14, 2011 at 12:30 AM, Ryan D. [email protected]
wrote:

What you really want to execute is:

(1…100).each do |num|
FileUtils.mkdir “Prefix-%03d” % num
end

facepalm That’s what I wanted to type. So used to format strings
doing it the non-Ruby way that muscle memory got me. My apologies!


Phillip G.

twitter.com/phgaw
phgaw.posterous.com

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

Thanks a lot. I didn’t realise that FileUtils.mkdir accepted a format
string.

Ryan D. wrote in post #1010583:

On Jul 13, 2011, at 14:51 , Simon H. wrote:

What you really want to execute is:

(1…100).each do |num|
FileUtils.mkdir “Prefix-%03d” % num
end

which is just syntactic sugar for:

Thanks for clearing that up, Ryan. Out of curiosity what was the end of
the last line above?

On Thu, Jul 14, 2011 at 8:55 PM, Simon H.
[email protected] wrote:

which is just syntactic sugar for:

Thanks for clearing that up, Ryan. Out of curiosity what was the end of
the last line above?

It’s called “colon”:

Or what did you mean?

SCNR

robert

Ah right, cheers Josh. I wasn’t sure if he meant syntactic sugar for
sprintf.

Robert: I didn’t mean the colon. Ryan’s post ended abruptly on rubyforum
with

“which is just syntactic sugar for:”

and nothing else. Anyway it doesn’t matter. Cheers.

Edit: by the way, I’ve since realised that I can do the above with
string formatting at all:


require ‘fileutils’

(“prefix-001”…“prefix-100”).each do |e|
FileUtils.mkdir(e)
end


On Fri, Jul 15, 2011 at 8:52 PM, Simon H.
[email protected] wrote:

Ah right, cheers Josh. I wasn’t sure if he meant syntactic sugar for
sprintf.

Robert: I didn’t mean the colon.

I didn’t assume you did. :slight_smile:

Ryan’s post ended abruptly on rubyforum
with

“which is just syntactic sugar for:”

and nothing else. Anyway it doesn’t matter. Cheers.

Interestingly the quote is shown in full in the mailing list. I read
his posting on the mailing list and didn’t even notice the line was
gone in ruby-forum. Apparently the gateway needs some fixing…

Kind regards

robert

On Fri, Jul 15, 2011 at 10:34 AM, Robert K.
[email protected]wrote:

end
SCNR

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I think Ryan was saying that receiver % arg was syntactic sugar for
receiver.%(arg) but the second part was in a quote which made it look
like
it wasn’t part of his response, so his response looked unfinished.