Local Jump Error

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)

On Tue, May 20, 2008 at 9:31 PM, Clement Ow
[email protected] 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

Adam S. 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! =)

Adam S. 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

Sebastian H. 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?

Sebastian H. 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|
           :

           :

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

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

Sebastian H. 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!

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 wrote:

Sebastian H. 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!

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?

Works on 1.8.7 and 1.9.3

def find_files(dir, options = {})
  files = Array.new

  options[:recurse]  =  (options.has_key?(:recurse) ==

false)?false:options[:recurse]
options[:relative] = (options.has_key?(:relative) ==
false)?false:options[:relative]
options[:regex] = (options.has_key?(:regex) ==
false)?nil:options[:regex]

  Find.find(dir) { |full_path|
    # do not recurse down any sub directories
    if(options[:recurse] == false && File.directory?(full_path) &&

full_path != dir)
Find.prune()
else
if(File.directory?(full_path) == false &&
files.include?(full_path) == false)
if(options[:regex] == nil )
files.push(full_path)
elsif(full_path =~ /#{options[:regex]}/)
files.push(full_path)
end
end
end
}

  if(options[:relative] == true)
    files.each_index { | ii |
      files[ii].gsub!(/#{dir}\/(.+)/, ".\/\\1")
    }
  end

  files
end

Avdi G.'s books reminded me that Hash.fetch is a good idea, you could
say

recurse = options.fetch(:recurse, false)
relative = options.fetch(:relative, false)
regex = options.fetch(:regex, nil)

and set the values or their defaults up in local variables rather than
modifying options.

Hope this helps,

Mike

On Jan 21, 2014, at 12:25 PM, Francis T. [email protected]
wrote:

 options[:regex]    =  (options.has_key?(:regex) ==
       if(options[:regex] == nil )
     files[ii].gsub!(/#{dir}\/(.+)/, ".\/\\1")
   }
 end

 files

end


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

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs