如何使红宝石动态多维数组? [英] How to make dynamic multi-dimensional array in ruby?

查看:101
本文介绍了如何使红宝石动态多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对多维数组初学者红宝石问题。

我想按年份和月份条目进行排序。所以我想创建一个将包含年多维数组 - >月 - >每月项

因此​​,阵列会像:

  2009  - >
       08
           - >第1项
           - >进入2
       09
           - >进入3
2007 - >
       10
           - >进入5

现在我有:

  @years = []
@ entries.each做|入门|
    timeobj = Time.parse(entry.created_at.to_s)
    年= timeobj.strftime(%Y)。to_i
    一个月= timeobj.strftime(%间)。to_i
    tmparr = []
    tmparr<< {月=>}进入
    @ years.push(年)
    @years<< tmparr
结束

但是当我试图通过几年数​​组迭代,我得到:未定义的方法`每个2009年:Fixnum对象

也试过:

  @years = []
@ entries.each做|入门|
    timeobj = Time.parse(entry.created_at.to_s)
    年= timeobj.strftime(%Y)。to_i
    一个月= timeobj.strftime(%间)。to_i
    @years [同期] [月]<<条目
结束


解决方案

您所得到的错误,因为 Fixnum对象(也就是一个数字)被压在阵列上在读取的行 @ years.push(年)

您使用数组入手的做法是有点瑕疵;一个阵列是完美的保持件的一个有序列表。你的情况,你必须从键到值的映射,这是一个完美的哈希。

在第一级中,键是年,值是散列。第二个层次的散列包含的条目阵列月键和值。

在这种情况下,您的code的典型输出会看起来像(根据你的例子):

  {2009年= GT; {8 => [条目1,ENTRY2] 9 => [Entry3]},2007 => {10 => [Entry5]}}

注意,然而,年和月顺序并不保证在任何特定的顺序。解决的办法通常是,每当你想访问他们订购的钥匙。现在,code,它会产生这样的输出(根据您的code的布局,虽然可以多进行的 rubier 的):

  @years = {}
@ entries.each做|入门|
  timeobj = Time.parse(entry.created_at.to_s)
  年= timeobj.strftime(%Y)。to_i
  一个月= timeobj.strftime(%间)。to_i
  @years [同期] || = {}#创建一个子散,除非它已经存在
  @years [同期] [月] || = []
  @years [同期] [月]<<条目
结束

I have a beginner ruby question about multi dimensional arrays.

I want to sort entries by year and month. So I want to create a multi-dimensional array that would contain years -> months -> entries of month

So the array would be like:

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

Now I have:

@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

but when I try to iterate through the years array, I get: "undefined method `each' for 2009:Fixnum"

Tried also:

@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

解决方案

You are getting the error because a FixNum (that is, a number) is pushed on the array, in the line that reads @years.push(year).

Your approach of using Arrays to start with is a bit flawed; an array is perfect to hold an ordered list of items. In your case, you have a mapping from keys to values, which is perfect for a Hash.

In the first level, the keys are years, the values are hashes. The second level's hashes contain keys of months, and values of arrays of entries.

In this case, a typical output of your code would look something like (based on your example):

{ 2009 => { 8 => [Entry1, Entry2], 9 => [Entry3] }, 2007 => { 10 => [Entry5] }}

Notice that, however, the order of years and months is not guaranteed to be in any particular order. The solution is normally to order the keys whenever you want to access them. Now, a code that would generate such an output (based on your layout of code, although can be made much rubier):

@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] ||= {} # Create a sub-hash unless it already exists
  @years[year][month] ||= []
  @years[year][month] << entry
end

这篇关于如何使红宝石动态多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆