[Wtr-general] undefined method `[]' for nil:NilClass (NoMethodError) error at the end of array???

Paul Carvalho tester.paul at gmail.com
Tue Jan 23 11:16:27 EST 2007


This is one of those times when I'll never understand why some things in
programming start counting at 1 and some things start counting at 0.

I, too, have several similar loops in some of my scripts, but I opted for
the more readable format of saying:

t.length.times { |x|
    puts t[x][0]
}

I just can't be bothered with loops like "1.upto(t.length) {|x| puts x}"
which, technically, counts from the "first item" to the "last item", but
really x starts counting at 0 and goes to (length - 1).  This might be
convenient if you are working with arrays but not a whole lot else.

Paul C.



On 23/01/07, Chris McMahon <christopher.mcmahon at gmail.com> wrote:
>
> On 1/22/07, mi <mdove at pheedo.com> wrote:
> > t = [["a", "b"], ["aa", "bb"]]
> >
> > 0.upto (t.length) { |x|
> >     puts t[x][0]
> > }
> >
> > For some reason i'm getting the following error at the end of the loop,
> > any idea WHY???
>
> 0.upto (t.length-1) { |x|
>      puts t[x][0]
> }
>
> should do it, but I think you figured that out.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/wtr-general/attachments/20070123/ffabe26a/attachment.html 


More information about the Wtr-general mailing list