Wie erstelle ich ein dynamisches mehrdimensionales Array in Ruby?

Ich habe eine Anfänger-Ruby-Frage zu mehrdimensionalen Arrays.

Ich möchte Einträge nach Jahr und Monat sortieren. Ich möchte also ein mehrdimensionales Array erstellen, das Jahre -> Monate -> Einträge des Monats enthält

Das Array würde also so aussehen:

2009 ->
       08
          -> Entry 1
          -> Entry 2
       09
          -> Entry 3
2007 ->
       10
          -> Entry 5

Jetzt habe ich:

@years = []
@entries.each do |entry|
    timeobj = Time.parse(entry.created_at.to_s)
    year = timeobj.strftime("%Y").to_i
    month = timeobj.strftime("%m").to_i
    tmparr = []
    tmparr << {month=>entry}
    @years.push(year)
    @years << tmparr
end

aber wenn ich versuche, das Array "years" zu durchlaufen, erhalte ich: "undefined method` each 'for 2009: Fixnum "

Versuchte auch:

@years = []
@entries.each do |entry|
    timeobj = Time.parse(entry.created_at.to_s)
    year = timeobj.strftime("%Y").to_i
    month = timeobj.strftime("%m").to_i
    @years[year][month] << entry
end

Antworten auf die Frage(4)

Ihre Antwort auf die Frage