# Simple program with a simple(?) error -- what's wrong?

**URL:** <https://www.ruby-forum.com/t/simple-program-with-a-simple-error-whats-wrong/143538>\
**Category:** Ruby\
**Created:** [July 20, 2008, 5:45am UTC](https://www.ruby-forum.com/t/simple-program-with-a-simple-error-whats-wrong/143538 "2008-07-20T05:45:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 20, 2008, 5:45am UTC](https://www.ruby-forum.com/t/simple-program-with-a-simple-error-whats-wrong/143538/1 "2008-07-20T05:45:46Z")

</div>

Hi,

I put together to exercise simple Ruby constructs. It’s posted at  
[http://www.pastie.org/237288](http://www.pastie.org/237288).

Before I introduced the “indx” variable it worked fine. The statement  
incrementing indx in the def causes the error:  
undefined method `+’ for nil:NilClass (NoMethodError)

I’m running:  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Why doesn’t the def use the instance of indx defined before the def?

Thanks in Advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 20, 2008, 9:12am UTC](https://www.ruby-forum.com/t/simple-program-with-a-simple-error-whats-wrong/143538/2 "2008-07-20T09:12:05Z")

</div>

The scope of indx is wrong, when you call it outside the function, it is  
only accesible witin that scope, the outer scope and so you have you do  
this

def foo  
indx=0  
…  
end

that will then work, you only need to call foo someplace if you want foo  
to  
run. You only define it, creating the function does not call it.

On Sat, Jul 19, 2008 at 10:39 PM, RichardOnRails \<

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 20, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/simple-program-with-a-simple-error-whats-wrong/143538/3 "2008-07-20T22:40:54Z")

</div>

reuben doetsch wrote:

> The scope of indx is wrong, when you call it outside the function, it is  
> only accesible witin that scope, the outer scope and so you have you do  
> this
> 
> def foo  
> indx=0  
> …  
> end

(…)  
Or replace “indx” with “@indx”.  
Regards,  
Siep

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 21, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/simple-program-with-a-simple-error-whats-wrong/143538/4 "2008-07-21T15:30:54Z")

</div>

On Jul 20, 4:35 pm, Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (…)  
> Or replace “indx” with “@indx”.  
> Regards,  
> Siep
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Thanks Reuben and Siep for you responses.

The following worked perfectly for creating sequentially numbered  
lines: as “def” was invoked successively with various arguments:

@indx=0  
def disp(s)  
@index += 1  
printf( [snip]

However, “index=0” inside the def, despite subsequent incrementing,  
had the dual faults of assigning the same index to every line and  
being inaccessible outside the “def”.

I had also tried “if !defined?(indx)” somehow, but that was useless  
across multiple invocations of “def”.

Again, thanks to you both for your responses.
