Ruby While方法不能停止来自Twitter GEM的循环

威尔逊

我在网上找到了一些Twitter脚本,并且试图在5个tweet收藏夹之后停止循环,但并没有停止。关于如何修复循环的任何指针?当我<5开始时,我= 0

        config = {
            consumer_key:        '',
            consumer_secret:     '',
            access_token:        '',
            access_token_secret: ''
        }
        rClient = Twitter::REST::Client.new config
        sClient = Twitter::Streaming::Client.new(config)
        # topics to watch
        topics = ['#rails', '#ruby', '#coding', '#codepen']
        sClient.filter(:track => topics.join(',')) do |tweet|
            if tweet.is_a?(Twitter::Tweet)
              puts tweet.text 
              rClient.fav tweet
       i+=1
            end
        end
    rescue
        puts 'error occurred, waiting for 5 seconds'
        sleep 5
    end
end
爱德华

这是因为i将停止while循环,但不会停止您的客户端进行迭代(因为这完全是在1种情况下完成的while)。尝试:

    sClient.filter(:track => topics.join(',')) do |tweet|
        break if i >= 5
        if tweet.is_a?(Twitter::Tweet)
          puts tweet.text 
          rClient.fav tweet
          i+=1
        end
    end

您不应该需要while循环。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章