开发者

How can I do a mod and case on the index of an .each do loop in ruby on rails?

开发者 https://www.devze.com 2023-02-14 05:35 出处:网络
Here is my concept and am hoping to get some suggestions: I have an array of contacts generated through searchlogic, such as:

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 i开发者_StackOverflown 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!


Perhaps Enumerable#each_with_index is what you need for this?

http://www.ruby-doc.org/core/classes/Enumerable.html#M001511

contacts.each_with_index do |contact, idx|
  quad = idx%4
end
0

精彩评论

暂无评论...
验证码 换一张
取 消