Hi -- On Mon, 16 Jun 2008, Tom Cloyd wrote: > I know this is a simple problem - to 'most everyone but me, but I cannot see > why I'm getting this error. I've explored things with ruby-debug, and with > irb inside of the debugger, and I simply cannot replicate the error. > > The code (this most of a little routine for converting an HTML file to a > textile marked up file): > > # input > filein = open( "{whatever}" ) > fi = filein.readlines > # output > fileout = open( "_textile.txt", 'w') > delta = [ > ["</p>", ''], > ["</h1>", '']] # for brevity, I've truncated this array - you get the > idea, I assume > ld = delta.length > i = 0 until i > ld > #results = fi[0].gsub( delta[i][0], delta[i][1] ) <= in an attempt > isolate error, I broke this into the following: > s1 = delta[i][0] # <= exception is thrown here > s2 = delta[i][1] > results = fi[0].gsub( s1, s2 ) > i += 1 > end > > The error msg: > > $ ruby textilemkr.rb > textilemkr.rb:55:in `main': undefined method `[]' for nil:NilClass > (NoMethodError) > from textilemkr.rb:66 > > I've confirmed that delta[1][0] is valid, that "i" is 0 when error occurs, > etc. > > If it works in irb, why doesn't it work when I run the routine via the > interpreter? What obvious thing am I missing? It doesn't work in irb: >> delta = [ ?> ["</p>", ''], ?> ["</h1>", '']] # => [["</p>", ""], ["</h1>", ""]] >> ld = delta.length => 2 >> i = 0 => 0 >> until i > ld >> s1 = delta[i][0] # <= exception is thrown here >> s2 = delta[i][1] >> i += 1 >> end NoMethodError: undefined method `[]' for nil:NilClass The problem is that the length of the array is 2, but delta[2] is nil. I'm reasonably sure that that's when it's failing, not when i is 0, unless the shortened version is missing something that would make that happen. I would dispense with i entirely and just iterate over delta. David -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS June 16-19 Berlin ADVANCING WITH RAILS July 21-24 Edison, NJ See http://www.rubypal.com for details and updates!