为什么Ruby代码的执行会跳转到另一个方法?[已关闭]

ruyhziif  于 2023-10-17  发布在  Ruby
关注(0)|答案(2)|浏览(92)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
上个月关门了。
Improve this question
我有一个Ruby 3.2.2的代码bug,这让我很困惑。使用byebug单步执行代码,执行将从第70行跳到第100行--发生这种情况确实令人困惑。
我在这里包含了相关的代码片段,带有行号,参数中aline的值为“2022 Outdoor Journal”:

61   def test_for_nonentry_lines( aline )
 62
 63     if 'JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECEMBER'.include? aline
 64       # If a month is detected, it means processing is between trip entries,
 65       # so set global IN_ENTRY to false:
 66         $IN_ENTRY = false
 67         return true
 68     elsif aline.include?('Outdoor Journal')
 69         $IN_ENTRY = false
 70         return true
 71     elsif aline.include?('——————')
 72         $IN_ENTRY = false
 73         return true
 74     elsif aline =~ /^\n$/ and not $IN_ENTRY
 75       # If this is a blank line but we're not writing a trip entry,
 76       # then this is a nonentry line:
 77         return true
 78     else
 79         return false
 80     end
 81   end
 82
 83   # ----------------------------------------------
 84   # is_start_of_entry
 85   #
 86   # tests current line as to whether is the start
 87   # of a new journal entry. returns boolean.
 88   # ----------------------------------------------
 89   def is_start_of_entry( aline )
 90
 91     # use a regexp to test if the passed in current line is start of a journal entry,
 92     # the pattern here is either the string "(pics)"
 93     # AND a date pattern like 1/15/22 or  1/5/22:
 94     # ELSE IF
 95     # the pattern matches this pattern: a blank line followed
 96     # by (some chars a comma)*n, a date, then a comma
 97
 98     # A line is the start of the new entry if:
 99     # - the line has a date & contains the string "(pics)"
100     if aline =~ /\d?\d\/\d?\d\/\d\d/ and aline =~ /(pics)/
101       return true

代码执行应该在第70行“返回true”。我正在使用byebug来单步执行代码,当它执行第70行时,程序跳转到第100行..
如果需要,可以提供整个程序。
提前感谢任何帮助…

blmhpbnm

blmhpbnm1#

以下是其中一种方式:

require "byebug"

def test_for_nonentry_lines
  byebug
  if true
    p "from here"
  else
    false
  end
end

def is_start_of_entry arg
  p "to over there"
end

is_start_of_entry("line") if test_for_nonentry_lines
# or like this
# is_start_of_entry(
#   test_for_nonentry_lines
# )

# if test_for_nonentry_lines
#   # in this case you would actually jump here
#   is_start_of_entry("line")
# end
$ ruby jump.rb
/home/alex/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/byebug-11.1.3/lib/byebug/attacher.rb:14: warning: undefining the allocator of T_DATA class Byebug::ThreadsTable
/home/alex/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/byebug-11.1.3/lib/byebug/attacher.rb:14: warning: undefining the allocator of T_DATA class Byebug::Context

[1, 10] in /home/alex/code/stackoverflow/jump.rb
    1: require "byebug"
    2: 
    3: def test_for_nonentry_lines
    4:   byebug
    5:   if true
=>  6:     p "from here"
    7:   else
    8:     false
    9:   end
   10: end
(byebug) next
"from here"

[8, 17] in /home/alex/code/stackoverflow/jump.rb
    8:     false
    9:   end
   10: end
   11: 
   12: def is_start_of_entry arg
=> 13:   p "to over there"
   14: end
   15: 
   16: 
(byebug) next
"to over there"

如果你跳到了你没有想到的地方,看看回溯bt,你也可以去up看看:

[8, 17] in /home/alex/code/stackoverflow/jump.rb
    8:     false
    9:   end
   10: end
   11: 
   12: def is_start_of_entry arg
=> 13:   p "to over there"
   14: end
   15: 
   16: is_start_of_entry(
   17:   test_for_nonentry_lines
(byebug) up

[11, 20] in /home/alex/code/stackoverflow/jump.rb
   11: 
   12: def is_start_of_entry arg
   13:   p "to over there"
   14: end
   15: 
=> 16: is_start_of_entry(
   17:   test_for_nonentry_lines
   18: )
   19:

另外,在byebug中没有颜色,使用debugger代替,默认情况下应该已经有了 https://github.com/ruby/debug

shyt4zoc

shyt4zoc2#

我认为问题是由case语句中使用的=操作符引起的。虽然我不能证明这一点,但当我用if/then语句替换case语句时,代码就开始工作了。

相关问题