【问题标题】:Changing object variables in an array of instance variables in Ruby在 Ruby 中更改实例变量数组中的对象变量
【发布时间】:2013-09-12 21:55:44
【问题描述】:

如何处理整数数组@test_array,创建一个新数组@cells,其中包含数组的每个元素的Cell 类实例,将@test_array 元素的值放入实例新创建对象的变量@value

之后,我希望能够更改不同对象的@value,并在程序结束时输出一个包含所有对象@value的数组。

class Cells

  attr_accessor :value

  def initialize(value)
    @value = value
  end

end

class Grid

  attr_accessor :test_array, :cells

  def initialize
    @test_array = [1, 2, 3, 4]
    @cells = []
    @test_array.each { |value| @cells << Cell.new(value) }
  end

  def put_values_of_objects_to_array
    @cells.value_to_a ????????
  end

end

考虑到其中一个答案,这是我在运行代码后得到的结果:

2.0.0p247 :080 > cells = []
 => [] 
2.0.0p247 :081 > abc = [1,2,3,4,5,6,7,8,9]
 => [1, 2, 3, 4, 5, 6, 7, 8, 9] 
2.0.0p247 :082 > abc.map {|value| cells << Cell.new(value)}
 => [[#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>], [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>]] 
2.0.0p247 :083 > cells
 => [#<Cell:0x007fe3dc3313d8 @value=1>, #<Cell:0x007fe3dc3313b0 @value=2>, #<Cell:0x007fe3dc331388 @value=3>, #<Cell:0x007fe3dc331360 @value=4>, #<Cell:0x007fe3dc331338 @value=5>, #<Cell:0x007fe3dc331310 @value=6>, #<Cell:0x007fe3dc3312e8 @value=7>, #<Cell:0x007fe3dc3312c0 @value=8>, #<Cell:0x007fe3dc331298 @value=9>] 
2.0.0p247 :084 > 

【问题讨论】:

  • 无论如何,test_array.each {|value| @cells &lt;&lt; Cell.new(value)} 应该是@test_array.each {|value| @cells &lt;&lt; Cell.new(value)}

标签: ruby


【解决方案1】:

这是一个有用的练习,你非常接近。声明一个数组 (@cells = []) 然后遍历另一个数组以对每个元素执行某些操作并将结果添加到新数组的东西确实有效,但这是一种非常常见的模式,以至于许多语言都有更简单的解决方案.在 Ruby 中,它被称为'map' aliased as 'collect'。演示:

class Cell

attr_accessor :value

  def initialize(value)
    @value = value
  end

end

class Grid

attr_accessor :test_array, :cells

  def initialize
    @test_array = [1,2,3,4]
    @cells = @test_array.map{|a_value| Cell.new(a_value)}
   end

  def put_values_of_objects_to_array
    @cells.map{|cell| cell.value}
  end

end


f = Grid.new
p f.put_values_of_objects_to_array #=> [1, 2, 3, 4]

【讨论】:

  • 感谢您的回答。但是,我不知道为什么,当我运行命令时,它会返回一个包含 81 个元素的数组,每个元素中有 81 个元素。所以基本上它得到了 6561 个 Class 对象。知道为什么吗?我一遍又一遍地检查语法,就像你发布的那样......
【解决方案2】:
class Cell
  attr_accessor :values

  @arrs = []    #Creates a "class instance variable"

  class <<self  #Creates an accessor for the "class instance variable"
    attr_reader :arrs
  end

  def initialize(values)
    @values = values
    self.class.arrs << @values    #Add the @values array to the "class instance variable"
  end
end

test_array = [1, 2, 3]

@cells = test_array.map do |num|   #Create the cells
  Cell.new(test_array.dup)
end

p @cells   

--output:--
[#<Cell:0x0000010085ffd8 @values=[1, 2, 3]>, #<Cell:0x0000010085ff88 @values=[1, 2, 3]>, #<Cell:0x0000010085ff60 @values=[1, 2, 3]>]


@cells.each_with_index do |cell, i|   #Change the last value of each cell's @values array
  cell.values[-1] = i
end


p Cell.arrs   #Show all the @values arrays of all the cells:

--output:--
[[1, 2, 3, 0], [1, 2, 3, 1], [1, 2, 3, 2]]

现在您已将 Grid 类添加到您的帖子中,您可以将类实例变量添加到 Grid 类(而不是 Cell 类)以跟踪数组。

class Grid
  attr_accessor :cells

  TEST_ARRAY = [1, 2, 3, 4]

  @arrs = []
  class <<self
    attr_reader :arrs
  end

  def initialize 
    @cells = []

    TEST_ARRAY.each do |value| 
      new_cell = Cell.new(TEST_ARRAY.dup) 
      @cells << new_cell
      self.class.arrs << new_cell.values
    end

  end

end

嘿,谢谢你,但是我想要的是 test_array 进入对象,而不是整个数组。

这不是这个意思:

...将@test_array 元素的值放入实例变量@value

class Cell
  attr_accessor :values

  def initialize(value)
    @values = [value]
  end
end

class Grid
  attr_accessor :cells

  TEST_ARRAY = [1, 2, 3, 4]

  @cell_values = []
  class <<self
    attr_reader :cell_values
  end

  def initialize 
    @cells = []

    TEST_ARRAY.each_with_index do |value, i| 
      new_cell = Cell.new(TEST_ARRAY[i]) 
      @cells << new_cell
      self.class.cell_values << new_cell.values
    end

  end

end

mygrid = Grid.new
p Grid.cell_values

mygrid.cells.each do |cell|
  cell.values << 5
end

p Grid.cell_values


--output:--
[[1], [2], [3], [4]]
[[1, 5], [2, 5], [3, 5], [4, 5]]

【讨论】:

  • 嘿,谢谢你,但是我想要的是 test_array 的一个元素进入对象,而不是整个数组。
猜你喜欢
  • 2017-05-24
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 2012-05-18
  • 1970-01-01
相关资源
最近更新 更多