Nope. The block passed to find is not a filter. Find takes an argument
as well
as a block and it executes the block for every file it finds. Find
always
returns nil. So you can’t call each on the return value of find and
using it
as the default value for inject makes no sense (as it’s the same as
inject(nil).
Find.find(src1).each do |file|
can be replaced with
Find.find(src1) do |file|
The inject could be replaced with:
src1 = []
Find.find(src) do |file|
src1 << file unless $file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end
or:
require ‘enumerator’
src1 = Find.enum_for(:find, src).reject do |file|
$file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end
Find always
returns nil. So you can’t call each on the return value of find and
using it
as the default value for inject makes no sense (as it’s the same as
inject(nil).
Find.find(src1).each do |file|
can be replaced with
Find.find(src1) do |file|
The inject could be replaced with:
src1 = []
Find.find(src) do |file|
src1 << file unless $file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end
or:
require ‘enumerator’
src1 = Find.enum_for(:find, src).reject do |file|
$file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end
(both untested)
HTH,
Sebastian
if $file_exception[i] != nil
Find.find($source) do |file|
src1 << file unless $file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end
else
src1 = $source
end
i = i + 1
Find.find(src1).each do |file|
:
:
end
Apparently now the code raises an error at the last Find.find(src1).each
do |file|, saying that there is a LocalJumpError.
Nope. The block passed to find is not a filter. Find takes an argument
as well
as a block and it executes the block for every file it finds.
If Find takes arguments, why it doesnt execute this block of code then?
Apparently now the code raises an error at the last Find.find(src1).each
do |file|, saying that there is a LocalJumpError.
You still have the each there. find does NOT return an array or any
other kind
of enumerable. You can’t use find without a block and you can’t use its
return
value (which is always nil) in any way.
HTH,
Sebastian
That means to say that I cant have an each, because Find does not return
an array? So does it mean to say that I’ll need not have each at all?
Do i do it this way then?
Find.find($source) do |file|
src1 << file unless $file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end #src1 = $file_exception[i].inject(Dir.glob(src)) {|result,
ex|result.reject{|x| File.basename(x) =~ Regexp.new(ex,
Regexp::IGNORECASE)}}
else
src1 = $source
end
i = i + 1
Find.find(src1) do |file|
file.each do |f|
:
:
That means to say that I cant have an each, because Find does not return
an array?
Yes.
So does it mean to say that I’ll need not have each at all?
Yes.
Do i do it this way then?
Find.find($source) do |file|
src1 << file unless $file_exception[i].any? do |x|
/#{x}/i =~ File.basename(file)
end
end
Yes, exactly.
Find.find(src1) do |file|
file.each do |f|
find yields the filenames as strings. So file.each will call String#each
on the filename. I don’t think that that’s what you want to do there (as
filenames only have one line anyway).
Apparently now the code raises an error at the last Find.find(src1).each
do |file|, saying that there is a LocalJumpError.
You still have the each there. find does NOT return an array or any
other kind
of enumerable. You can’t use find without a block and you can’t use its
return
value (which is always nil) in any way.
find yields the filenames as strings. So file.each will call String#each
on the filename. I don’t think that that’s what you want to do there (as
filenames only have one line anyway).
HTH,
Sebastian
Ok, but if i just put
Find.find(src1) do |file|
and eliminate file.each I get an error in:
`basename’: can’t convert Array into String (TypeError)
It seems that there is an array created someway or another… Is there
any way i can carry out my code with the consideration that I cant have
file.each? Or any other way that does the same logic? (all i want to do
is to search for files in my source paths with some exceptions included
and then execute commands accordingly(move, copy or delete). Much help
is appreciated!
find yields the filenames as strings. So file.each will call String#each
on the filename. I don’t think that that’s what you want to do there (as
filenames only have one line anyway).
HTH,
Sebastian
Ok, but if i just put
Find.find(src1) do |file|
and eliminate file.each I get an error in:
`basename’: can’t convert Array into String (TypeError)
It seems that there is an array created someway or another… Is there
any way i can carry out my code with the consideration that I cant have
file.each? Or any other way that does the same logic? (all i want to do
is to search for files in my source paths with some exceptions included
and then execute commands accordingly(move, copy or delete). Much help
is appreciated!
I’ve finally came out with the code without any errors(after much
debugging):
if $file_exception[i] != nil
$source.each do |file|
src1 << file unless $file_exception[i].each do |x|
/#{x}/i =~ File.basename(file)
end
end
else
src1 = $source
end
i = i + 1
src1.each do |folder|
Find.find(folder + "/") do |file|
:
:
It needs to be broken down into each source path and then from each
source path, you find files recursively. However, there’s one problem,
the exception doesnt work at all? Can anyone help me with the rejigging
of the file exception part? Thanks!