Skip to content

Instantly share code, notes, and snippets.

@programisti
Created April 3, 2021 07:57
Show Gist options
  • Save programisti/2080a8d586d850858c93ec414c616958 to your computer and use it in GitHub Desktop.
Save programisti/2080a8d586d850858c93ec414c616958 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# rubocop:disable Style/Documentation
class FlightManager
def add_new_flight(flight_number, origin, destination)
@flights ||= []
flight = {flight_number: flight_number, origin: origin, destination: destination}
already_added = @flights.include?(flight)
@flights.push(flight) unless already_added
!already_added
end
def find_flights_between(origin, destination, direction_sensitive)
@flights.select do |flight|
if direction_sensitive
find_direct_flights(flight, origin, destination)
else
find_flights(flight, origin, destination)
end
end
end
private
def find_direct_flights(flight, origin, destination)
flight[:origin] == origin && flight[:destination] == destination
end
def find_flights(flight, origin, destination)
flight[:origin] == origin ||
flight[:destination] == destination ||
flight[:destination] == origin ||
flight[:origin] == destination
end
end
# rubocop:enable Style/Documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment