Local Jump Error

C

Clement Ow

I get the following error when i execute my code:
A snippet of my code is as follows:

if $file_exception != nil
src1 = $file_exception.inject(Find.find(src)) {|result,
ex|result.reject{|x| File.basename(x) =~ Regexp.new(ex,
Regexp::IGNORECASE)}}
else
src1 = $source
end

i = i + 1

Find.find(src1).each do |file|
matchExp = excep_yyyymmdd.match(File.basename(file))
matchExp1 = excep_ddmmyyyy.match(File.basename(file))

Is there something wrong with my code at all? (maybe i overlooked
something very simple)
 
A

Adam Shelly

I get the following error when i execute my code:
A snippet of my code is as follows:

if $file_exception != nil
src1 = $file_exception.inject(Find.find(src)) {|result,
ex|result.reject{|x| File.basename(x) =~ Regexp.new(ex,
Regexp::IGNORECASE)}}

Is there something wrong with my code at all? (maybe i overlooked
something very simple)

The error message is telling you exactly what you overlooked: find
takes a block, not an argument.
You probably want Find.find{|val|val==src}

-Adam
 
C

Clement Ow

Adam said:
The error message is telling you exactly what you overlooked: find
takes a block, not an argument.
You probably want Find.find{|val|val==src}

-Adam

hmmm. Then how should i go about coding these lines? Cause just by
adding {} creates some syntax errors? Any help is appreciated! =)
 
S

Sebastian Hungerecker

Adam said:
You probably want Find.find{|val|val==src}

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.any? do |x|
/#{x}/i =~ File.basename(file)
end
end

or:

require 'enumerator'
src1 = Find.enum_for:)find, src).reject do |file|
$file_exception.any? do |x|
/#{x}/i =~ File.basename(file)
end
end
# (both untested)

HTH,
Sebastian
 
C

Clement Ow

Sebastian said:
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.any? do |x|
/#{x}/i =~ File.basename(file)
end
end

or:

require 'enumerator'
src1 = Find.enum_for:)find, src).reject do |file|
$file_exception.any? do |x|
/#{x}/i =~ File.basename(file)
end
end
# (both untested)

HTH,
Sebastian


if $file_exception != nil
Find.find($source) do |file|
src1 << file unless $file_exception.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?
 
S

Sebastian Hungerecker

Clement said:
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
 
C

Clement Ow

Sebastian said:
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.any? do |x|
/#{x}/i =~ File.basename(file)
end
end
#src1 = $file_exception.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|
:

:
 
S

Sebastian Hungerecker

Clement said:
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.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).

HTH,
Sebastian
 
C

Clement Ow

Clement said:
Ok, but if i just put
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 != nil
$source.each do |file|
src1 << file unless $file_exception.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!
 
Z

zuo peng

you probably need:
src1 << file unless $file_exception.all? do |x|
/#{x}/i =~ File.basename(file)
end

Clement said:
Ok, but if i just put
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 != nil
$source.each do |file|
src1 << file unless $file_exception.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!
 
C

Clement Ow

zuo said:
you probably need:
src1 << file unless $file_exception.all? do |x|
/#{x}/i =~ File.basename(file)
end
I tried all ways and means, all?, any? But it doesnt seem to work. Is
there something wrong with the structure of the code itself?
 

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

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top