Reputation: 5049
Ruby 2.0
Why would the code below give unexpected return (LocalJumpError) ?
# some code here
puts "Scanning for xml files .."
zip_files = Dir.entries(directory).select { |f| File.extname(f) == '.zip' }
if(zip_files.count == 0)
puts "No files found, exiting..."
return
end
# more code here ( if files found)
Error: unexpected return (LocalJumpError)
No files found, exiting...
[Finished in 0.9s with exit code 1]
Upvotes: 4
Views: 5618
Reputation: 39
Alternatively, you could rescue the LocalJumpError
puts "Scanning for xml files .."
zip_files = Dir.entries(directory).select { |f| File.extname(f) == '.zip' }
begin
return unless zip_files.count > 0
# more code here ( if files found)
rescue LocalJumpError
puts "No files found, exiting..."
end
Upvotes: -3
Reputation: 239311
You're not in a method. You cannot return
from there. If you want to exit early, use exit
.
Upvotes: 9