Class: RandomUsCity
- Inherits:
-
Object
- Object
- RandomUsCity
- Defined in:
- lib/random_us_city.rb,
lib/random_us_city/version.rb
Overview
This class returns a struct with data for a random city in the US. For example: #<struct RandomUsCity::City
zip="67878",
latitude="37.986428",
longitude="-101.751732",
city="Syracuse",
state="KS",
county="Hamilton">
Defined Under Namespace
Classes: City
Constant Summary collapse
- DATA_FIELDS =
[:zip, :latitude, :longitude, :city, :state, :county]
- VERSION =
"0.1.2"
Instance Method Summary collapse
-
#initialize ⇒ RandomUsCity
constructor
A new instance of RandomUsCity.
-
#random_city ⇒ Object
Returns a struct containing data for a random US city.
Constructor Details
#initialize ⇒ RandomUsCity
Returns a new instance of RandomUsCity.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/random_us_city.rb', line 20 def initialize # read in our city data... @cities = [] data_file = File.join(path_to_resources, "cities.csv") CSV.foreach(data_file, :headers => true) do |row| @cities << City.new(*( row.map do |r| if r[0] == 'longitude' || r[0] == 'latitude' r[1].to_f else r[1] end end )) end end |
Instance Method Details
#random_city ⇒ Object
Returns a struct containing data for a random US city.
40 41 42 |
# File 'lib/random_us_city.rb', line 40 def random_city return @cities.sample end |