我有一个关于多维数组的初学红宝石问题.
我想按年份和月份对条目进行排序.所以我想创建一个包含年 - >月 - >月份条目的多维数组
所以数组就像:
2009 ->
08
-> Entry 1
-> Entry 2
09
-> Entry 3
2007 ->
10
-> Entry 5
Run Code Online (Sandbox Code Playgroud)
我现在有:
@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
Run Code Online (Sandbox Code Playgroud)
但是当我尝试遍历岁月数组时,我得到:"未定义的方法`每个'为2009:Fixnum"
也尝试过:
@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
Run Code Online (Sandbox Code Playgroud)