-
-
Save dnagir/a8c86e12d7bf7afff3a7 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
require 'test/unit' | |
require './economics' | |
class EconomicsTest < Test::Unit::TestCase | |
def setup | |
@world = World.current | |
end | |
def test_population_of_country_with_the_most_people | |
assert_not_equal 0, @world.most_populated_country.population | |
end | |
def test_country_with_the_most_people | |
assert_equal 'China', @world.most_populated_country.name | |
end | |
def test_five_countries_with_highest_inflation | |
assert_equal ["Belarus", "Turkey", "Azerbaijan", "Malawi", "Yemen"], @world.highest_inflation_countries(5).map(&:name) | |
end | |
def test_five_highest_inflation_rates | |
assert_equal [244.0, 94.0, 85.0, 83.3, 71.3], @world.highest_inflation_countries(5).map(&:inflation) | |
end | |
def test_six_continents | |
assert_equal 6, @world.continents.length | |
end | |
def test_countries_belong_to_continents | |
assert_not_nil @world.countries.first.continent | |
assert_not_equal 0, @world.continents.first.countries.length | |
assert_same @world.continents.first, @world.continents.first.countries.last.continent | |
end | |
def test_you_can_query_whatever_you_want | |
europe = @world.continents.find {|c| c.name == 'Europe'} | |
assert_equal 'Germany', europe.countries.max {|a,b| a.population <=> b.population}.name | |
assert_equal 'Ukraine', europe.countries.max {|a,b| a.total_area <=> b.total_area}.name | |
# ..and so on | |
end | |
end |
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
require 'rexml/document' | |
class Continent | |
attr_reader :name, :countries | |
def initialize(element) | |
@name = element.attributes['name'] | |
@countries = [] | |
end | |
end | |
class Country | |
attr_reader :name, :population, :inflation, :total_area | |
attr_reader :continent | |
def initialize(world, element) | |
@name = element.attributes['name'] | |
@population = element.attributes['population'].to_i | |
@inflation = element.attributes['inflation'].to_f | |
@total_area = element.attributes['total_area'].to_f | |
located_within = element.attributes['continent'] | |
@continent = world.continents.find {|c| c.name == located_within } | |
@continent.countries.push self | |
end | |
end | |
class World | |
attr_reader :continents, :countries | |
def self.current | |
@world ||= World.new | |
end | |
def initialize(cia_file='cia-1996.xml') | |
# Yes, it will be slow, but it is done only once. So why sacrifice the Ruby-ish way! | |
@continents, @countries = [], [] | |
doc = REXML::Document.new File.new cia_file | |
doc.elements.each('cia/continent') { |e| @continents.push Continent.new(e) } | |
doc.elements.each('cia/country') { |e| @countries.push Country.new(self, e) } | |
end | |
def most_populated_country | |
countries.max { |a,b| a.population <=> b.population } | |
end | |
def highest_inflation_countries(top) | |
countries.sort_by {|c| -c.inflation }.first top | |
end | |
end |
Nah! I excluded the XML. Please take the newer Gist.
such a nice and clean code !
=P
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry for including the XML :)
Just wanted to make sure it will "just run" on your side.