I am trying to create an array of dates using loop. But the loop is only pushing one date and when I query an array I see it's not an array, rather a list. Help.
date1 = '01-01-2019'.to_date
dates = []
count = 0
repeat = 3
while (count < repeat)
count += 1
date2 = date1 + count.month
dates << date2
puts dates
end
Expected results should be [01-02-2019, 01-03-2019, 01-04-2019].
However, if I use rails console, all I get are the dates in a list. If I raise dates.inspect in controller, I only get 01-02-2019.
How can I fix this?
dates = 3.times.map { |i| '01-01-2019'.to_date + i.months }does what you assumingly need.map's receiver to, say,(1..repeat)or1.upto(repeat).Enumerablemethods instead". If you ever find yourself writing a loop in Ruby, there's a 99.999% chance you are doing something wrong.