-
-
Save ecnalyr/35d8d74352206c46c3b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This creates a hash with an array of keys which only uses the first value, so the hash is probably unecessary. | |
# Is there a way to map the given `locations_by_display_name`, find a location name for each one, and return that value? | |
# The method seems to complex for what it needs to do, and am having difficulty simplifying it. | |
def actual_from_display_name(locations_by_display_name) | |
actual_locations = [] | |
result = Hash.new { |h, k| h[k]=[] }.tap do |h| | |
locations_with_display_name.each { |k, v| h[k] << v } | |
end | |
result.select { |_, v| locations_by_display_name.include?(v.first) }.each do |location| | |
actual_locations << location.first | |
end | |
actual_locations | |
end | |
# This is an alternative I have tried (it works), but it's quiet loopy (I feel like I could use map within map?): | |
def actual_from_display_name(locations_by_display_name) | |
actual_locations = [] | |
locations_with_display_name.each do |loc| | |
locations_by_display_name.each do |name| | |
if loc.include? name | |
actual_locations << loc[0] | |
end | |
end | |
end | |
actual_locations | |
end | |
# Most recent iteration (nested maps): | |
def actual_from_display_name(locations_by_display_name) | |
locations_with_display_name.map do |loc| | |
locations_by_display_name.map do |name| | |
loc[0] if loc.include? name | |
end.compact | |
end.flatten | |
end | |
#test | |
# locations_with_display_name returns something like: [["unknown", "unknown"],["alphabet soup", "123123123124"]] | |
context '#actual_from_display_name' do | |
it 'returns a list of actual locations when given humanized versions' do | |
location_service = LocationService.new('potato') | |
assert_equal ["unknown", "alphabet soup"], | |
location_service.actual_from_display_name(["unknown", "123123123124"]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment