I am working on iteration: time targeting section of this (the Event Manager exercise from Jumpstart Lab). I figured out how to iterate over the array and turn it into a hash from this thread. This is what I have:
require "date"
time = ["11/12/08 10:47","11/12/08 13:23","11/12/08 13:30","11/12/08 14:04","11/12/08 14:46"]
#makes array of all times
time.each do |time|
@array = Array(DateTime.strptime(time,'%m/%d/%Y %H:%M').hour)
end
#makes hash of :time => number of instances
result = Hash.new(0)
@array.each do |time|
result[time] += 1
end
puts result
The hash I get when I run it is { 14 => 1 }
, which is not what I'm looking for. I suspect this happens because my array is made of integers, not strings like the example. Is there a way to accomplish the same effect on integers? Or do I have to convert my integers to strings? If I have to convert to strings, where do I put to_s
?