From: Thufir Date: 2007-11-11T06:00:07+09:00 Subject: Re: undefined local variable or method (filling an array) On Nov 9, 4:43 pm, 7stud -- wrote: > Thufir wrote: > > I've been working on this for quite some time and don't understand the > > error: > > > C:/code/monsters.rb:6: undefined local variable or method `monsters' > > for main:Ob > > ject (NameError) > > my_program.rb: > > monsters[0] = 10 > > --output:-- > undefined local variable or method `monsters' for main:Object > (NameError) > > Normally, variables spring into existence when you assign to them. > However, the statement above works a little differently. The characters > '[]=' are actually the name of a method in the Array class. That means > the the code above is using the variable monsters to call that method. > However, in order to call a method of the Array class, you need an > existing array instance. But, nowhere in the code is an array > previously assign to the variable monsters. > > Try this instead: > > my_program.rb: > > monsters = [] > monsters[0] = 10 > -- > Posted viahttp://www.ruby-forum.com/. Gee, it was so weird and frustrating. Thank you all! -Thufir