# What the...?

**URL:** <https://rubytalk.org/t/what-the/2819>\
**Category:** ruby-talk\
**Created:** [11 November 2002 20:47 UTC](https://rubytalk.org/t/what-the/2819 "2002-11-11T20:47:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tokikenshi](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@Tokikenshi](https://rubytalk.org/u/Tokikenshi)\
**Post date:** [11 November 2002 20:47 UTC](https://rubytalk.org/t/what-the/2819/1 "2002-11-11T20:47:02Z")

</div>

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?

-johan

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [11 November 2002 20:47 UTC](https://rubytalk.org/t/what-the/2819/2 "2002-11-11T20:47:09Z")

</div>

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/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 5. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [11 November 2002 20:55 UTC](https://rubytalk.org/t/what-the/2819/3 "2002-11-11T20:55:26Z")

</div>

Easy one; when you do points[i].x=… you’re calling the ‘x=’ method of  
the points[i] object, which happens to be nil, for points[1] == nil.  
You’re thinking in C++ or Java; Ruby uses accessors.

> **···**
>
> On Tue, Nov 12, 2002 at 05:47:02AM +0900, 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?
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Whoa…I did a ‘zcat /vmlinuz \> /dev/audio’ and I think I heard God…  
> – mikecd on #Linux

---

<div class="post-metadata">

**Author:** ![Tokikenshi](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@Tokikenshi](https://rubytalk.org/u/Tokikenshi)\
**Post date:** [12 November 2002 15:49 UTC](https://rubytalk.org/t/what-the/2819/4 "2002-11-12T15:49:50Z")

</div>

Allright. Thanks a million everybody. 🙂

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)\
**Post date:** [11 November 2002 20:55 UTC](https://rubytalk.org/t/what-the/2819/5 "2002-11-11T20:55:50Z")

</div>

Actually, becuase he’s manually handling index 0, he would want:

1.upto(points.length - 1) { |i| … }

-austin  
– Austin Ziegler, austin@halostatue.ca on 2002.11.11 at 15.54.29

> **···**
>
> On Tue, 12 Nov 2002 05:47:09 +0900, Kent Dahl wrote:
> 
> > 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.
