简体   繁体   English

用于组织NFL的最佳Ruby数据结构

[英]Best Ruby data structure for organizing the NFL

I'm trying to organize NFL teams into data structures in Ruby. 我正在尝试将NFL团队组织成Ruby中的数据结构。 My goal is to easily query to know that the Baltimore Ravens are the AFC (conference) and the AFC North (division). 我的目标是轻松查询一下巴尔的摩乌鸦是AFC(会议)还是AFC North(分区)。

I would also like to easily see which teams are in the AFC (16 of them) and which teams are in the AFC North (4 of them). 我还想轻松地看到哪些球队在AFC中(其中16支)和​​哪些球队在AFC North中(其中4支)。

Right now I'm using arrays on arrays but I'm sure there is a better way to do this. 现在,我在数组上使用数组,但是我确定有更好的方法可以做到这一点。

With the way my data is structured right now, I have to puts nfl[0][0][0] to print out Baltimore Ravens (first 0 is AFC/NFC, second 0 is conference and third 0 is division). 以我现在的数据结构方式,我必须放入nfl [0] [0] [0]来打印巴尔的摩乌鸦(第一个0是AFC / NFC,第二个0是会议,第三个0是除法)。 This seems to be way too complicated. 这似乎太复杂了。

Any help or insight would be amazing! 任何帮助或见识将是惊人的!

They should be classes with relationships rather than trying to leverage the native ruby data structures. 它们应该是具有关系的类,而不是试图利用本机的ruby数据结构。 Even if the classes don't DO things, you get a lot of natural sounding code out of rails relationships. 即使类不做任何事情,您也会从Rails关系中获得很多自然的代码。

I'd much rather say: 我宁愿说:

team = Team.find_by_name("Ravens")

than

team = nfl[0][0][0]

You also get potentially other helpful calls for nearly free: 您还将免费获得潜在的其他有用电话:

team = Team.find_by_city("Baltimore")   # also gets you the ravens

and when you want to grab all the teams for a division: 当您想吸引所有团队进行分组时:

all_nfc_teams = Division.find_by_name("NFC").teams

which is far more readable than 远比它可读性强

all_nfc_teams = nfl[0][1]

I can imagine all sorts of other helpful semantics, like finding all of the week 4 games for the AFC, or ordering a division of teams by wins and losses. 我可以想像各种其他有用的语义,例如为AFC查找第4周的所有比赛,或按获胜和亏损命令分配球队。 You can define scopes on Teams that would allow you to retrieve collections of teams with very clear semantics. 您可以在Teams上定义范围,以使您可以使用非常清晰的语义检索团队的集合。

all_nfc_teams = Team.nfc

"Best" is subjective. “最佳”是主观的。 It also depends on the nature of the organizational entities. 这也取决于组织实体的性质。

Do divisions and/or conferences do anything other than hold teams? 除举行团队以外,部门和/或会议是否有其他活动? If so, then they should be classes. 如果是这样,那么它们应该是类。

Otherwise hashes are probably fine, although I might encapsulate them in a convenience class, to hide the hash structures, provide named-team access, etc. 否则,散列可能会很好,尽管我可以将它们封装在一个便利类中,以隐藏哈希结构,提供命名组访问等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM