集合中一行中的单个 Ruby 值

发布于 2024-10-17 05:30:01 字数 287 浏览 2 评论 0原文

我有一个对象的集合。每个对象有 3 个属性

“id”、“name”、“is_primary”。

对象集合通常包含 1 到 5 个对象。

我想做的是检查集合以查看 is_primary 是否为 true。如果是这样,则输出名称,或者至少返回它。

如果可能的话,我想用 1 行代码来完成此操作。我正在尝试精简这一行,以便在 Rails 中实现 erb 输出。稍后在页面中我将全部输出。我以为我已经有了它,但如果我返回 nil,它会增加额外的空间,从而奇怪地移动所有 html。

谢谢。

I have a collection of objects. There are 3 properties in each object

'id', 'name', 'is_primary'

The collection of objects will usually have anywhere from 1 to 5 objects.

What I want to do is check the collection to see if is_primary is true. If so output the name, or at least return it.

I want to do this in 1 line of code if possible. I am trying to slim up this one line for erb output in rails. Later in the page i'll output them all. I thought I had it, but if I return nil it adds extra space which shifts all the html oddly.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

黑凤梨 2024-10-24 05:30:01

嗯,如果没有元素 is_primary,这不太有效...我还在想...

c.detect(&:is_primary).name

好吧,怎么样:

((a = c.detect(&:is_primary)) && a.name).to_s

碰巧的是,在 的 erb 模板中这是可以的><%= 表达式返回 nil,这只会产生一个空字符串,因此对于这种情况,您可以使用:

(a = c.detect(&:is_primary)) && a.name

更新: 响应第一个评论,我确实有一个没有发布的测试用例......

class A; attr_accessor :is_primary, :name, :id; end
t = [A.new, A.new, A.new, (a = A.new; a.name = 'xyz'; a.is_primary = true; a)]

puts (a = t.detect(&:is_primary)) && a.name

puts ((a = [].detect(&:is_primary)) && a.name).to_s

Hmm, this doesn't quite work if no element is_primary...I'm still thinking...

c.detect(&:is_primary).name

Ok, how about:

((a = c.detect(&:is_primary)) && a.name).to_s

As it happens, it is OK in an erb template for the <%= expression to return nil, that just results in an empty string, so for that case you can use:

(a = c.detect(&:is_primary)) && a.name

Update: Responding to the first comment, I do have a test case that I didn't post...

class A; attr_accessor :is_primary, :name, :id; end
t = [A.new, A.new, A.new, (a = A.new; a.name = 'xyz'; a.is_primary = true; a)]

puts (a = t.detect(&:is_primary)) && a.name

puts ((a = [].detect(&:is_primary)) && a.name).to_s
要走干脆点 2024-10-24 05:30:01

补充@DigitalRoss,你还可以写:(

collection.detect(&:is_primary).try(:name) || "default_if_no_element_or_name"

好吧,说实话,我更喜欢 Ick 的也许而不是 Rails 的tryc.detect(&:is_primary) .maybe.name

旁注:恕我直言,这个标志只能在一行中处于活动状态,这不是一个好主意。您可能会遇到不一致的状态,多个状态处于活动状态,并且在更新(事务等)时您会担心这一点。尝试将 PK 引用存储在其他地方(父模型?状态模型?)。

如果可能的话,我想用一行代码来完成此操作。我正在尝试精简这一行,以便在 Rails 中实现 erb 输出。稍后在页面中,我将把它们全部输出。

不需要单行话(很有趣,因为我刚刚写了一个):将代码适当地移动到您的模型或助手中,并保持您的视图原始。

Complementing @DigitalRoss, you can also write:

collection.detect(&:is_primary).try(:name) || "default_if_no_element_or_name"

(well, to be honest I prefer Ick's maybe over Rails' try: c.detect(&:is_primary).maybe.name)

Side note: IMHO a flag that can only be active for a row it's not such a good idea. You may have inconsistent states with more than one being active and you'll have worry about it when updating (transactions, and so on). Try to store the PK reference somewhere else (a parent model? a state model?).

I want to do this in 1 line of code if possible. I am trying to slim up this one line for erb output in rails. Later in the page i'll output them all.

No need for one-liners (funny since I just wrote one): move the code to yous models or helpers as appropriate and keep your views pristine.

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