当我在我的代码中添加一个 byebug 语句时,我正在调查的错误消失了

When I add a byebug statement to my code the bug I was investigating disappears

我 运行 遇到了一个奇怪的错误,我不确定如何解决。我正在尝试制作一个程序,使用 Ruby 和 Selenium 在浏览器中解决 Wordle。我注意到,从第二次猜测开始,程序只会继续猜测同一个词,而不是合并反馈并获取新词。于是我加了一个byebug语句看看是怎么回事,然后就开始工作了。如果我删除 byebug 语句,该错误会再次出现。我简直不敢相信,所以我推到 Github 并在另一台计算机上克隆,同样的事情又发生了。基本上,只要我在第 24 行之后的任意位置放置一个 byebug 语句,代码就会按预期工作。这是代码。

require 'webdrivers'
require './words.rb'
require 'byebug'

class WordleSolver
  
  attr_reader :driver

  def initialize
    options = Selenium::WebDriver::Options.chrome
    @driver = Selenium::WebDriver.for :chrome, options: options
    @driver.manage.timeouts.implicit_wait = 10
    @driver.get 'https://www.nytimes.com/games/wordle/index.html'
    @game_div = driver.find_element(css: 'game-app').shadow_root.find_element(css: 'div#game')
    @words = PossibleWords::WORDS 
    @word = ['', '', '', '', '']
    @present = {}
    @absent = []
    @current_row = 1
  end

  def solve
    close_modal
    guess('store')
    5.times do 
      get_feedback
      break if game_won?
      next_guess = formulate_guess
      guess(next_guess)
      sleep(3)
    end
  end

  def game_won?
    @word.each { |letter| return false if letter == '' }
    true
  end

  def close_modal
    @game_div.find_element(css: 'game-modal').shadow_root.find_element(css: 'div.close-icon').click
  end

  def guess(word)
    @driver.action.send_keys(word).send_keys(:enter).perform
  end

  def formulate_guess
    @words.each do |word|
      return word if valid_guess?(word)
    end
    'guess'
  end

  def valid_guess?(word)
    @word.each_with_index do |letter, index|
      if letter != ''
        return false if word[index] != letter
      end
    end

    @absent.each do |letter|
      return false if word.include?(letter)
    end

    @present.each do |letter, incorrect_positions|
      return false if !word.include?(letter)
      incorrect_positions.each do |position|
        return false if word[position] == letter
      end
    end
  end

  def get_feedback
    row = @game_div.find_element(css: "game-row:nth-of-type(#{@current_row})").shadow_root
    1.upto(5) do |n|
      tile = row.find_element(css: "game-tile:nth-of-type(#{n})")
      letter = tile.attribute("letter")
      evaluation = tile.attribute("evaluation")
      add_feedback(letter, evaluation, n - 1)
    end
    
    @current_row += 1
  end

  def add_feedback(letter, evaluation, position)
    case evaluation
    when 'absent'
      @absent.push(letter)
    when 'present'
      if @present[letter]
        @present[letter].push(position)
      else
        @present[letter] = [position]
      end
    when 'correct'
      @present.delete(letter)
      @word[position] = letter if @word[position] == ''
    end
  end
end

begin
  solver = WordleSolver.new
  solver.solve
  sleep(5)
ensure
  solver.driver.quit
end

TLDR 当我向我的代码添加一个 byebug 语句时,我正在调查的错误消失了

BroiSatse 的评论解决了我的问题。通过添加 byebug 语句,我还导致程序等待,使其有时间与浏览器同步,从而修复了错误。