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
simonh
July 13, 2011, 11:07pm
2
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
simonh
July 13, 2011, 11:08pm
3
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 }
simonh
July 13, 2011, 11:16pm
4
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.
simonh
July 13, 2011, 11:56pm
5
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.
Should this, contrary to expectations, not work, just create the
string before you pass it to #mkdir within the block.
–
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
simonh
July 14, 2011, 12:30am
6
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:
simonh
July 14, 2011, 1:39am
7
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
simonh
July 13, 2011, 11:51pm
8
Thanks a lot. I didn’t realise that FileUtils.mkdir accepted a format
string.
simonh
July 14, 2011, 8:55pm
9
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?
simonh
July 15, 2011, 5:35pm
10
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”:
The colon, :, is a punctuation mark consisting of two equally sized dots aligned vertically. A colon often precedes an explanation, a list, or a quoted sentence. It is also used between hours and minutes in time, between certain elements in medical journal citations, between chapter and verse in Bible citations, and, in the US, for salutations in business letters and other formal letter writing.
In Ancient Greek, in rhetoric and prosody, the term κῶλον (kôlon, lit. 'limb, member of a body') did ...
Or what did you mean?
SCNR
robert
simonh
July 15, 2011, 8:52pm
11
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
simonh
July 16, 2011, 12:16am
12
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.
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
simonh
July 15, 2011, 5:51pm
13
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.