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

发布于 2025-01-02 08:00:48 字数 289 浏览 0 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2025-01-09 08:00:48

它们应该是具有关系的类,而不是试图利用本机 ruby​​ 数据结构。即使这些类不执行任何操作,您也可以从 Rails 关系中获得许多听起来自然的代码。

我更愿意说:

team = Team.find_by_name("Ravens")

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

你几乎免费获得潜在的其他有用的电话:

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

当你想抓住所有团队进行划分时:

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

这比

all_nfc_teams = nfl[0][1]

我想象的所有其他有用的语义更具可读性,比如找到本周亚足联共有 4 场比赛,或者按胜负顺序划分球队。您可以在 Teams 上定义范围,这将允许您检索具有非常清晰语义的团队集合。

all_nfc_teams = Team.nfc

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:

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. You can define scopes on Teams that would allow you to retrieve collections of teams with very clear semantics.

all_nfc_teams = Team.nfc
宛菡 2025-01-09 08:00:48

“最好”是主观的。它还取决于组织实体的性质。

除了组建团队之外,部门和/或会议还做其他事情吗?如果是这样,那么它们应该是类。

否则,散列可能没问题,尽管我可能将它们封装在一个方便的类中,以隐藏散列结构,提供命名团队访问等。

"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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文