在 Ruby 中循环多个数组
我有多个 ActiveRecord
子类 Item
实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:
项目 A 需要 5 天内维护
B 项需在 6 天内付款
项目 A 需要在 7 天内付款
B 项需要 8 天内维护
我目前有两个查询用于查找 maintenance
和 payment
项目(非排他查询)以及类似以下内容的输出:
<%- item_p = nil -%>
<%- item_m = nil -%>
<%- loop do -%>
<% item_p ||= @items_p.shift %>
<% item_m ||= @items_m.shift %>
<%- if item_p.nil? and item_m.nil? then break -%>
<%- elsif item_p and (item_m.nil? or item_p.paymt < item_m.maint) then -%>
<%= item_p.name %> payment required in ...
<%- elsif item_m and (item_p.nil? or item_m.maint < item_p.paymt) then -%>
<%= item_m.name %> maintenance required in ...
<%- end -%>
<%- end -%>
任何方式清理上面(丑陋的)代码的可读性?
I have multiple arrays of instances of ActiveRecord
subclass Item
that I need to loop through an print in accordance to earliest event. In this case, I need to print print out payment and maintenance dates as follows:
Item A maintenance required in 5 days
Item B payment required in 6 days
Item A payment required in 7 days
Item B maintenance required in 8 days
I currently have two queries for finding maintenance
and payment
items (non-exclusive query) and something like the following to output them:
<%- item_p = nil -%>
<%- item_m = nil -%>
<%- loop do -%>
<% item_p ||= @items_p.shift %>
<% item_m ||= @items_m.shift %>
<%- if item_p.nil? and item_m.nil? then break -%>
<%- elsif item_p and (item_m.nil? or item_p.paymt < item_m.maint) then -%>
<%= item_p.name %> payment required in ...
<%- elsif item_m and (item_p.nil? or item_m.maint < item_p.paymt) then -%>
<%= item_m.name %> maintenance required in ...
<%- end -%>
<%- end -%>
Any way to cleanup the readability of the above (ugly) code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
采用鸭子类型并确保您的对象是多态。您希望您的付款项目与维护项目可比较,以便对它们进行排序。
因此,假设您有一个
Payment
和一个Maintenance
类:了解我们如何创建
action
、due
和 < 两个类中的 code><=> 方法?我们还小心地包含了 Ruby 内置模块Comparable
。这使我们能够执行以下操作:然后视图变得简单如下:
我确信我没有正确了解您的实现细节,但我希望这能让您了解如何解决您的问题。
Embrace duck-typing and make sure that your objects are polymorphic. You want your payment items to be comparable with maintenance items, in order to sort them.
So, suppose you have a
Payment
and aMaintenance
class:See how we create an
action
,due
and<=>
method in both classes? We also took care to include the Ruby built-in moduleComparable
. This allows us to do the following:The view then becomes as simple as:
I'm sure I haven't got the details of your implementation right, but I hope this gives you an idea of how to approach your problem.
这是快速且肮脏的(即未优化):
This is quick and dirty (i.e. not optimized):
在你看来,你做得太过分了。实际上,您应该在控制器中弄清楚所有这些,并通过一个清理后的结构,您可以对其进行迭代以用于显示目的。
举个例子:
然后您可以根据需要迭代
@messages
。这里真正的问题是,你没有从战略上讲,为这类事情构建这些对象。如果您有一个单一的截止日期方法,而不必根据类型区分
paymt
和maint
,那就太好了。同样,如果将两者配对成一个数组而不是单独提供,那就更好了。如果您将它们放在
[ p, m ]
对中,则可以更简单地进行迭代:action
方法将返回payment 或
maintenance
根据需要。You're doing way too much in your view. Really you should figure out all of this in the controller and pass through a cleaned up structure that you can iterate over for display purposes.
As an example:
You would then iterate over
@messages
as required.The real problem here is that you haven't structured these objects for this sort of thing strategically speaking. It would be nice if you had a single method for the due date instead of having to differentiate between
paymt
andmaint
according to the type. Likewise, it would be better if both of these were paired up into an Array instead of supplied separately.If you had them in
[ p, m ]
pairs you could iterate much more simply:The
action
method would returnpayment
ormaintenance
as required.