Ask a Question related to Ruby, Design and Development.
-
Kurt V. Hindenburg #1
Dir.foreach not with patterns?
--Boundary-02=_GKgY/p64avOG7gO
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline
Why does the third one not work as expected? It appears that=20
Dir.foreach will not work with patterns...
#!/usr/bin/ruby
s =3D "/etc/host*"
#1
l =3D Dir[s]=20
p l
#2
Dir.foreach("/etc") { |d| p d }
#3
Dir.foreach(s) { |d| p d }
#4
# This is wasteful
l =3D Dir[s]
l.each { |f| p f }
=2D-=20
^^^ Kurt
There is no good nor evil, just power.
--Boundary-02=_GKgY/p64avOG7gO
Content-Type: application/pgp-signature
Content-Description: signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQA/YgKG0cAvx3ELfKARAt8dAJ4wrTeNfjRnQZ9XrserhejBSuNGKA CggWAk
WAnLflpyjLYvLKOZfQH4fcQ=
=Fd1o
-----END PGP SIGNATURE-----
--Boundary-02=_GKgY/p64avOG7gO--
Kurt V. Hindenburg Guest
-
Add Patterns to Pie Charts
Aaaahhhaaha can I vent. (I'm mostly done). I'm trying to bring in some excel charts into Indesign. I've read to turn them into .pdf's. Or... -
copying patterns
In illustrator 10, Mac. I have a pattern created by a friend. It was in an 8.5x11" box. I needed the final print to be that size. I also wanted to... -
fh9 cannot export patterns to swf?
Hi! try to make a shape fill with pattern, it will not export it with the pattern... why ? & how to overcome that? -
Patterns
Hi ! I've got a simple looking problem, do maybe someone will have simple, short solution for it. I hava a string whis is an expression e.g.:... -
moire patterns
took digital photos of buildings with wooden siding. in photoshop siding showed a moire pattern. how can i get rid of pattern? thankx -
nobu.nokada@softhome.net #2
Re: Dir.foreach not with patterns?
Hi,
At Sat, 13 Sep 2003 02:28:02 +0900,
Kurt V. Hindenburg wrote:Use Dir.glob.> Why does the third one not work as expected? It appears that
> Dir.foreach will not work with patterns...
--
Nobu Nakada
nobu.nokada@softhome.net Guest
-
dblack@superlink.net #3
Re: Dir.foreach not with patterns?
Hi --
On Sat, 13 Sep 2003, Kurt V. Hindenburg wrote:
You don't need two lines, though -- you can do this in a way that's actually> Why does the third one not work as expected? It appears that
> Dir.foreach will not work with patterns...
>
> #!/usr/bin/ruby
>
> s = "/etc/host*"
>
> #1
> l = Dir[s]
> p l
>
> #2
> Dir.foreach("/etc") { |d| p d }
>
> #3
> Dir.foreach(s) { |d| p d }
>
> #4
> # This is wasteful
> l = Dir[s]
> l.each { |f| p f }
shorter than #3:
Dir[s].each {|f| p f}
David
--
David Alan Black
home: [email]dblack@superlink.net[/email]
work: [email]blackdav@shu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]
dblack@superlink.net Guest
-
Weirich, James #4
Re: Dir.foreach not with patterns?
I like the Dir[] form (or its "glob" alternative). I used to write
recursive functions to search a directory tree. Then I discovered
Dir['**/*.rb']. Not only is it easier, it's also faster.
--
-- Jim Weirich / Compuware
-- FWP Capture Services
-- Phone: 859-386-8855
> -----Original Message-----
> From: [email]dblack@superlink.net[/email] [mailto:dblack@superlink.net]
> Sent: Friday, September 12, 2003 1:44 PM
> To: [email]ruby-talk@ruby-lang.org[/email]
> Subject: Re: Dir.foreach not with patterns?
>
>
> Hi --
>
> On Sat, 13 Sep 2003, Kurt V. Hindenburg wrote:
>>> > Why does the third one not work as expected? It appears that
> > Dir.foreach will not work with patterns...
> >
> > #!/usr/bin/ruby
> >
> > s = "/etc/host*"
> >
> > #1
> > l = Dir[s]
> > p l
> >
> > #2
> > Dir.foreach("/etc") { |d| p d }
> >
> > #3
> > Dir.foreach(s) { |d| p d }
> >
> > #4
> > # This is wasteful
> > l = Dir[s]
> > l.each { |f| p f }
> You don't need two lines, though -- you can do this in a way
> that's actually
> shorter than #3:
>
> Dir[s].each {|f| p f}
>
>
> David
>
> --
> David Alan Black
> home: [email]dblack@superlink.net[/email]
> work: [email]blackdav@shu.edu[/email]
> Web: [url]http://pirate.shu.edu/~blackdav[/url]
>
>Weirich, James Guest
-
Alan Davies #5
Re: Dir.foreach not with patterns?
Weirich, James wrote:
I'm not so sure. I timed my recursive "Filescan" class against Dir[] on>> I like the Dir[] form (or its "glob" alternative). I used to write
>> recursive functions to search a directory tree. Then I discovered
>> Dir['**/*.rb']. Not only is it easier, it's also faster.
a large fileserver. Dir[] took 92 seconds, and my Filescan class only
took 72 seconds.
I also prefer the ordering of the output of my Filescan class.
Alan Davies Guest
-
Weirich, James #6
Re: Dir.foreach not with patterns?
> >> I like the Dir[] form (or its "glob" alternative). I used to write
Hmmm ... Perhaps I implemented the my recursive search in a slow manner.>> >> recursive functions to search a directory tree. Then I discovered
> >> Dir['**/*.rb']. Not only is it easier, it's also faster.
> I'm not so sure. I timed my recursive "Filescan" class against Dir[] on
> a large fileserver. Dir[] took 92 seconds, and my Filescan class only
> took 72 seconds.
Care to share your Filescan class?
--
-- Jim Weirich / Compuware
-- FWP Capture Services
-- Phone: 859-386-8855
Weirich, James Guest
-
Jason Creighton #7
Re: Dir.foreach not with patterns?
On Thu, 18 Sep 2003 15:59:25 +0100
Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> wrote:
Could you post your Filescan class?> Weirich, James wrote:
>>> >> I like the Dir[] form (or its "glob" alternative). I used to write
> >> recursive functions to search a directory tree. Then I discovered
> >> Dir['**/*.rb']. Not only is it easier, it's also faster.
> I'm not so sure. I timed my recursive "Filescan" class against Dir[] on
> a large fileserver. Dir[] took 92 seconds, and my Filescan class only
> took 72 seconds.
>
> I also prefer the ordering of the output of my Filescan class.
I'd like to see it, and compare w/ find.rb in the standard distribution.
Jason Creighton
Jason Creighton Guest
-
Alan Davies #8
Re: Dir.foreach not with patterns?
> Could you post your Filescan class?
Attatched. Its faster at most things than Dir#[]. I think the reason>
> I'd like to see it, and compare w/ find.rb in the standard distribution.
>
it is faster is because it never actually changes the working directory.
To use it, e.g.:
require "Filescan"
File.open("out.txt", 'w') do |file|
Filescan.new("c:/").each { |name| file.puts name if name =~ /\.mp3$/i}
end
There are also functions each_filename, each_file, each_dirname, each_dir.
each_line can also be used to seach through indvidual lines of text
files. It can also be used for search and replace.
# Filescan.rb
#
# Class to recursively scan through files from a given dir.
#
# Copyright (C) 2003 by Alan Davies
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
class Filescan
include Enumerable
@@dotDir = /^\.\.?$/
def initialize(dir='.', recursive=true, verbose=false)
@startDir = dir
@recursive = recursive
@verbose = verbose
yield self if block_given?
end
attr_accessor :startDir, :recursive, :verbose
def each(startDir=@startDir, &block)
puts startDir if @verbose
begin
Dir.open(startDir) do |dir|
dir.each do |entry|
next if entry =~ @@dotDir
yield (fullPath = File.expand_path(entry, startDir))
self.each(fullPath, &block) if @recursive and File.directory?(fullPath)
end
end
rescue Errno::EINVAL, Errno::ENOENT
$stderr.puts "Can't open dir: #{startDir}"
return
end
end # def each
# Executes a block for each directory name
def each_dirname(pattern=//)
self.each { |dirName| yield dirName if File.directory?(dirName) and (dirName =~ pattern) }
end
# Executes a block for each directory
def each_dir(pattern=//)
each_dirname(pattern) do |dirName|
Dir.open(dirName) { |dirHandle| yield dirHandle, dirName }
end
end
# Execute a block for each filename
def each_filename(pattern=//)
self.each { |fileName| yield fileName if File.file?(fileName) and (fileName =~ pattern) }
end
# Execute a block for each file
def each_file(mode='r', pattern=//)
self.each_filename(pattern) do |filename|
File.open(filename, mode) { |file| yield file, filename }
end
end
# Executes a block for each line of each file.
# If the files are opened in writable mode, then the block must return the
# no of changes that were made to the line.
# Files are not written back to the disk if no lines are altered.
def each_line(writable=false, pattern=//)
self.each_file(writable ? 'r+' : 'r', pattern) do |file, filename|
fileChanges = lineChanges = len = 0
text = file.readlines
# Yield the block for each line in the file
text.each do |line|
lineChanges = yield(line, filename)
fileChanges += lineChanges if writable
end
# Write the file back to disk
if writable and (fileChanges > 0)
puts "#{filename} - #{fileChanges} changes made"
file.rewind
file.syswrite(text)
file.truncate(file.pos)
end
end # self.eachfile
end # def each_line
end # class Filescan
Alan Davies Guest
-
Alan Davies #9
Re: Dir.foreach not with patterns?
> Attatched. Its faster at most things than Dir#[]. I think the reason
And again. This one has much faster implementations of each_filename> it is faster is because it never actually changes the working directory.
and each_dirname, by doing the regexp before doing the file/directory test.
# Filescan.rb
#
# Class to recursively scan through files from a given dir.
#
# Copyright (C) 2003 by Alan Davies
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
class Filescan
include Enumerable
@@dotDir = /^\.\.?$/
def initialize(dir='.', recursive=true, verbose=false)
@startDir = dir
@recursive = recursive
@verbose = verbose
yield self if block_given?
end
attr_accessor :startDir, :recursive, :verbose
def each(startDir=@startDir, &block)
puts startDir if @verbose
begin
Dir.open(startDir) do |dir|
dir.each do |entry|
next if entry =~ @@dotDir
yield (fullPath = File.expand_path(entry, startDir))
self.each(fullPath, &block) if @recursive and File.directory?(fullPath)
end
end
rescue Errno::EINVAL, Errno::ENOENT
$stderr.puts "Can't open dir: #{startDir}"
return
end
end # def each
# Executes a block for each directory name
def each_dirname(pattern=//)
self.each { |dirName| yield dirName if (dirName =~ pattern) and File.directory?(dirName) }
end
# Executes a block for each directory
def each_dir(pattern=//)
each_dirname(pattern) do |dirName|
Dir.open(dirName) { |dirHandle| yield dirHandle, dirName }
end
end
# Execute a block for each filename
def each_filename(pattern=//)
self.each { |fileName| yield fileName if (fileName =~ pattern) and File.file?(fileName) }
end
# Execute a block for each file
def each_file(mode='r', pattern=//)
self.each_filename(pattern) do |filename|
File.open(filename, mode) { |file| yield file, filename }
end
end
# Executes a block for each line of each file.
# If the files are opened in writable mode, then the block must return the
# no of changes that were made to the line.
# Files are not written back to the disk if no lines are altered.
def each_line(writable=false, pattern=//)
self.each_file(writable ? 'r+' : 'r', pattern) do |file, filename|
fileChanges = lineChanges = len = 0
text = file.readlines
# Yield the block for each line in the file
text.each do |line|
lineChanges = yield(line, filename)
fileChanges += lineChanges if writable
end
# Write the file back to disk
if writable and (fileChanges > 0)
puts "#{filename} - #{fileChanges} changes made"
file.rewind
file.syswrite(text)
file.truncate(file.pos)
end
end # self.eachfile
end # def each_line
end # class Filescan
Alan Davies Guest
-
ts #10
Re: Dir.foreach not with patterns?
>>>>> "A" == Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> writes:
Change this
A> @@dotDir = /^\.\.?$/
You can have a file (or a directory) with \n in the name
Guy Decoux
ts Guest
-
Alan Davies #11
Re: Dir.foreach not with patterns?
ts wrote:
I'm not sure what you're getting at.>>>>>>>"A" == Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> writes:
>
>
> Change this
>
> A> @@dotDir = /^\.\.?$/
>
> You can have a file (or a directory) with \n in the name
>
>
>
> Guy Decoux
>
I'm pretty sure you can't have filenames with \n in them, and that
pattern wouldn't match them anyway.
Alan Davies Guest
-
ts #12
Re: Dir.foreach not with patterns?
>>>>> "A" == Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> writes:
A> I'm pretty sure you can't have filenames with \n in them, and that
A> pattern wouldn't match them anyway.
Then test it with a real OS :-)
svg% ls -a
./ ../
svg%
svg% ruby -e 'Dir.mkdir "aa\n.."'
svg%
svg% ls -a
./ ../ aa?../
svg%
svg% ruby -e 'Dir.foreach(".") {|d| p d}'
"."
".."
"aa\n.."
svg%
svg% ruby -e 'Dir.foreach(".") {|d| next if d =~ /^\.\.?$/; p d}'
svg%
Guy Decoux
ts Guest
-
Alan Davies #13
Re: Dir.foreach not with patterns?
ts wrote:
Personally I can't stand linux/unix, but each to his own, as they say.>>>>>>>"A" == Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> writes:
>
> A> I'm pretty sure you can't have filenames with \n in them, and that
> A> pattern wouldn't match them anyway.
>
> Then test it with a real OS :-)
Filenames with \n in them is the most ridiculous thing I've ever heard.>
> svg% ls -a
> / ../
> svg%
>
> svg% ruby -e 'Dir.mkdir "aa\n.."'
> svg%
>
> svg% ls -a
> / ../ aa?../
> svg%
>
> svg% ruby -e 'Dir.foreach(".") {|d| p d}'
> "."
> ".."
> "aa\n.."
> svg%
>
> svg% ruby -e 'Dir.foreach(".") {|d| next if d =~ /^\.\.?$/; p d}'
> svg%
>
>
>
> Guy Decoux
>
>
I take it the regexp should be /\A\.\.?\z/ then?
Alan.
Alan Davies Guest
-
ts #14
Re: Dir.foreach not with patterns?
>>>>> "A" == Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> writes:
A> Filenames with \n in them is the most ridiculous thing I've ever heard.
:-)
A> I take it the regexp should be /\A\.\.?\z/ then?
or you can do like in lib/find.rb
svg% grep 'next if' lib/find.rb
next if f == "." or f == ".."
svg%
Guy Decoux
ts Guest
-
Alan Davies #15
Re: Dir.foreach not with patterns?
ts wrote:
I did used to do that. I can't even remember why I changed it to a regexp!> next if f == "." or f == ".."
Alan Davies Guest
-
Gavin Sinclair #16
Re: Dir.foreach not with patterns?
On Wednesday, September 24, 2003, 12:33:54 AM, Alan wrote:
> ts wrote:>> next if f == "." or f == ".."Come on, you can't resist.> I did used to do that. I can't even remember why I changed it to a regexp!
class Object
def in?(enum)
enum.include?(self)
end
end
...
next if f.in? [".", ".."]
...
Isn't that much nicer? ;)
Gavin
Gavin Sinclair Guest
-
Paul Duncan #17
Re: Dir.foreach not with patterns?
--TN8pJM9vJMHHFgJc
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
* Gavin Sinclair (gsinclair@soyabean.com.au) wrote:exp!> On Wednesday, September 24, 2003, 12:33:54 AM, Alan wrote:
>=20>=20> > ts wrote:> >> next if f =3D=3D "." or f =3D=3D ".."> > I did used to do that. I can't even remember why I changed it to a reg=or>=20
> Come on, you can't resist.
>=20
> class Object
> def in?(enum)
> enum.include?(self)
> end
> end
>=20
> ...
> next if f.in? [".", ".."]
> ...
next if %w{. ..}.include? f
but personally i prefer this:
next if f =3D~ /^\.\.?$/
--=20> Isn't that much nicer? ;)
> =20
> Gavin
>=20
Paul Duncan <pabs@pablotron.org> pabs in #gah (OPN IRC)
[url]http://www.pablotron.org/[/url] OpenPGP Key ID: 0x82C29562
--TN8pJM9vJMHHFgJc
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQE/eHGkzdlT34LClWIRAnFHAKCxLkNaqu4WcOdYd64j8v2FdX/CfgCgxVgP
BPrO3S7ZwUsVTK1nLL1EUpM=
=Isbc
-----END PGP SIGNATURE-----
--TN8pJM9vJMHHFgJc--
Paul Duncan Guest
-
dblack@superlink.net #18
Re: Dir.foreach not with patterns?
Hi --
On Tue, 30 Sep 2003, Paul Duncan wrote:
However....> * Gavin Sinclair (gsinclair@soyabean.com.au) wrote:>> > On Wednesday, September 24, 2003, 12:33:54 AM, Alan wrote:
> >> >> > > ts wrote:
> > >> next if f == "." or f == ".."> >> > > I did used to do that. I can't even remember why I changed it to a regexp!
> > Come on, you can't resist.
> >
> > class Object
> > def in?(enum)
> > enum.include?(self)
> > end
> > end
> >
> > ...
> > next if f.in? [".", ".."]
> > ...
> or
> next if %w{. ..}.include? f
>
> but personally i prefer this:
>
> next if f =~ /^\.\.?$/
$ ruby -e 'File.new("abc\n..\ndef","w")'
$ ruby -e 'Dir["abc*"].each {|f|abc> puts "#{f}: yes" if /^\.\.?$/.match(f)}'
..
def: yes
I know, I know, not likely :-) Still, /\A\.\.?\z/ would be safer.
David
--
David Alan Black
home: [email]dblack@superlink.net[/email]
work: [email]blackdav@shu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]
dblack@superlink.net Guest



Reply With Quote

