Ruby forgets local variables during a while loop?

后端 未结 6 823
我在风中等你
我在风中等你 2020-12-09 17:07

I\'m processing a record-based text file: so I\'m looking for a starting string which constitutes the start of a record: there is no end-of-record marker, so I use the start

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 17:56

    I think this is because message is defined inside the loop. At the end of the loop iteration "message" goes out of scope. Defining "message" outside of the loop stops the variable from going out of scope at the end of each loop iteration. So I think you have the right answer.

    You could output the value of message at the beginning of each loop iteration to test whether my suggestion is correct.

提交回复
热议问题