【发布时间】: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 << Cell.new(value)}应该是@test_array.each {|value| @cells << Cell.new(value)}。
标签: ruby