From: dblack@... Date: 2002-10-11T02:17:34+09:00 Subject: Re: undefined method for nil.. Hello -- On Fri, 11 Oct 2002, Enric Lafont wrote: > #### 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 :-) 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 -- 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