help on a script

R

Ruby Newbee

Hi,

I just wrote a small script to find the files in current directory and
its child directories which include the word "ruby".
But it can't work, please help point out my error, thanks.


def myfind(path=".")
Dir.foreach(path) do |f|
if File.file? f
open(f) do |c|
puts "#{path}/#{f}" if c.read.scan(/ruby/)
end
elsif File.directory? f
newpath = File.join path,f
myfind(newpath)
end
end
end

myfind
 
Z

Zion hardes

[Note: parts of this message were removed to make it a legal post.]

exclude . and .. in directories
 
Z

Zion hardes

[Note: parts of this message were removed to make it a legal post.]

elsif File.directory?(f) && File.basename(f) != '..' && File.basename(f) !=
'.'

Maybe like this? :(
 
A

Ammar Ali

Ruby said:
Hi,

I just wrote a small script to find the files in current directory and
its child directories which include the word "ruby".
But it can't work, please help point out my error, thanks.


def myfind(path=".")
Dir.foreach(path) do |f|
if File.file? f
open(f) do |c|
puts "#{path}/#{f}" if c.read.scan(/ruby/)
end
elsif File.directory? f
newpath = File.join path,f
myfind(newpath)
end
end
end

myfind

Hi,

Walking the directory recursively can be done with 'glob', or it's
equivalent method '[]'. The following will recursively find all files
that have a '.rb' extension, starting from the current directory.

Dir['**/*.rb'].each { |path| puts path }

The scan method returns an array of matches, or takes a block which can
act on that array. To check if any matches where found, one can check
that the returned array is not empty, for example:

puts path if c.read.scan(/ruby/).length > 0



ammar
 
R

Ruby Newbee

Thanks all.
Under all your helps, I finally get the script to work correctly.

def myfind(path=".")
Dir.foreach(path) do |f|
f = File.join(path,f)
## puts f
if File.file? f
open(f) do |c|
puts "#{f}" unless c.read.scan(/ruby/).empty?
end
elsif File.directory? f and File.basename(f) != "." and
File.basename(f) != ".."
myfind(f)
end
end
end

myfind


Thanks.
Jenn.
 
R

Robert Klemme

2010/1/20 Ruby Newbee said:
Thanks all.
Under all your helps, I finally get the script to work correctly.

def myfind(path=3D".")
=A0 =A0Dir.foreach(path) do |f|
=A0 =A0 =A0 =A0f =3D File.join(path,f)
## =A0 =A0 =A0 =A0puts f
=A0 =A0 =A0 =A0if File.file? f
=A0 =A0 =A0 =A0 =A0 =A0open(f) do |c|
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0puts "#{f}" unless c.read.scan(/ruby/).emp=
ty?

You just need a single match - not all of them.
=A0 =A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0elsif File.directory? f and File.basename(f) !=3D "." and
File.basename(f) !=3D ".."
=A0 =A0 =A0 =A0 =A0 =A0myfind(f)
=A0 =A0 =A0 =A0end
=A0 =A0end
end

myfind

It is easier if you use Find:

require 'find'

Find.find path do |f|
puts f if File.file?(f) && /ruby/ =3D~ File.read(f)
end

:)

http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/index.html

Kind regards

robert

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top