用于组织 NFL 的最佳 Ruby 数据结构
我正在尝试用 Ruby 将 NFL 球队组织成数据结构。我的目标是轻松查询知道巴尔的摩乌鸦队是 AFC(分区)和 AFC North(分区)。
我还想轻松查看哪些球队在亚足联(其中 16 支)以及哪些球队在亚足联北部(其中 4 支)。
现在我正在数组上使用数组,但我确信有更好的方法来做到这一点。
按照我现在的数据结构方式,我必须输入 nfl[0][0][0] 来打印巴尔的摩乌鸦队(第一个 0 是 AFC/NFC,第二个 0 是分区,第三个 0 是分区)。这似乎太复杂了。
任何帮助或见解都会很棒!
I'm trying to organize NFL teams into data structures in Ruby. My goal is to easily query to know that the Baltimore Ravens are the AFC (conference) and the AFC North (division).
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).
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). This seems to be way too complicated.
Any help or insight would be amazing!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们应该是具有关系的类,而不是试图利用本机 ruby 数据结构。即使这些类不执行任何操作,您也可以从 Rails 关系中获得许多听起来自然的代码。
我更愿意说:
比
你几乎免费获得潜在的其他有用的电话:
当你想抓住所有团队进行划分时:
这比
我想象的所有其他有用的语义更具可读性,比如找到本周亚足联共有 4 场比赛,或者按胜负顺序划分球队。您可以在 Teams 上定义范围,这将允许您检索具有非常清晰语义的团队集合。
They should be classes with relationships rather than trying to leverage the native ruby data structures. Even if the classes don't DO things, you get a lot of natural sounding code out of rails relationships.
I'd much rather say:
than
You also get potentially other helpful calls for nearly free:
and when you want to grab all the teams for a division:
which is far more readable than
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. You can define scopes on Teams that would allow you to retrieve collections of teams with very clear semantics.
“最好”是主观的。它还取决于组织实体的性质。
除了组建团队之外,部门和/或会议还做其他事情吗?如果是这样,那么它们应该是类。
否则,散列可能没问题,尽管我可能将它们封装在一个方便的类中,以隐藏散列结构,提供命名团队访问等。
"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.