[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Local Jump Error

Clement Ow

5/21/2008 4:32:00 AM

I get the following error when i execute my code:
>>c:/ruby/lib/ruby/1.8/Find.rb:39:in `find': no block given (LocalJumpError)

A snippet of my code is as follows:

if $file_exception[i] != nil
src1 = $file_exception[i].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)
--
Posted via http://www.ruby-....

10 Answers

Adam Shelly

5/21/2008 5:43:00 AM

0

On Tue, May 20, 2008 at 9:31 PM, Clement Ow
<clement.ow@asia.bnpparibas.com> wrote:
> I get the following error when i execute my code:
>>>c:/ruby/lib/ruby/1.8/Find.rb:39:in `find': no block given (LocalJumpError)
>
> A snippet of my code is as follows:
>
> if $file_exception[i] != nil
> src1 = $file_exception[i].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

Clement Ow

5/21/2008 6:38:00 AM

0

Adam Shelly wrote:
> 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! =)
--
Posted via http://www.ruby-....

Sebastian Hungerecker

5/21/2008 7:21:00 AM

0

Adam Shelly wrote:
> 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[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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Clement Ow

5/21/2008 8:34:00 AM

0

Sebastian Hungerecker wrote:
>
> 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?
--
Posted via http://www.ruby-....

Sebastian Hungerecker

5/21/2008 3:25:00 PM

0

Clement Ow wrote:
> 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Clement Ow

5/22/2008 9:46:00 AM

0

Sebastian Hungerecker wrote:
> Clement Ow wrote:
>> 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|
:

:
--
Posted via http://www.ruby-....

Sebastian Hungerecker

5/22/2008 7:59:00 PM

0

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

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Clement Ow

5/28/2008 2:38:00 AM

0

Clement Ow wrote:
> Sebastian Hungerecker wrote:
>>> 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
>
> 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!
--
Posted via http://www.ruby-....

zuo peng

5/28/2008 4:47:00 AM

0

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

On Wed, May 28, 2008 at 10:37 AM, Clement Ow
<clement.ow@asia.bnpparibas.com> wrote:
> Clement Ow wrote:
>> Sebastian Hungerecker wrote:
>>>> 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
>>
>> 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!
> --
> Posted via http://www.ruby-....
>
>

Clement Ow

5/28/2008 6:31:00 AM

0

zuo peng wrote:
> you probably need:
> src1 << file unless $file_exception[i].all? do |x|
> /#{x}/i =~ File.basename(file)
> end
>
> On Wed, May 28, 2008 at 10:37 AM, Clement Ow

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?
--
Posted via http://www.ruby-....