Как можно обработать массив @test_array
целых чисел, создавая новый массив @cells
, который содержит экземпляр класса Cell
для каждого элемента массива, поставив значение @test_array
элемента в переменную экземпляра @value
вновь созданного объекта?Изменение переменных объекта в массиве переменных экземпляра в Ruby,
Впоследствии, я хочу иметь возможность изменять @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 {| значение | @cells << Cell.new (значение)} 'должно быть '@test_array.each {| значение | @cells << Cell.new (значение)} '. – steenslag