David A. Black wrote: > 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 > Nice. Thanks much, on both points. I never "saw" i get to 2, and so didn't see the error. Jeez. I do find it hard to be a weekend warrior with Ruby. I lose bits between my ears during the workweek! Thanks again for you help. t. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tom Cloyd, MS MA, LMHC Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << tc / tomcloyd.com >> (email) << TomCloyd.com >> (website & psychotherapy weblog) << sleightmind.wordpress.com >> (mental health issues weblog) << directpathdesign.com >> (web site design & consultation) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~