# Array addressing problem

**URL:** <https://www.ruby-forum.com/t/array-addressing-problem/140556>\
**Category:** Ruby\
**Created:** [June 16, 2008, 8:22am UTC](https://www.ruby-forum.com/t/array-addressing-problem/140556 "2008-06-16T08:22:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [June 16, 2008, 8:22am UTC](https://www.ruby-forum.com/t/array-addressing-problem/140556/1 "2008-06-16T08:22:52Z")

</div>

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 = [  
["

", ‘’],  
["", ‘’]] # 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?

All help will be gratefully accepted.

Tom

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [June 16, 2008, 8:47am UTC](https://www.ruby-forum.com/t/array-addressing-problem/140556/2 "2008-06-16T08:47:32Z")

</div>

Hi –

On Mon, 16 Jun 2008, Tom C. wrote:

> # output
> 
> s2 = delta[i][1]
> 
> 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 = [  
> > ?\> ["
> > 
> > ", ‘’],  
> > ?\> ["", ‘’]] #  
> > =\> [["", “”], ["", “”]]
> 
> > 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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [June 16, 2008, 9:02am UTC](https://www.ruby-forum.com/t/array-addressing-problem/140556/3 "2008-06-16T09:02:47Z")

</div>

David A. Black wrote:

> > to a textile marked up file):  
> > ld = delta.length
> 
> > > s1 = delta[i][0] # \<= exception is thrown here  
> > > 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.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```
