I'm having serious issues with my Windows 10, black screen only, had to go back to a system recovery point, but even then I had that black screen, and only after switching off 2 times, and hard reset 6 times eventually the desktop appeared. So, if I disappear from the internet for a while, you know why.I addition to Trog, always keep in mind, that in Ruby everything is an object. In fact, Arrays never store values (no numbers or letters), they store objects (and not even that, they store references to those objects, but let's not get too deep)
But what is an array? Exactly, an object. So, if you point to an array, the object "array" will be stored, not the contents of that array.
The "splatting", Trog mentioned is caused by the use of a so called splat operator. In other functional languages this is known as "destructuring assignment".
Code: Select all
a = Array.new
a[0] = 1
a[1] = @f3[2, 3]
watch a
Replace your code with above one, then have a look at the info pane. Do you recognize it? First of all, there are array entries although we create an empty array. This is, because it will auto-generate the array structure. You don't need to define a number of entries when creating an array. If you do
it will generate 100 entries, with the last one being set the object 1. All others will be set to the object nil. That's ok though for your float array, as nil is converted to 0 for green floats/float arrays.
Secondly, you only have 2 entries. The numbers after 1 are enclosed in square brackets, which means they are an array. This array is stored at a[1]. Thirdly, it is not the full array of f3, but only 3 numbers.
The conclusion is, that Ruby looks at your code, recognizes a range for f3, and creates a new array that is filled with the objects at index 2, 3, and 4. This new array is then stored at index 1 in array a.
This is also true for a. Something like "watch a[2..4]" would show an array with the contents of a from index 2 to index 4. But then you are assigning objects to this range you created. At this point Ruby notices that you don't want an array returned, but fill a slice of the array. Now it goes into a structuring mode.
Try this. You will notice that the array now reads [1, 2, 6]
Ruby noticed that you wanted to replace index 2, 3 and 4, but you only assigned one object. So it sets index 2, but since the other two are missing, they are deleted from the original array. Change the assignment line to
Now all three entries from the original array are replaced.
But what if you assign more than you reserved entries for?
Well, as I said above, an array is auto-generating entries that are needed. Ruby fills the three entries you defined, but now there are still three objects left over. It generates entries and inserts them after the last defined one (here index 4). Index 5 gets the object 6, etc.
If you read closely, you noticed that I said "insert". To prove this, use this code
Code: Select all
a = Array.new
a = [1, 2, 3, 4, 5, nil]
a[2..4] = 6, 6, 6, 6, 6, 6
watch a
The last entry of the original array is "nil". And so it is after we made our assignments! All the sixes are inserted.
Other languages, like C, or Basic, etc., will throw an error instead of just generating new entries. I'm not sure what I prefer.