小编Y.Z*_*Y.Z的帖子

Ruby:为什么所有坐标都在一个数组中更新?

我正在尝试更改特定的坐标,但是数组正在更新所有坐标。

目标是将fixed属性更改为单个坐标。

class Case
  attr_accessor :fixed

  def initialize
    self.fixed = false
  end

  def fixed?
    !!fixed
  end
end

def display(arr)
  5.times do |x|
    5.times do |y|
      print arr[x][y].fixed?
      print ' '
    end

    puts
  end
end

# Defining array
arr = Array.new(5){ Array.new(5, Case.new) }

# Displaying the arrays
display(arr)

# Changing value of a single coord
arr[2][3].fixed = true

# Displaying the arrays
display(arr)

Run Code Online (Sandbox Code Playgroud)

这是第一个显示呼叫的结果

false false false false false 
false false false false false 
false false false false …
Run Code Online (Sandbox Code Playgroud)

ruby

2
推荐指数
1
解决办法
45
查看次数

查找数组中零子数组的数量

我需要找到一个数组中零子数组的数量:

array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)

结果应该是:3因为我们有0, 000, 0, 0

计算零(6)的数量将无效。

ruby algorithm

-3
推荐指数
1
解决办法
103
查看次数

标签 统计

ruby ×2

algorithm ×1