class TestClass
attr_accessor :name, :id
end
values = ["test1", "test2"]
mapped_values = values.map{|value|
test_class = TestClass.new
test_class.name = value
test_class.id = #some random number
return test_class
}
puts mapped_values
Obviously this won't work, it will just return the first value and not the whole of newly constructed list. I have this test script which what I wanted to achieve is that it returns the list of TestClass with value name and id in it, from the Array.map operation. I'm just trying to find the best way to do it in Ruby.
I could do something like this
tests = []
values.each do |value|
test_class = TestClass.new
test_class.name = value
test_class.id = #some random number
tests << test_class
end
I believe there must be a better way of doing this?