我想知道为什么while循环中的IF语句只会触发一次。 我应该获得4个连接的行,但我只是得到2个。 显然,IF没有正常发射
我基本上想要列出给定列车编号的下一个时刻表(time_tables)。
谢谢!
require 'date'
Time_tables = [
{ name: '01251', start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'BC' },
{ name: '05012', start_date: '2014-04-24 22:20:00', start_location: 'RI', end_date: '2014-04-24 23:10:00', end_location: 'XX' },
{ name: '03232', start_date: '2014-04-24 17:10:00', start_location: 'X', end_date: '2014-04-24 20:10:00', end_location: 'B' },
{ name: '02435', start_date: '2014-04-24 17:10:00', start_location: 'Z', end_date: '2014-04-24 20:10:00', end_location: 'B' },
{ name: '04545', start_date: '2014-04-24 22:15:00', start_location: 'BC', end_date: '2014-04-24 22:20:00', end_location: 'RI' },
{ name: '03545', start_date: '2014-04-24 23:15:00', start_location: 'XX', end_date: '2014-04-25 00:10:00', end_location: 'E' }
]
class TrainSearch
def initialize(data)
@data = data
end
def rows
@rows ||= @data.map {|row| Row.new(row)}
end
# { name: '01251', start_date: '2014-04-24 22:03:00', start_location: 'A', end_date: '2014-04-24 22:10:00', end_location: 'BC' }
def find_train(train_number)
rows.find { |row| row.name == train_number }
end
def find_next_route(after_time, end_location)
rows.find { |row| row.start_date > after_time and row.start_location == end_location }
end
def find_train_and_next_routes(train_number)
if find_train(train_number)
train = find_train(train_number)
routes = [train] # add the train as the first route
else
p 'not valid number'
end
# we search first to see if we find a route
# we reassign the train variable to the new train found
#if(train = find_next_route(train.end_date, train.end_location))
i = 0
while i <= rows.count do
if(train = find_next_route(routes[-1].end_date, routes[-1].end_location))
p 'are we in ?: '
p i
routes << train
end
i += 1
end
# at the end return the train and the routes
return routes
end
# Do this because we need to parse the date into a time to sort
class Row
def initialize(row)
@row = row
end
def start_date
DateTime.parse(@row[:start_date])
end
def end_date
DateTime.parse(@row[:end_date])
end
def name
@row[:name]
end
def start_location
@row[:start_location]
end
def end_location
@row[:end_location]
end
end
end
user = TrainSearch.new(Time_tables)
p user.find_train_and_next_routes '01251'
答案 0 :(得分:0)
为什么你期待4条路线? 我假设你想要这个
#=> [
#<TrainSearch::Row:0x26f3c58 @row={:name=>"01251", :start_date=>"2014-04-24 22:03:00",:start_location=>"A", :end_date=>"2014-04-24 22:10:00", :end_location=>"BC"}>,
#<TrainSearch::Row:0x26f3bf8 @row={:name=>"04545", :start_date=>"2014-04-24 22:15:00", :start_location=>"BC", :end_date=>"2014-04-24 22:20:00", :end_location=>"RI"}>,
#<TrainSearch::Row:0x26f3c40 @row={:name=>"05012", :start_date=>"2014-04-24 22:20:00", :start_location=>"RI", :end_date=>"2014-04-24 23:10:00", :end_location=>"XX"}>,
#<TrainSearch::Row:0x26f3be0 @row={:name=>"03545", :start_date=>"2014-04-24 23:15:00",:start_location=>"XX", :end_date=>"2014-04-25 00:10:00", :end_location=>"E"}>]
如果是这样,你的问题是那个
#<TrainSearch::Row:0x26f3c40 @row={:name=>"05012", :start_date=>"2014-04-24 22:20:00", :start_location=>"RI", :end_date=>"2014-04-24 23:10:00", :end_location=>"XX"}>
与
的end_date同时开始 #<TrainSearch::Row:0x26f3bf8 @row={:name=>"04545", :start_date=>"2014-04-24 22:15:00", :start_location=>"BC", :end_date=>"2014-04-24 22:20:00", :end_location=>"RI"}>
因此
def find_next_route(after_time, end_location)
rows.find { |row| row.start_date > after_time && row.start_location == end_location }
end
返回nil,因为row.start_date > after_time
为false,添加>=
会获得所需的结果
您也可以按如下方式实施Row
class Row
attr_reader :row,:start_date,:end_date,:name,:start_location,:end_location
def initialize(row)
@row = row
@row.each &method(:instance_variable_set)
end
end