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


Mark Liang wrote:

>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[i].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][i] }
>     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)
>
>
>
>  
>