Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7770137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:20:05+00:00 2026-06-01T16:20:05+00:00

I am learning Ruby and I have a bug I cannot understand. I have

  • 0

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:

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

This works fine and the resulting strings (lines) are displayed correctly on a web page
I am generating.

Additionally, I want to remove all non-empty lines following the pattern. I have modified the method as follows:

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

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.

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?

So, unfortunately, also due to my scarce knowledge of Ruby, I have no clue as to how to debug this problem.

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”?

EDIT

Additional information. I am extending code that I have not written myself (it comes from
Octopress, file plugins/include_code.rb). The original
rendering code looks like this:

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 = "<figure class='code'><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"
    source += " #{highlight(code, @filetype)}</figure>"
    safe_wrap(source)
  end
end

I have replaced the line

code = file.read

with

code = linesToString(removeHeaderLines(stringToLines(file.read)))

where the two missing methods are:

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

I hope this helps.

EDIT 2

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

"A\nB\n".split(/\n/)

gives

["A", "B"]

Whereas by using each_line (as I did), one gets each line with the ‘\n’ at the end.
As a consequence

suffix.drop_while {|line| (line != "")}

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:

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

and it works fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:20:06+00:00Added an answer on June 1, 2026 at 4:20 pm

    I don’t know what cause the exception, but your code could be like this:

    def remove_header_lines(lines)
      pattern = /some pat/
      index = lines.index {|lines| lines =~ pattern}
    
      lines = lines.drop(index+1).drop_while {|lines| line != ""} unless index.nil?
    
      lines.join
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is is that I'm a newbie learning Ruby, or does it really have more
I am learning Ruby and Rails. I have a Ruby on Rails project that
Busy learning Ruby... the documentation have an example: hello world.count(lo, o) that return 2
I have some code that reads a file into an array of lines, and
I am learning Ruby and have to do some homework, but have problem with
I'm just learning ruby on rails and I have a table of user roles
Good afternoon, I'm learning about using RegEx's in Ruby, and have hit a point
I have started learning Ruby and just tried out my first hello world program
I have just started learning ruby on rails. I have a doubt wrt routing.
I have started learning Ruby recently and I was trying out the following piece

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.