如何在 ruby on Rails 中的 .each do 循环的索引上执行 mod 和 case?
这是我的概念,希望得到一些建议:
我有一组通过 searchlogic 生成的联系人,例如:
contacts = Contacts.date_due_is(Date.today)
这些联系人通过 Prawn 输出到四张明信片上,其中一个联系人在每个“象限”——比如 I、II、III 和 IV。
当我循环时,我需要知道将其放置在哪个象限。我认为最好的方法是能够对索引值进行 mod
操作,然后对文本输出进行 case
操作,但不知道该怎么做这。
该索引不是整个联系人数据库中的联系人的索引,而是通过each-do 循环的数组。
我想做的与此等效(从概念上讲,但找不到代码示例):
i = contact.index (其中 contact.index 可以是联系人数组中的 0、1、2 等) .quad
= mod(i/4) - 可能的输出为 0、1、2 或 3(或类似的值)
:
然后在左上角输出。 1:然后在左下输出 2:然后在右上角输出 3:然后在右下角输出
问题:
1)我如何在 ruby on Rails 中做到这一点?
2)还有其他推荐吗?
谢谢大家!
Here is my concept and am hoping to get some suggestions:
I have an array of contacts generated through searchlogic, such as:
contacts = Contacts.date_due_is(Date.today)
These contacts are being output via Prawn onto four postcards, with one contact in each "quadrant" -- say I, II, III, and IV.
As I loop through, I need to know which quadrant to place it. I think the best way to do it would be to be able to do a mod
of the index value and then case
the text output, but don't know how to do this.
The index isn't of the contact in the entire Contacts database, but in terms of the array looping through with the each-do.
What I'd like to do would be the equivalent of this (conceptually, but can't find examples of the code):
i = contact.index (where contact.index can be 0, 1, 2, etcetera in the contacts array.
quad = mod(i/4) -- where the possible output is 0, 1, 2, or 3 (or something like that).
Case quad:
0 : then output in upper left
1 : then output in lower left
2: then output in upper right
3: then output in lower right
Question:
1) How could I do this in ruby on rails?
2) Is there another recommendation?
Thanks to you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许
Enumerable#each_with_index
正是您所需要的?http://www.ruby-doc.org/core/classes/Enumerable .html#M001511
Perhaps
Enumerable#each_with_index
is what you need for this?http://www.ruby-doc.org/core/classes/Enumerable.html#M001511