Skip to content

Instantly share code, notes, and snippets.

@veronicacannon
Last active April 4, 2020 17:39
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 veronicacannon/f3dcbe4cc4eb149087bdf13812ab0bf1 to your computer and use it in GitHub Desktop.
Save veronicacannon/f3dcbe4cc4eb149087bdf13812ab0bf1 to your computer and use it in GitHub Desktop.
telldontask exercise1
# frozen_string_literal: true
Map = Struct.new(:addresses) do
def within_radius(kms)
within_range = []
addresses.each do |address|
within_range << address.to_s if address.kms <= kms
end
within_range
end
end
Address = Struct.new(:street, :number, :distance) do
def to_s
"#{street} Nr. #{number} (#{distance})"
end
def kms
distance.to_i
end
end
home = Address.new("Home Str.", "1", "10km")
work = Address.new("Work Str.", "6", "13km")
vacation_home = Address.new("Vacation Str.", "5", "100km")
parents_house = Address.new("Parents Str.", "2", "230km")
map = Map.new([home, work, vacation_home, parents_house])
puts map.within_radius(50)
# Expected:
#
# => ['Home Str. Nr. 1 (10km)', 'Work Str. Nr. 6 (13km)']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment