From: Kent Dahl Date: 2002-11-12T05:47:09+09:00 Subject: Re: What the...? Johan Persson wrote: > > Take a look on the snippet below > > # points is an array of structure-objects defining a point > points[0].x = 5 > points[0].y = 4 > 1.upto(points.length) { |i| > points[i].x = points[i-1].x > points[i].y = points[i-1].y > } > > Now that looks like a legal bunch of statements, right? The thing is > every time Ruby interpret the line 'points[i].x = points[i-1].x' it > stops, giving me nothing but a cryptic error message, 'undefined > method `x=' for nil (NameError)'. > Could someone please tell me what's going on? Array indexing starts at 0. 1.upto iterates _including_ points.length, which doesn't exist, meaning [] returns nil... and you can guess the rest. Try 0.upto(points.length-1) { |i| instead. -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 5. year ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)