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 メソッドは、指定されたデータに基づいて最も安いホテルを出力する必要があります。

4

5 に答える 5

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
于 2013-04-28T18:28:14.080 に答える