编写简洁的多个else / if语句

时间:2014-11-19 03:28:23

标签: ruby

我有以下代码,

@checkedin=[]
@accepted=[]
@rejected=[]   
result.each do |parse_order|
  orderId = parse_order['orderID']
    if parse_order['status'] == -1
        @rejected << orderId
    elsif parse_order['status'] == 1
        @accepted << [orderId, parse_order['createdAt']]
    elsif parse_order['status'] == 2
       @checkedin << [orderId, parse_order['createdAt']]
    elsif parse_order['status'] == 3
        next       
   end   
end

有没有更好的方法来简洁它。 谢谢。

5 个答案:

答案 0 :(得分:5)

您可以使用单行条件。此外,在块结束时不需要next

@checkedin = []
@accepted  = []
@rejected  = []   

result.each do |parse_order|
  orderId = parse_order['orderID']
  status = parse_order['status']

  @rejected  << orderId if status == -1
  @accepted  << [orderId, parse_order['createdAt']] if status == 1
  @checkedin << [orderId, parse_order['createdAt']] if status == 2
end

答案 1 :(得分:3)

@checkedin=[]
@accepted=[]
@rejected=[]   
result.each do |parse_order|
  orderId = parse_order['orderID']
  case parse_order['status']
  when -1 then @rejected << orderId
  when 1 then @accepted << [orderId, parse_order['createdAt']]
  when 2 then @checked_in << [orderId, parse_order['createdAt']]
  end
end

您不需要最后一个案例

为了便于阅读,我建议删除每个案例

result.each do |parse_order|
  reject(parse_order) || accept(parse_order) || check_in(parse_order)
end

def reject(parse_order)
  @rejected << parse_order['orderId'] if parse_order['orderId'] == -1
end

def accept(parse_order)
  @accepted << [parse_order['orderId'], parse_order['createdAt']] if parse_order['orderId'] == 1
end

def check_in(parse_order)
  @checked_in << [parse_order['orderId'], parse_order['createdAt']] if parse_order['orderId'] == 2
end

为简洁起见,我可能会将parse_order重命名为order

答案 2 :(得分:3)

我要把帽子戴在戒指上。恕我直言,它更具人性化,更易于维护。虽然它有更多的代码行(由于Order类),但实际的订单过滤更简洁:

require 'order' 
orders = results.map{|result| Order.new(result['status'], result['createdAt'])}

@checked_in_orders = orders.select {|order| order.checked_in?}
@accepted_orders = orders.select {|order| order.accepted?}
@rejected_orders = orders.select {|order| order.rejected?}

Order类是:

# orders.rb
class Order
  REJECTED_STATUS = -1
  ACCEPTED_STATUS = 1
  CHECKED_IN_STATUS = 2

  attr_reader :created_at

  def initialize(status, created_at)
    @status, @created_at = [status, created_at]
  end

  def rejected?
    @status == REJECTED_STATUS
  end

  def accepted?
    @status == ACCEPTED_STATUS
  end

  def checked_in?
    @status == CHECKED_IN_STATUS
  end     
end

答案 3 :(得分:2)

我会使用Enumerable#group_by

REJECTED   = -1
ACCEPTED   =  1
CHECKED_IN =  2

h = result.group_by {|order| order['status']}
@rejected   = h[REJECTED  ].map {|order|  order['orderID']}
@accepted   = h[ACCEPTED  ].map {|order| [order['orderID'] order['createdAt']]}
@checked_in = h[CHECKED_IN].map {|order| [order['orderID'] order['createdAt']]}

答案 4 :(得分:0)

您可以使用案例表达式:

@checkedin=[]
@accepted=[]
@rejected=[]   
result.each do |parse_order|
  orderId = parse_order['orderID']

  case parse_order['status']
  when  -1
    @rejected << orderId
  when 1
    @accepted << [orderId, parse_order['createdAt']]
  when  2
    @checkedin << [orderId, parse_order['createdAt']]
  when 3
    next       
  end   
end

您可以在this blog postofficial documentation中看到案例的更多用法。