Undefined method

M

Mark Liang

I've recently started learning Ruby, I cannot seem to locate the
cause of this error. Please pardon me if the answer is more
straightforward as it seems.

I've double-checked, blkcheck method works fine. I suspect the
problem lies with line 32, Blk.each ..., however, I don't see why
a problem should be there. Please help shed some light.

The error message:

../creat.rb:25:in `blkcheck': undefined method `[]' for nil:NilClass
(NoMethodError)
from ./creat.rb:25:in `times'
from ./creat.rb:25:in `blkcheck'
from ./creat.rb:24:in `times'
from ./creat.rb:24:in `blkcheck'
from ./creat.rb:33:in `blkscheck'
from ./creat.rb:32:in `each'
from ./creat.rb:32:in `blkscheck'
from ./creat.rb:45


The src:

1 #!/usr/bin/ruby
2
3 Val = [1, 2, 3, 4, 5, 6, 7, 8, 9]
4 Blk = [[0,0],[0,3],[0,6],[3,0],[3,3],[3,6],[6,0],[6,3],[6,6]]
5
6 def linescheck (tab)
7 num = 0
8 9.times { |i| num += 1 if tab.sort == Val }
9 1 if num == 9
10 end
11
12 def colscheck (tab)
13 num = 0
14 9.times do |i|
15 tmp = []
16 9.times { |j| tmp <<= tab[j] }
17 num += 1 if tmp.sort == Val
18 end
19 1 if num == 9
20 end
21
22 def blkcheck (tab, i, j)
23 tmp = []
24 3.times do |k|
25 3.times { |l| tmp <<= tab[i+k+l][j+k] }
26 end
27 1 if tmp.sort == Val
28 end
29
30 def blkscheck (tab)
31 num = 0
32 Blk.each do |i,j|
33 num += 1 if blkcheck(tab,i,j)
34 end
35 1 if num == 9
36 end
37
38 # Create the table of values
39 val = []
40 9.times do |i|
41 val <<= Val
42 end
43
44 #blkscheck(val)
45 print "Hello world!\n" unless blkscheck(val)
 
D

dblack

Hi --

I've recently started learning Ruby, I cannot seem to locate the
cause of this error. Please pardon me if the answer is more
straightforward as it seems.

I've double-checked, blkcheck method works fine. I suspect the
problem lies with line 32, Blk.each ..., however, I don't see why
a problem should be there. Please help shed some light.

The error message:

../creat.rb:25:in `blkcheck': undefined method `[]' for nil:NilClass
(NoMethodError)
from ./creat.rb:25:in `times'
from ./creat.rb:25:in `blkcheck'
from ./creat.rb:24:in `times'
from ./creat.rb:24:in `blkcheck'
from ./creat.rb:33:in `blkscheck'
from ./creat.rb:32:in `each'
from ./creat.rb:32:in `blkscheck'
from ./creat.rb:45


The src:

1 #!/usr/bin/ruby
2
3 Val = [1, 2, 3, 4, 5, 6, 7, 8, 9]
4 Blk = [[0,0],[0,3],[0,6],[3,0],[3,3],[3,6],[6,0],[6,3],[6,6]]
5
6 def linescheck (tab)
7 num = 0
8 9.times { |i| num += 1 if tab.sort == Val }
9 1 if num == 9
10 end
11
12 def colscheck (tab)
13 num = 0
14 9.times do |i|
15 tmp = []
16 9.times { |j| tmp <<= tab[j] }
17 num += 1 if tmp.sort == Val
18 end
19 1 if num == 9


I'm not sure why you would need 1 there. If you're just testing the
truth value, then you'd want simply:

num == 9

If you need to return an integer value then be warned that if the
condition fails, you'll be returning a non-integer, namely nil.
20 end
21
22 def blkcheck (tab, i, j)
23 tmp = []
24 3.times do |k|
25 3.times { |l| tmp <<= tab[i+k+l][j+k] }
26 end
27 1 if tmp.sort == Val
28 end
29
30 def blkscheck (tab)
31 num = 0
32 Blk.each do |i,j|
33 num += 1 if blkcheck(tab,i,j)

On the third iteration, j is 6. That means that in blkcheck, i+k+j is
eventually going to add up to 9. Since tab only has nine elements,
there's no tab[9]. tab[9] is nil, so when you do tab[9][j+k] you're
calling [] on nil.
34 end
35 1 if num == 9
36 end
37
38 # Create the table of values
39 val = []
40 9.times do |i|
41 val <<= Val
42 end
43
44 #blkscheck(val)
45 print "Hello world!\n" unless blkscheck(val)

Discover the joys of 'puts' :)


David
 
J

Johannes Friestad

The simplest way to debug something like this is to add a few print
statements so you see which values are involved.

The error
/creat.rb:25:in `blkcheck': undefined method `[]' for nil:NilClass
(NoMethodError)
means that your line 25 has just called '[]' on nil.

So in line 25
3.times { |l| tmp <<=3D tab[i+k+l][j+k] }
either "tab" or "tab[i+k+l]" is nil. But you don't know which, or why.

By adding a couple of print statements to your method, like this
--------
def blkcheck (tab, i, j)
tmp =3D []
printf("input: i=3D%i, j=3D%i, tab(length=3D%i)=3D%s\n", i, j, tab.lengt=
h,
tab.inspect)
3.times do |k|
3.times { |l|
e1=3Dtab[i+k+l]
e2=3De1[j+k] if e1
printf("tab[%i]=3D%s, tab[%i][%i]=3D%s\n", i+k+l, e1.inspect,
i+k+l, j+k, e2.inspect)
tmp <<=3D tab[i+k+l][j+k]
}
end
1 if tmp.sort =3D=3D Val
end
--------

you'll get a printout like this
------
input: i=3D6, j=3D0, tab(length=3D9)=3D[[1, 2, 3, ...]....]
tab[6]=3D[1, 2, 3, 4, 5, 6, 7, 8, 9], tab[6][0]=3D1
tab[7]=3D[1, 2, 3, 4, 5, 6, 7, 8, 9], tab[7][0]=3D1
tab[8]=3D[1, 2, 3, 4, 5, 6, 7, 8, 9], tab[8][0]=3D1
tab[7]=3D[1, 2, 3, 4, 5, 6, 7, 8, 9], tab[7][1]=3D2
tab[8]=3D[1, 2, 3, 4, 5, 6, 7, 8, 9], tab[8][1]=3D2
tab[9]=3Dnil, tab[9][1]=3Dnil
NoMethodError: undefined method `[]' for nil:NilClass
-----

and it's easy to see that the nil error comes from calling tab[9] on
an array of length 9.

Good luck with your future debugging :)

Hi --

I've recently started learning Ruby, I cannot seem to locate the
cause of this error. Please pardon me if the answer is more
straightforward as it seems.

../creat.rb:25:in `blkcheck': undefined method `[]' for nil:NilClass
(NoMethodError)
from ./creat.rb:25:in `times'
from ./creat.rb:25:in `blkcheck'
from ./creat.rb:24:in `times'
from ./creat.rb:24:in `blkcheck'
from ./creat.rb:33:in `blkscheck'
from ./creat.rb:32:in `each'
from ./creat.rb:32:in `blkscheck'
from ./creat.rb:45
 
T

Tom Reilly

You could also define [] for the nil class and have it return whatever
you thought appropriate. For example:

class NilClass
def []
nil #or whatever you want returned
end
end
 
J

Jacob Fugal

You could also define [] for the nil class and have it return whatever
you thought appropriate. For example:

class NilClass
def []
nil #or whatever you want returned
end
end

No, no, NO! See, this situation is exactly why I *wouldn't* want
Ruby's nil to follow the NilObject pattern (or whatever it's called).
If it did, Mark would be getting wrong results as opposed to the
undefined method exception. Which do you think is easier to debug?
I'll take the exception that tells me what line number, any day.

Jacob Fugal
 
D

Damphyr

Jacob said:
You could also define [] for the nil class and have it return whatever
you thought appropriate. For example:

class NilClass
def []
nil #or whatever you want returned
end
end


No, no, NO! See, this situation is exactly why I *wouldn't* want
Ruby's nil to follow the NilObject pattern (or whatever it's called).
If it did, Mark would be getting wrong results as opposed to the
undefined method exception. Which do you think is easier to debug?
I'll take the exception that tells me what line number, any day.
I'll second that and quote Dave and Andy:
"Crash Early"
V.-

--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top