6 Replies - 2042 Views - Last Post: 11 February 2010 - 07:57 PM Rate Topic: -----

#1 Adare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-June 09

why is my method undefined?

Posted 11 February 2010 - 05:20 PM

ruby counting.rb
testcounting.rb:8:in `counting': undefined method `+' for nil:NilClass (NoMethodError)
from counting.rb:12:in `<main>'


I am not sure why it doesn't recognize it but I'm coming from C++ so I am completely new at this.

Here is my code:

def counting()
	print "test"
        x += 1
	puts x
end
	
counting()
counting()
counting()
counting()
counting()


thx

Is This A Good Question/Topic? 0
  • +

Replies To: why is my method undefined?

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: why is my method undefined?

Posted 11 February 2010 - 05:24 PM

It isn't the 'counting' method which is undefined, but trying to use '+' on a nil

'x' isn't defined as a variable, so it has the value nil, and you cannot add 1 to nil, as you try to there. Give x an initial value (probably 0).

Hopefully that made sense :)
Was This Post Helpful? 0
  • +
  • -

#3 Skaggles   User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 255
  • View blog
  • Posts: 641
  • Joined: 01-March 09

Re: why is my method undefined?

Posted 11 February 2010 - 05:33 PM

Exactly what erik.price said. Everytime you call the 'counting' method, the 'x' variable will be set to nil, since you're not passing it an initial value.

If you're just trying to loop through a count you can try using something like:

3.times do |x|
  puts "Test " + x.to_s
end



Hope this helped.
Was This Post Helpful? 0
  • +
  • -

#4 Adare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-June 09

Re: why is my method undefined?

Posted 11 February 2010 - 05:34 PM

I got it work but not do as I would like.

I get the results:
 ruby counting.rb
1
1
1
1
1



I need it to print

1
2
3
4
5

How do I do that without initializing x every time I call 'counting'.

I currently have:

def counting()
	x = 0
        x += 1
	puts x
end

	
counting()
counting()
counting()
counting()
counting()



thx
Was This Post Helpful? 0
  • +
  • -

#5 Skaggles   User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 255
  • View blog
  • Posts: 641
  • Joined: 01-March 09

Re: why is my method undefined?

Posted 11 February 2010 - 05:48 PM

This won't work because you're declaring X = 0 within every method call. So the count will always start over on each call to counting. What you can try is to declare X = 0 before calling the method, and update it on each method call. This should acheive the affect you're looking for:

def counting(x)
	puts "Test " + x.to_s
	x += 1
end

x = 1
x = counting(x)
x = counting(x)
x = counting(x)


Was This Post Helpful? 0
  • +
  • -

#6 Adare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-June 09

Re: why is my method undefined?

Posted 11 February 2010 - 07:53 PM

thx. this all helped. ended up using a class but the error message was cryptic to me. I'm used to C like errors.
Was This Post Helpful? 0
  • +
  • -

#7 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: why is my method undefined?

Posted 11 February 2010 - 07:57 PM

I've always found the opposite to be true: Ruby errors being nice and easy compared to C. But to each his own I guess :P

Good job figuring it out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1