array addressing problem

T

Tom Cloyd

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[0], delta[1] ) <= in an attempt
isolate error, I broke this into the following:
s1 = delta[0] # <= exception is thrown here
s2 = delta[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?

All help will be gratefully accepted.

Tom

--
 
D

David A. Black

Hi --

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[0], delta[1] ) <= in an attempt
isolate error, I broke this into the following:
s1 = delta[0] # <= exception is thrown here
s2 = delta[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:
?> ["</p>", ''],
?> [ said:
ld = delta.length => 2
i = 0 => 0
until i > ld
s1 = delta[0] # <= exception is thrown here
s2 = delta[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
 
T

Tom Cloyd

David said:
Hi --

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[0], delta[1] ) <= in an attempt
isolate error, I broke this into the following:
s1 = delta[0] # <= exception is thrown here
s2 = delta[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 = [
?> [ said:
ld = delta.length => 2
i = 0 => 0
until i > ld
s1 = delta[0] # <= exception is thrown here
s2 = delta[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
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top