Ruby:OOP&双暗阵列问题

时间:2009-11-12 08:58:35

标签: ruby arrays oop inheritance

我需要创建一个二维数组Class。我已经完成了一项工作,但发现我的班级只有一个内部的双暗阵列,要访问这些元素,我必须写一个冗余的单词'table':

class Table
  attr_accessor :table
  def initialize(w,h)
    @table = Array.new(h)
    h.times do @table << Array.new(w) 
  end
end

x = Table.new(10,10)
x.table[5][6] = 'example'

等等。我的想法是,我想一次只写x[5][6]来访问元素。据我所知,我必须继承Array类,并以某种方式扩展它以表现为两个昏暗的数组。如果我是对的 - 我该怎么做?

3 个答案:

答案 0 :(得分:1)

认为这可能就是你要找的东西。它使用@table实例变量来跟踪内部数组,但不公开它的访问器。这是定义:

class Table

  def initialize(row_count, column_count)
    @table = Array.new

    row_count.times do |index|
      @table[index] = Array.new(column_count)
    end
  end

  def []=(row, column, value)
    @table[row][column] = value
  end

  def [](row, column = nil)
    if column.nil?
      @table[row]
    else
      @table[row][column]
    end
  end

  def inspect
    @table.inspect
  end
end

以下是您可以使用它的方式:

t = Table.new(2, 5)
t[0,0] = 'first'
t[1,4] = 'last'
t[0][1] = 'second'

puts t[0,0]
puts t.inspect

您可能还想查看Ruby's enumerable module而不是继承Array。

答案 1 :(得分:0)

答案 2 :(得分:0)

您是否有任何特定原因需要编写自己的数组类?默认情况下,您可以通过提供第二个参数来告诉数组填充新元素的内容:

>> a = Array.new(10, [])
=> [[], [], [], [], [], [], [], [], [], []]

编辑:显然,这种方式是使用对传递对象的引用来填充数组,因此一旦执行a[0][0] = "asd",包含数组的每个第一个元素都将发生变化。不酷。

>> a[0][0] = "asd"
=> "asd"
>> a
=> [["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"], ["asd"]]

要让每个包含的数组都是唯一的,请使用第三种语法,并在每次执行时给它一个块 - 块的结果将用于填充数组:

>> b = Array.new(10) { [] }
=> [[], [], [], [], [], [], [], [], [], []]
>> b[0][0] = "asd"
=> "asd"
>> b
=> [["asd"], [], [], [], [], [], [], [], [], []]

此外,由于ruby数组的工作方式,甚至不需要定义y轴大小:

>> a = Array.new(5)
=> [nil, nil, nil, nil, nil]
>> a[10]
=> nil
>> a[10] = "asd"
=> "asd"
>> a
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "asd"]

当您在索引中放入大于当前大小的内容时,会自动扩展该数组。因此,只需创建一个包含十个空数组的数组,并准备好使用10 * n大小的数组。

相关问题