Length and undefined method?

I have some code that reads a file. Each line has a number and a string. I'm trying to test for the length of the string to be > 0 so blank lines can be ignored.

  file.each_line do |line|
    line_items = line.split()
    expense = line_items[1]
    if expense.length < 1
      ...

The error I'm getting is:

  undefined method `length' for nil:NilClass (NoMethodError)

I'm obviosuly missing something, just not sure what. "expense" should wind up a string so length should be a valid method.

Leam

···

--
http://31challenge.net
http://31challenge.net/insight

Have you considered what happens if there is a blank line in the file?

[1] pry(main)> line = " \n"
=> " \n"
[2] pry(main)> line_items = line.split
=>

You might consider seeing if the split succeeded, for example

    next if line_items.length < 2

Hope this helps,

Mike

···

On Dec 30, 2014, at 8:27 AM, Leam Hall <leamhall@gmail.com> wrote:

I have some code that reads a file. Each line has a number and a string. I'm trying to test for the length of the string to be > 0 so blank lines can be ignored.

  file.each_line do |line|
    line_items = line.split()
    expense = line_items[1]
    if expense.length < 1
      ...

The error I'm getting is:

  undefined method `length' for nil:NilClass (NoMethodError)

I'm obviosuly missing something, just not sure what. "expense" should wind up a string so length should be a valid method.

Leam

--
http://31challenge.net
http://31challenge.net/insight

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Quoting Leam Hall (leamhall@gmail.com):

I have some code that reads a file. Each line has a number and a
string. I'm trying to test for the length of the string to be > 0 so
blank lines can be ignored.

  file.each_line do |line|
    line_items = line.split()
    expense = line_items[1]
    if expense.length < 1
      ...

The error I'm getting is:

  undefined method `length' for nil:NilClass (NoMethodError)

Umm... Most probably you should have

  expense = line_items[0]

otherwise, if your original string (that you do not provide) does not
contain whitespace, line_items[1] will be nil.

Carlo

···

Subject: length and undefined method?
  Date: Tue 30 Dec 14 08:27:30AM -0500

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Split by default splits on whitespace. If your line doesn't contain
whitespace or it contains only whitespace, or only one element which
is not whitespace, your line_items array will contain less than 2
elements:

2.0.0p195 :001 > "".split
=>
2.0.0p195 :002 > " ".split
=>
2.0.0p195 :003 > " a".split
=> ["a"]
2.0.0p195 :004 > " a ".split
=> ["a"]

So, at that point, line_items[1] will be nil:

2.0.0p195 :005 > " a ".split[1]
=> nil

so no length method there to be called. I suggest placing a
p line, line_items

to check what you really have.

Jesus.

···

On Tue, Dec 30, 2014 at 2:27 PM, Leam Hall <leamhall@gmail.com> wrote:

I have some code that reads a file. Each line has a number and a string. I'm
trying to test for the length of the string to be > 0 so blank lines can be
ignored.

        file.each_line do |line|
          line_items = line.split()
          expense = line_items[1]
          if expense.length < 1
            ...

The error I'm getting is:

        undefined method `length' for nil:NilClass (NoMethodError)

I'm obviosuly missing something, just not sure what. "expense" should wind
up a string so length should be a valid method.

if string is zero length then line_items[1] will be nil

"1 ".split

=> ["1"]

···

On Tue, Dec 30, 2014 at 6:57 PM, Leam Hall <leamhall@gmail.com> wrote:

I have some code that reads a file. Each line has a number and a string.
I'm trying to test for the length of the string to be > 0 so blank lines
can be ignored.

        file.each_line do |line|
          line_items = line.split()
          expense = line_items[1]
          if expense.length < 1
            ...

The error I'm getting is:

        undefined method `length' for nil:NilClass (NoMethodError)

I'm obviosuly missing something, just not sure what. "expense" should wind
up a string so length should be a valid method.

Leam

--
http://31challenge.net
http://31challenge.net/insight

I think that was the issue; the loop hit the blank line before printing anything.

What I would up doing was:

  if line_items.count < 1

The almost depressing part was that the file contained expenditures and tells me how much I'm spending on things like gas, books, etc. I preferred blissful ignorance. :slight_smile:

Thanks!

Leam

···

On 12/30/14 08:31, Arun kant sharma wrote:

if string is zero length then line_items[1] will be nil
>"1 ".split
=> ["1"]

On Tue, Dec 30, 2014 at 6:57 PM, Leam Hall <leamhall@gmail.com > <mailto:leamhall@gmail.com>> wrote:

    I have some code that reads a file. Each line has a number and a
    string. I'm trying to test for the length of the string to be > 0 so
    blank lines can be ignored.

             file.each_line do |line|
               line_items = line.split()
               expense = line_items[1]
               if expense.length < 1
                 ...

    The error I'm getting is:

             undefined method `length' for nil:NilClass (NoMethodError)

    I'm obviosuly missing something, just not sure what. "expense"
    should wind up a string so length should be a valid method.

    Leam

    --
    http://31challenge.net
    http://31challenge.net/insight

--
http://31challenge.net
http://31challenge.net/insight