简体   繁体   English

Ruby跳过列表任务中的项目

[英]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. 我正在尝试制作一个应用程序,如果该应用程序提供键入的选项,则键入false,然后跳过列表中的某些元素,并跳至下一个执行相同任务的对象。

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. 如代码所示,如​​果用户在提示时输入false,则应用程序应跳过该项目并转到下一个项目。 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 ); 但是,它的作用是:当用户键入false时,应用程序将跳过当前项目,然后不执行应为除打印之外的所有其他项目执行的任何代码(第二行代码);

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 正如我做我最好的假后,显示输入的代码做跳跃列表中的某些项目,但它也从来没有为其它项目执行代码,因为它应该,因为它是each循环

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? 首先,我认为也许continue是错误的,但是正如您从输出中看到的,continue是真实的,这使我想知道为什么ruby会跳过我的代码?

UPDATE Here is where the to_bool method comes from: 更新这是to_bool方法的来源:

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 ). 您的代码很可能引发除“请undefined method to_bool for true:TrueClass请”以外的异常(我希望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. 如果要改进它,请将其粘贴到StackExchange CodeReview页面。 (link in the comment). (评论中的链接)。

UPDATE: 更新:

My bad, you are in nested loop, so the if statement won't work. 不好意思,您处于嵌套循环中,因此if语句将不起作用。 You might look at sth similar to raise/rescue bit, namely throw/catch, see example here: How to break from nested loops in Ruby? 您可能会看到与提高/救援位类似的东西,即throw / catch,请参见此处的示例: 如何从Ruby中的嵌套循环中打破? . I still think you should post it to codereview though for refactoring advises. 我仍然认为您应该将其发布到codereview中,以获取重构建议。

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' . 您正在继续上调用to_bool方法,并在您的救援块中分配true而不是'true' Hence your to_bool method raises exception which is then rescued same way as 'no more please' exception. 因此,您的to_bool方法会引发异常,然后以与“ no more please”异常相同的方式进行挽救。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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