Undefined method for nil

Sorry to bother you with this stupid and trivial question, but I’m a
rookie with ruby, and I can not understand why I get the undefined
method message.

I’ve been looking in the mailing list and I’ve not found any aceptable
answer, even I’ve asked Google, but the only answers are in Kanji and
Katakana, and unfortunatelly I don’t read japanese.

My problem is a very simple one, I’m writing small programs to learn but
in my first one I found that the interpreter complains that my semantics
are bad.

This is my program, a simple parser for the djbdns flat database

argfile=ARGV[0]
inFile= (argfile == nil or argfile.upcase == ‘STDIN’ ) ? STDIN :
File.new(argfile,‘r’)

firstChar=’‘
inFile.each_line { | line |
line.chomp!
firstChar=line[0].chr
arr=firstChar.to_a + line[1,line.size].split(’:’)

… More code here, but the interpreter accepts it

}

The interpreter says that
dns_process.rb:7: undefined method chr' for nil (NameError) from dns_process.rb:5:ineach_line’
from dns_process.rb:5

It seems that ruby understands that line[0] is a “nil” so he can not
find the “chr” method, when in fact “line” is a string and “line[0]” is
an integer, so “chr” is a right method for an integer, but ruby seems
not to understand this

Another problem
dns_process.rb:8: undefined method split' for nil (NameError) from t.rb:5:ineach_line’
from t.rb:5

Same problem, ruby does not understand that “line[1,line.size]” is a
string, so “split” is a right method for it

I’ve tested to put parentheses (line[0]).chr but this also does not
work and even to separate the methods a=line[0]; a.chr but also does not
work, ruby still thinks that it’s a “nil”. When I do a “test bed” in irb
with data, it works as expected.

I’m using ruby 1.6.6 on a Mandrake 8.2 system.

DOES ANYONE KNOW WHAT I’M DOING WRONG !!!

Please, an answer is needed, ruby is a wonderful language but it’s very
poorly documented, I’ve read the online “Programming Ruby” , and it’s
right to see how powerful ruby can be, but it’s not a very good help to
understand it deeply.

Does anyone know where to get better documentation ? What book is right?
I will buy one if I’m sure I’m spending my money right.

Thanks for reading this long question, and Thanks in advance for any answer.

Regards

Enric Lafont

Hello –

This is my program, a simple parser for the djbdns flat database

argfile=ARGV[0]
inFile= (argfile == nil or argfile.upcase == ‘STDIN’ ) ? STDIN :
File.new(argfile,‘r’)

firstChar=‘’
inFile.each_line { | line |
line.chomp!
firstChar=line[0].chr
arr=firstChar.to_a + line[1,line.size].split(‘:’)

… More code here, but the interpreter accepts it

}

The interpreter says that
dns_process.rb:7: undefined method chr' for nil (NameError) from dns_process.rb:5:in each_line’
from dns_process.rb:5

It seems that ruby understands that line[0] is a “nil” so he can not
find the “chr” method, when in fact “line” is a string and “line[0]” is
an integer, so “chr” is a right method for an integer, but ruby seems
not to understand this

Well, I think we can pretty confidently say that line[0] is nil :slight_smile:

If you have a blank line, and chomp! it, you then have an empty string.
And then, the [0] element will be nil.

So, I suspect there’s one or more empty lines in the file.

Try this:

line.chomp!
next unless line[0]

David

···

On Fri, 11 Oct 2002, Enric Lafont wrote:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

dblack@candle.superlink.net wrote:

Well, I think we can pretty confidently say that line[0] is nil :slight_smile:

If you have a blank line, and chomp! it, you then have an empty string.
And then, the [0] element will be nil.

So, I suspect there’s one or more empty lines in the file.

Try this:

line.chomp!
next unless line[0]

Thank David, you’re very right, it’s my “prototype” mind the one that
refuses to deal with exceptions as needed. My problem is that I was
thinking that Ruby was still in the loading and checking proccess so I
was unaware that it was dealing with real data.

Thanks again.

Enric Lafont