No implicit conversion

First my code (in Ace): 

g = 6 @t = [] for i in 0..g-1 for j in 0..g-1 @t[[j]] = Sprite.new() # line of the error @t[[j]].bitmap = Cache.system("dirt0") @t[[j]].z = 150 @t[[j]].x = 16*i @t[[j]].y = 16*j end endMy goal here is to create a simple grid of dirt0 sprites (this runs in a Window_Base).  I've used this same logic process several times in other engines so thats not what I'm worried about, nor is it the problem.  The problem I run into is when we reach the Sprite.new line and it throws a "no implicit conversion from nil to integer" error.  This also occurs when I attempt to store any other type in the array.

I can store whatever I need outside the for loops, which leads me to believe that they are part of the issue but I am not sure how.

I'm no Ruby master but in no case do I find myself calling for a conversion or anything else suggested by the message which adds to my confusion.
So let's break down the line that has the error.

# The line with the error@t[[j]] = Sprite.new()# This line has essentially two parts - left of the equal sign and right of the equal sign.# You're telling Ruby to assign whatever is on the right to whatever is on the left.# Left side right side@t[[j]] = Sprite.new()# There's only one method call on the right, so let's start there.# The method evaluates and returns a Sprite object# Sprite.new() => A_Sprite_Object# The left side is a bit more complicated, so we'll work out side in.# Regardless of iteration (values of i and j), the first call is to the array.@t[ ]# This method says, 'get a location in the array'. It requires an integer, so whatever we pass# to is needs to be an integer. So what do we pass to it?[j]# You might be tempted to think you're passing it two integers, but actually you're passing it# an array and an integer. # an array with one value[j] # a method call on the array with an integer.# So let's run through some values of i and j.# 0,0@t[[0][0]] # get the zero position in array [0]=> @t[0] # get the zero position in array @t=> valid position# 0,1@t[[0][1]] # get the one position in array [0]=> @t[nil] # get the nil position in array @t=> Error "No implicit conversion from nil to integer"So ultimately, you're unintentionally passing 'nil' to a method that requires an integer. Normally, Ruby will try to help you out by converting your data types when it can but it can't convert nil into an integer.

I would recommend you use RGSS3's built in Table Class. You could rewrite your code as this.

Code:
g = 5           #Table.new(x_size, y_size)@my_table = Table.new(5 , 5) # x_size is 5, y_size is 5for i in 0..g  for j in 0..g    # use @my_table[x, y] to get/set position    @my_table[i, j] = Sprite.new()  endend
Thanks for the detailed explanation Mobius.  Two items, Table was my first and preferred method of handling things, and had done it the same way as you have written.  Problem is RGSS3's table class only accepts short integers, not sprites, thus why I had to fall back onto the 2d array method of things.

While I believe that I understand your explanation I am unsure of how to use it to solve the issue or use a different method to achieve the same result.
Thanks for the detailed explanation Mobius.  Two items, Table was my first and preferred method of handling things, and had done it the same way as you have written.  Problem is RGSS3's table class only accepts short integers, not sprites, thus why I had to fall back onto the 2d array method of things.

While I believe that I understand your explanation I am unsure of how to use it to solve the issue or use a different method to achieve the same result.
Interesting. I've never had a reason to use the Table class myself so I had always assumed that it could hold any objects. Well in any event, there's a few more options that I can think of.

Option 1.

# Create a "2D" array by making an array of arrays.# You can do this explicitly like this:array = [ [], [], [], [], [] ] # This would have an "x" size of 5# You can also do it a little more quickly using Array.new(size)array = Array.new(5)array.collect! { |item| item = Array.new(5) }# This array will be created with 5 empty positions which we can then fill with empty arrays# also of length 5 using the method 'collect!'# You could then reference the positions like this:array[x][y]# Note that this is pretty similar to what you were trying to do earlier.Option 2.

# If the previous option seems kind of difficult to keep track of then we can make our own# "Table" class to handle the details. This is often called a 'wrapper'.class MyTable # Set up array automatically based on desired sizes # rather than having to manually do it each time def initialize(x_size = 0, y_size = 0) @data = Array.new(x_size) @data.collect! { |item| item = Array.new(y_size) } end # This method allows us to reference locations using "table[x, y]" def [](x,y) return @data[x][y] end # This method allows us to set locations using "table[x, y] = value" def []=(x,y,value) @data[x][y] = value end # Get the x_size of the table def x_size return @data.size end # Get the y_size of the table def y_size return @data[0].size end # A method for filling the table with some test data def test_fill for i in 0...self.x_size for j in 0...self.y_size self[i, j] = "#{i}, #{j}" end end endendYou could do a few more things with the wrapper class but that's the core functionality.
Option 2 is what I am going with, thanks!  To often I find myself reliant on the RGSS3 classes that I forget I can do stuff like this.  I already know additional options provided me by using/making my own class for this.  

I'm more than satisfied with the education/solution, I would consider this closed.
Cool! Glad I could help.

Original thread: https://forums.rpgmakerweb.com/threads/no-implicit-conversion.44912/