From: Enric Lafont Date: 2002-10-11T02:02:30+09:00 Subject: 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: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 Another problem dns_process.rb:8: undefined method `split' for nil (NameError) from t.rb:5:in `each_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