title: Ruby forgets local variables during a while loop?
body: <p>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 of the next record to delimit the last record.</p>
  
  <p>So I have built a simple program to do this, but I see something which suprises me: it looks like Ruby is forgetting local variables exist - or have I found a programming error ? [although I don't think I have : if I define the variable 'message' before my loop I don't see the error].</p>
  
  <p>Here's a simplified example with example input data and error message in comments:</p>
  
  <pre><code>flag=false
  # message=nil # this is will prevent the issue.
  while line=gets do
      if line =~/hello/ then
          if flag==true then
              puts "#{message}"
          end
          message=StringIO.new(line);
          puts message
          flag=true
      else
          message &lt;&lt; line
      end
  end
  
  # Input File example:
  # hello this is a record
  # this is also part of the same record
  # hello this is a new record
  # this is still record 2
  # hello this is record 3 etc etc
  # 
  # Error when running: [nb, first iteration is fine]
  # &lt;StringIO:0x2e845ac&gt;
  # hello
  # test.rb:5: undefined local variable or method `message' for main:Object (NameError)
  #
  </code></pre>