我只是试图让填充与对象的数组的功能,但什么是错的:简单数组赋值
row1 = []
class Tile
def initialize(type)
@type = type
end
end
def FillRow1
[1..10].each {
random = rand(1..3)
if random == 1 row1.(Tile.new("land") end
else if random == 2 row1.(Tile.new("Water") end
else ifrandom == 3 row1.(Tile.new("empty") end
}
row1
end
所以 “<<” 将对象添加到阵列?我可以直接在数组上使用rand? –
是的,'<<'是一个重载操作符,基本上是'push'的语法糖。我们没有在数组上使用rand,而是只使用了结果,rand(x..y)返回一个数字,我们用它作为索引来获取数组'types'的相应项。 – Need4Steed