title: Ruby error: undefined method `[]’ for nil:NilClass
body: <p>I am learning Ruby and I have a bug I cannot understand. I have a method that takes an array of strings (lines) and removes all lines up to a certain line containing a pattern.
  The method looks as follows:</p>
  
  <pre><code>def removeHeaderLines(lines)
    pattern = "..." # Some pattern.
  
    # If the pattern is not there, do not drop any lines.
    prefix  = lines.take_while {|line| (not line.match(pattern))}
    if prefix.length == lines.length then
      return prefix
    end
  
    # The pattern is there: remove all line preceding it, as well as the line
    # containing it.
    suffix  = (lines.drop_while {|line| (not line.match(pattern))}).drop(1)
    return suffix
  end
  </code></pre>
  
  <p>This works fine and the resulting strings (lines) are displayed correctly on a web page
  I am generating.</p>
  
  <p>Additionally, I want to remove all non-empty lines following the pattern. I have modified the method as follows:</p>
  
  <pre><code>def removeHeaderLines(lines)
    pattern = "..." # Some pattern.
  
    # If the pattern is not there, do not drop any lines.
    prefix  = lines.take_while {|line| (not line.match(pattern))}
    if prefix.length == lines.length then
      return prefix
    end
  
    # The pattern is there: remove all line preceding it, as well as the line
    # containing it.
    suffix  = (lines.drop_while {|line| (not line.match(pattern))}).drop(1)
  
    # Remove leading non-empty lines.
    # ADDING THIS INTRODUCES A BUG.
    body = suffix.drop_while {|line| (line != "")}
    return body
  end
  </code></pre>
  
  <p>Very surprisingly (at least for me) this does not work. On the generated web page, instead of the content, I see the error message: Liquid error: undefined method `[]’ for nil:NilClass.</p>
  
  <p>I cannot make much sense out of this message. As far as I understand, some code calling my code has tried to access a non-array object as if it were an array. But both versions
  of my method return an array of strings (both variables suffix and body are set to an array of strings), so why should there be a difference?</p>
  
  <p>So, unfortunately, also due to my scarce knowledge of Ruby, I have no clue as to how to debug this problem.</p>
  
  <p>Does anybody see any mistake in the above code? Alternatively, does anybody have any hints as to what can cause the error "undefined method `[]’ for nil:NilClass"?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Additional information. I am extending code that I have not written myself (it comes from
  <a href="http://octopress.org" rel="nofollow">Octopress</a>, file plugins/include_code.rb). The original
  rendering code looks like this:</p>
  
  <pre><code>def render(context)
    code_dir = (context.registers[:site].config['code_dir'].sub(/^\//,'') || 'downloads/code')
    code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path
    file = code_path + @file
  
    if File.symlink?(code_path)
      return "Code directory '#{code_path}' cannot be a symlink"
    end
  
    unless file.file?
      return "File #{file} could not be found"
    end
  
    Dir.chdir(code_path) do
  
      ##################################
      # I have replaced the line below #
      ##################################
      code = file.read
  
      @filetype = file.extname.sub('.','') if @filetype.nil?
      title = @title ? "#{@title} (#{file.basename})" : file.basename
      url = "/#{code_dir}/#{@file}"
      source = "&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;#{title}&lt;/span&gt; &lt;a href='#{url}'&gt;download&lt;/a&gt;&lt;/figcaption&gt;\n"
      source += " #{highlight(code, @filetype)}&lt;/figure&gt;"
      safe_wrap(source)
    end
  end
  </code></pre>
  
  <p>I have replaced the line</p>
  
  <pre><code>code = file.read
  </code></pre>
  
  <p>with</p>
  
  <pre><code>code = linesToString(removeHeaderLines(stringToLines(file.read)))
  </code></pre>
  
  <p>where the two missing methods are:</p>
  
  <pre><code>def stringToLines(string)
    ar = Array.new
    string.each_line {|line| ar.push(line)}
  
    return ar
  end
  
  def linesToString(lines)
    s = ""
    lines.each {|line| s.concat(line)}
  
    return s
  end
  </code></pre>
  
  <p>I hope this helps.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Thanks to Hassan's hint (use the join method) I have found the problem!
  Parallel to the join method there exists a split method. So</p>
  
  <pre><code>"A\nB\n".split(/\n/)
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>["A", "B"]
  </code></pre>
  
  <p>Whereas by using each_line (as I did), one gets each line with the '\n' at the end.
  As a consequence</p>
  
  <pre><code>suffix.drop_while {|line| (line != "")}
  </code></pre>
  
  <p>drops all lines. The result was an empty string that apparently crashes the library
  I am using. Thanks to Hassan for indicating a more idiomatic solution. I have now the
  following:</p>
  
  <pre><code>def removeHeaderLines(code)
    lines = code.split(/\r?\n/)
    pat   = /.../ # Some pattern.
    index = lines.index {|line| line =~ pat}
    lines = lines.drop(index + 1).drop_while {|line| line != ""} unless index.nil?
  
    lines.join "\n"
  end
  </code></pre>
  
  <p>and it works fine.</p>