secret
Last active

  • Download Gist
econimics_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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
economics.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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

Sorry for including the XML :)
Just wanted to make sure it will "just run" on your side.

Nah! I excluded the XML. Please take the newer Gist.

such a nice and clean code !

=P

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.