[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginner's question on methods

Peter Vanderhaden

6/23/2007 4:28:00 PM

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.

--
Posted via http://www.ruby-....

2 Answers

Tim Hunter

6/23/2007 4:38:00 PM

0

Peter Vanderhaden wrote:
> 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.
>
>
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


--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Peter Vanderhaden

6/23/2007 7:45:00 PM

0

Tim Hunter wrote:
> Peter Vanderhaden 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.

--
Posted via http://www.ruby-....