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)

···

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

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

···

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)

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).each { |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

···

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

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-forum.com/\.

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

Avdi Grimm'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 Trujillo <lists@ruby-forum.com> wrote:

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).each { |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

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

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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-forum.com/\.

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

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-forum.com/\.

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

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!

···

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

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-forum.com/\.

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-forum.com/\.

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-forum.com/\.