Justin Collins
2/24/2009 12:07:00 PM
Dave Lilley wrote:
> Hi I've got this misunderstanding of how to use arrays and objects
> stored there in (note I'm actually using Shoes if this has any bearing
> on my problem).
>
> below is a bit of code i've written that creates an array of 25 places.
> @myarray = Array(1..25)
>
This is not necessary, the array will grow as you add things to it.
Given the code you have shown, you can just as well do:
@myarray = []
> I then want to "install" a bona fide object like a shape into each 25
> places.
> so i do this...
> 1.upto @myarray.length do |p|
>
Remember, Ruby arrays are 0-based, so you array of 25 items is indexed 0
to 24. Using 'p' as a variable name is not recommended, because 'p' is
also a built-in method.
Also, a more common way to do this (and noticing the array is now empty
if defined as above) would be
(0..25).each do |p|
> @myarray[p] = oval(0,0,20,20)
> @myarray[p].move = x,y
>
I think you mean to do
@myarray[p].move x, y
> note: x & y being defined else where so each shape is visable on screen
>
> or can it be @myarray[p] = oval(0,0,20,20).move x,y?
>
This would work, too, though you may need to do
@myarray[p] = oval(0,0,20,20).move(x,y)
-Justin