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

--
Posted via http://www.ruby-forum.com/.

require 'fileutils'

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

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

···

On Wed, Jul 13, 2011 at 10:55 PM, Simon Harrison <simon@simonharrison.net> wrote:

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?

--
Phillip Gawlowski

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

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 Harrison <simon@simonharrison.net>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/\.

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 3:55 PM, Simon Harrison <simon@simonharrison.net>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/\.

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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:

···

On Wed, Jul 13, 2011 at 11:51 PM, Simon Harrison <simon@simonharrison.net> wrote:

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

--
Phillip Gawlowski

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.

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 Jul 13, 2011, at 14:51 , Simon Harrison wrote:

  FileUtils.mkdir("Prefix-%03d".%(num))

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 8:52 PM, Simon Harrison <simon@simonharrison.net> wrote:

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

*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!

···

On Thu, Jul 14, 2011 at 12:30 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

What you really want to execute is:

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

--
Phillip Gawlowski

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

Ryan Davis wrote in post #1010583:

···

On Jul 13, 2011, at 14:51 , Simon Harrison 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?

--
Posted via http://www.ruby-forum.com/\.

It's called "colon": Colon (punctuation) - Wikipedia

Or what did you mean?

SCNR

robert

···

On Thu, Jul 14, 2011 at 8:55 PM, Simon Harrison <simon@simonharrison.net> wrote:

Ryan Davis wrote in post #1010583:

On Jul 13, 2011, at 14:51 , Simon Harrison 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?

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

···

On Fri, Jul 15, 2011 at 10:34 AM, Robert Klemme <shortcutter@googlemail.com>wrote:

On Thu, Jul 14, 2011 at 8:55 PM, Simon Harrison <simon@simonharrison.net> > wrote:
> Ryan Davis wrote in post #1010583:
>> On Jul 13, 2011, at 14:51 , Simon Harrison 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?

It's called "colon": Colon (punctuation) - Wikipedia

Or what did you mean?

SCNR

robert

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