简体   繁体   中英

Ruby skips items from list tasks

I am trying to make an app which if give the option to type, it types false then it skips the certain element from the list and it jumps to the next executing the same task.

That is the basic idea of the following code:

string["items"].each do |item|
  p continue.to_s + "<- item"

  begin 
    Anemone.crawl("http://" + item["displayLink"] + "/") do |anemone|                   
      anemone.on_every_page do |page|                           
        if continue.chomp.to_bool == false
          raise "no more please"
        end
        request = Typhoeus::Request.new(page.url, followlocation: true)
        response = request.run
        email = /[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}/.match(response.body)
        if email.nil?
        else
          p email
          begin
            continue = Timeout::timeout(2) do
              p "insert now false/nothing"
              gets
            end
          rescue Timeout::Error
            continue = "true"
          end
        end                 
      end                   
    end                 
    rescue
      continue = true
      next                  
    end         
    p "---------------------------------------------------------"
end

As the code shows, if the user types false when prompted the app should skip the item and go to the next one. However what it does is: when the user types false the app skips the current item and then doesn't execute any of the code that should be executed for all of the other items except the printing ( the second line of code );

Here is how the output looks like:

$ruby main.rb
"1"
"true<- item"
#<MatchData "support@keycreative.com">
"insert now false/nothing"
false
"true<- item"
"true<- item"
"true<- item"

As I'm doing my best to show after false is entered the code does skip the certain item from the list but it also never ever executes code for the other items as it should since it is an each loop

First I thought that maybe the continue is false however as you can see from the output the continue is true which makes me wonder why does ruby skip my code?

UPDATE Here is where the to_bool method comes from:

class String

    def to_bool()
        return true if self == "true"
        return false if self == "false"
        return nil
    end
end

In your last rescue statement add:

rescue => e
  puts e.message
  continue = true
  next                  
end

and inspect the output. Most likely your code is throwing an exception other than "no more please" (I expect undefined method to_bool for true:TrueClass ). Note that using exception for skipping the loop element is a terrible idea. Why can't you just get rid of this rescue and do:

if continue.chomp.to_bool == false
  continue = true
  next
end

There are a lot of things in this code which makes it very un-ruby-like. If you want to improve it please paste it to StackExchange CodeReview page. (link in the comment).

UPDATE:

My bad, you are in nested loop, so the if statement won't work. You might look at sth similar to raise/rescue bit, namely throw/catch, see example here: How to break from nested loops in Ruby? . I still think you should post it to codereview though for refactoring advises.

As to your actual code (without refactoring). You are calling to_bool method on continue, and in your rescue block you assign true instead of 'true' . Hence your to_bool method raises exception which is then rescued same way as 'no more please' exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM