M
Matthew Smillie
I'm writing a quick script to selectively process some files, and I
found a bit of odd behaviour using Pathname#each and #each_entry,
which is best illustrated, rather than explained:
irb(main):001:0> require 'pathname'
=> true
irb(main):002:0> p = Pathname.new("/home/matt/test")
=> #<Pathname:/home/matt/test>
irb(main):003:0> p.entries.each{ |q|
irb(main):004:1* puts "operating on: #{q.dirname}/#{q.basename}"
irb(main):005:1> }
operating on: ./.
operating on: ./..
operating on: ./a
operating on: ./b
operating on: ./c
Now, the problem that results is that in the block, I can't do q.read
reliably (or indeed q.anything), because it's looking for q relative
to the current directory, when surely it should be looking relative
to p.
Apart from files not being found if you're not in the correct
directory, this could lead to more difficult-to-detect problems if
you have a structure like the following and run the script in a
directory which has identically-named files to the intended target,
for instance:
/home/matt/project/training/{1, 2, 3, 4, etc}
/home/matt/project/test/{1, 2, 3, 4, etc}
Since even when the directory to be processed is given as an absolute
pathname, its subdirectories are treated as relative pathnames.
I realise that I can solve this by doing (p + q).read inside the
block, but surely this isn't the desired behaviour of Pathname#each?
thanks,
matthew smillie.
found a bit of odd behaviour using Pathname#each and #each_entry,
which is best illustrated, rather than explained:
irb(main):001:0> require 'pathname'
=> true
irb(main):002:0> p = Pathname.new("/home/matt/test")
=> #<Pathname:/home/matt/test>
irb(main):003:0> p.entries.each{ |q|
irb(main):004:1* puts "operating on: #{q.dirname}/#{q.basename}"
irb(main):005:1> }
operating on: ./.
operating on: ./..
operating on: ./a
operating on: ./b
operating on: ./c
Now, the problem that results is that in the block, I can't do q.read
reliably (or indeed q.anything), because it's looking for q relative
to the current directory, when surely it should be looking relative
to p.
Apart from files not being found if you're not in the correct
directory, this could lead to more difficult-to-detect problems if
you have a structure like the following and run the script in a
directory which has identically-named files to the intended target,
for instance:
/home/matt/project/training/{1, 2, 3, 4, etc}
/home/matt/project/test/{1, 2, 3, 4, etc}
Since even when the directory to be processed is given as an absolute
pathname, its subdirectories are treated as relative pathnames.
I realise that I can solve this by doing (p + q).read inside the
block, but surely this isn't the desired behaviour of Pathname#each?
thanks,
matthew smillie.