arrays - 空白数组在填充值时表现异常,具体取决于它的创建方式(Ruby)
问题描述
我有一个迭代填充的数组数组,但由于某种原因,它的定义方式似乎正在改变它的行为,即使它的结果等于其他方法的结果。所以这里是代码:
@height = 5
@nums = (1..60).to_a
@grid = []
@height.times do # Method one for making the grid
@grid << []
end
print @grid, "\n" # Print the grid, shows [[], [], [], [], []] as expected
@grid2 = Array.new(@height, []) # Cleaner way of making the grid
print @grid2, "\n" # Print the grid, shows [[], [], [], [], []] as expected
print @grid2 == @grid, "\n" # Are they equal? returns true as expected
@grid = @grid2 # Commenting out this line changes the behavior of the output
@nums.length.times do |n| # Fill the grid with our numbers
@grid[n % @height] << @nums[n]
end
@grid.each do |i| # Display the grid
print i, "\n"
end
解决方案
推荐阅读
- python - 如何在我的索引视图中显示购物车项目?
- javascript - 根据字符串包含的内容隐藏和显示 HTML 元素
- javascript - 反映已编译的 TypeScript 文件
- c# - 目标路径“”是目录而不是文件
- java - Spring Kafka 重试未按预期工作
- sas - SAS 连续观察中的两个平均值 - 考虑到下降时间变化的额外小时
- visual-studio - 如何修复 nuget 包管理器不起作用?
- flutter - 颤振新项目创建后,我找不到主 dart 文件开始编程
- datetime - 如何让 Orange 从 Excel 文件中识别日期时间?
- neural-network - Z3 / OCaml 绑定未考虑变量边界