# Beginner's question on methods

**URL:** <https://www.ruby-forum.com/t/beginners-question-on-methods/107585>\
**Category:** Ruby\
**Created:** [June 23, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/beginners-question-on-methods/107585 "2007-06-23T18:26:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [June 23, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/beginners-question-on-methods/107585/1 "2007-06-23T18:26:33Z")

</div>

I’m new to Ruby, having worked with Perl the past couple of years. I’m  
having trouble calling a method to increment a counter, and I’m assuming  
that the problem is with the scope of the variables?

I’m getting the following error message:  
P:/PETERV2/scripts/vlog2.rb:25:in `test_meth1': undefined method`+’ for  
nil:NilClass (NoMethodError)  
from P:/PETERV2/scripts/vlog2.rb:47  
from P:/PETERV2/scripts/vlog2.rb:36:in `each' from P:/PETERV2/scripts/vlog2.rb:36 from P:/PETERV2/scripts/vlog2.rb:7:in`open’  
from P:/PETERV2/scripts/vlog2.rb:7

This is the method I’m trying to call:  
def test\_meth1  
counter = counter + 1  
return counter  
end

This is the call:

```
      case typrec # case statement

```

example.  
when “TR001”  
cntr = test\_meth1  
puts cntr # Here I want to print the counter value.  
when “AB001”  
ab001 += 1  
when “AI001”  
ai001 += 1  
else  
puts “Invalid message type”  
end

I assume that there’s a really simple mistake I’m making, so any help  
would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [June 23, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/beginners-question-on-methods/107585/2 "2007-06-23T18:39:22Z")

</div>

Peter V. wrote:

> from P:/PETERV2/scripts/vlog2.rb:7:in `open’  
> case typrec # case statement  
> end
> 
> I assume that there’s a really simple mistake I’m making, so any help  
> would be greatly appreciated.

You never initialize the ‘counter’ variable so it’s default value is  
nil. Ruby is telling you that there is no “+” method for a nil object,  
that is, you can’t add 1 to nil.

If “test\_meth1” were in a class, you could make counter be an instance  
variable and initialize it in the initialize method. Since it’s not,  
it’ll have to be a global variable. Global variable names start with $.

$counter = 0  
def test  
$counter = $counter + 1  
return $counter  
end

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [June 23, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/beginners-question-on-methods/107585/3 "2007-06-23T21:44:53Z")

</div>

Tim H. wrote:

> Peter V. wrote:
> 
> > from P:/PETERV2/scripts/vlog2.rb:7:in `open’  
> > case typrec # case statement  
> > end
> > 
> > I assume that there’s a really simple mistake I’m making, so any help  
> > would be greatly appreciated.
> 
> You never initialize the ‘counter’ variable so it’s default value is  
> nil. Ruby is telling you that there is no “+” method for a nil object,  
> that is, you can’t add 1 to nil.
> 
> If “test\_meth1” were in a class, you could make counter be an instance  
> variable and initialize it in the initialize method. Since it’s not,  
> it’ll have to be a global variable. Global variable names start with $.
> 
> $counter = 0  
> def test  
> $counter = $counter + 1  
> return $counter  
> end

Tim,  
Thanks very much for the response! I’ve got it working now.  
Peter V.
