在Ruby中,我可以限制对物体在IRB中表现出来或使用.MIST.
题
我正在写一个用于解决Sudoku难题的课程,该拼图包含两个维度的阵列,其中包含指针 Cells
这一点回到了这两个维数阵列。这样的事情:
def class Sudoku
attr :rows, :columns, :blocks
def initialize
# build each of the rows, columns, and blocks with a 9x9 map of Cells
end
end
def class Cell
attr :value, :row, :column, :block
def initialize(row, column, block, value)
# set each pointer to its parent row, column and block etc
end
end
问题是当我做类似的事情时:
p = Puzzle.new
在 irb
, ,IRB冻结。我现在已经修改了一些代码,所以它不行,但是现在如果我这样做:
irb> p.rows
=> TONS OF SHIT GETS RETURNED
它输出了大量的嵌套指针,大约需要20秒才能返回到 irb
迅速的。其中很多与一些无限指针有关:
p.rows[0][0].row[0].row[0].row[0]....
因此,我想知道Ruby是否有一种方法可以返回该数组的浅色表示,因为它的所有指针最终都指向自身。
解决方案
重新定义拼图检查并仅显示您想要的内容。
例如:
def inspect
"Puzzle with size #{rows.size}"
end
不隶属于 StackOverflow