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.