Skip to content

Instantly share code, notes, and snippets.

@radavis
Created August 23, 2013 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radavis/6321931 to your computer and use it in GitHub Desktop.
Save radavis/6321931 to your computer and use it in GitHub Desktop.
The Next Train assignment.
EASTEREGG = "journey.txt"
class TrainSchedule
FILENAME = 'schedule.txt'
@@schedule = {}
def initialize
if @@schedule.empty?
File.open(FILENAME, 'r').each do |line|
train, time = line.split(",")
@@schedule[train] = time.to_f
end
end
end
def next(now)
@@schedule.select { |train, time| time > now }.first
end
end
trains = TrainSchedule.new
puts "What time are you leaving? "
print "> "
leaving_time = gets.chomp.to_f
train, time = trains.next(leaving_time)
puts "You should catch #{train} leaving at #{time}\n"
# journey easter egg
if time == 24
File.open(EASTEREGG, 'r').each { |line| puts line }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment