2013-04-28 64 views
0

我有一个数组:@costumer_request = ['regular', '12/03/2013', '14/03/2013']。 我需要验证第一个项目是'常规'还是'奖励',然后验证阵列的其余部分的每个日期是否是周末。 我做了这样的事情:循环遍历数组,并检查每个项目

@costumer_request.each_with_index do |item, index| 
    if index[0] == 'regular:' 
    if DateTime.parse(index).to_date.saturday? or DateTime.parse(index).to_date.sunday? 
     print "It's a weekend" 
    else 
     print "It's not a weekend" 
    end 
    end 
end 

require 'date' 

module HotelReservation 

    class Hotel 

    HOTELS = { 
     :RIDGEWOOD => 'RidgeWood', 
     :LAKEWOOD => 'LakeWood', 
     :BRIDGEWOOD => 'BridgeWood' 
    } 

    def weekend?(date) 
     datetime = DateTime.parse(date) 
     datetime.saturday? || datetime.sunday? 
    end 

    def find_the_cheapest_hotel(text_file) 

     @weekends_for_regular = 0 
     @weekdays_for_regular = 0 

     @weekends_for_rewards = 0 
     @weekdays_for_rewards = 0 

     File.open(text_file).each_line do |line| 

     @costumer_request = line.delete!(':').split 
     @costumer_request = line.delete!(',').split 

     #Here I want to process something in each array 
     #but if I do something like bellow, it will 
     #store the result of the two arrays in the same variable 
     #I want to store the result of the first array, process something 
     #and then do another thing with the second one, and so on. 

     if(@costumer_request.first == 'regular') 
     @costumer_request[1..-1].each do |date| 
      if (weekend?(date)) 
      @weekends_for_regular +=1 
      else 
      @weekdays_for_regular +=1 
      end 
     end 
     else 
      if(@costumer_request.first == 'rewards') 
      @costumer_request[1..-1].each do |date| 
      if (weekend?(date)) 
       @weekends_for_rewards +=1 
      else 
       @weekdays_for_rewards +=1 
      end 
      end 
     end 
     end 
    end 
    end 
end 
end 

的find_the_cheapest_hotel方法应该最廉价的酒店基于给定的数据输出。

+2

在哪里的问题? – tessi 2013-04-28 18:16:25

+0

首先:检查客户是定期还是奖励客户。第二:检查给定日期是否是周末。如果周末显示消息“这是一个周末”,则显示“这不是一个周末” – 2013-04-28 18:19:59

+0

是否要验证阵列中的所有日期均为周末日期,并且如果第一个条目不是“定期或奖励?什么是预期的输出?如果两者都匹配并且所有日期均为周末,则为真?或匹配日期的列表? – Doon 2013-04-28 18:28:06

回答

0

下面是做这件事的真正干净的方式:

def is_weekend?(dt) 
    dt.saturday? || dt.sunday? 
end 

type, *dates = @costumer_request 

if(type == 'regular') 
    dates.each do |date| 
    if(weekend?(Date.parse(date)) 
     #do processing here 
    end 
    end 
end 
+0

感谢您的回答。 *日期=创建日期数组?我没有得到这部分。 – 2013-04-28 22:23:56

+0

该行使用图示运算符(*)。它将类型设置为@customer_request的第一个元素,并将日期设置为数组的重置。 – 2013-04-28 23:32:07

+0

明白了。谢谢。 – 2013-04-28 23:47:45

1
require 'time' 
require 'date' 
@costumer_request = ['regular', '28/03/2013', '14/03/2013'] 
if @costumer_request.first == 'regular' 
    if @costumer_request[1...-1].all?{|item| Time.local(item).saturday? || Time.local(item).sunday? } 
     print "It's a weekend" 
    else 
     print "It's not a weekend" 
    end 
end 

输出:

It's a weekend 
+0

您可能想要在循环外部移动“第一个”检查。 – 2013-04-28 18:28:13

+0

@DaveS。我做完!请评估。 – 2013-04-28 18:32:22

0
require 'time' 

def weekend?(date) 
    datetime = DateTime.parse(date) 
    datetime.saturday? || datetime.sunday? 
end 

@costumer_request = ['regular', '28/03/2013', '14/03/2013'] 

type = @costumer_request.shift 

if type == 'regular' 
    @costumer_request.each do |date| 
    if weekend?(date) 
     puts "#{date} a weekend" 
    else 
     puts "#{date} not a weekend" 
    end 
    end 
end 
0

你还不如直接加weekend?为DateTime:

class DateTime 
    def weekend? 
    saturday? || sunday? 
    end 
end 

现在你可以转换所有,但第一个元素DateTime对象,然后chec k他们都在周末。

if @customer_request.first == 'regular' 
    dates = @customer_request[1..-1].map { |date_string| DateTime.parse(date_string) } 
    if dates.all?(&:weekend?) 
    puts 'all dates on a weekend' 
    else 
    puts 'at least one date is not on a weekend' 
    end 
else 
    puts 'not a regular customer request' 
end 
-1
@customer_request = ['regular', '12/03/2013', '14/03/2013'] 



def val 
    @customer_request.first == 'regular' && 
    @customer_request.drop(1).inject(true) do |m, e| 
     wd = Time.local(*e.split('/').reverse.map(&:to_i)) 
     m &&= (wd.saturday? || wd.sunday?) 
    end 
end 


p val 
p val ? 'all are weekends' : 'something isn\'t a weekend' 
+0

为什么你将实例变量赋值给单个字母变量名?这完全不可读。 – Chris 2013-04-28 18:35:35

+0

@chris,更新... – DigitalRoss 2013-04-28 18:58:53

相关问题