Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created March 17, 2015 21:29
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 phlipper/52fa1ab8654482110bca to your computer and use it in GitHub Desktop.
Save phlipper/52fa1ab8654482110bca to your computer and use it in GitHub Desktop.
TECH601-00 Day 4 - Hash exercises with country data
# DRY this up!
# DRY - Don't Repeat Yourself! :)
# Data Type vs. Data Structure
#
# New Data Type: Hash (or dictionary)
# A Hash is a Data Structure
# Angola:
country0_id = "AGO"
country0_name = "Angola"
country0_population = 21_471_618
country0_capital = "Luanda"
country0_latitude = -8.81155
country0_longitude = 13.242
country0_income_level = "Upper Middle"
country0_high_income = false
country0 = {
"id" => "AGO",
"name" => "Angola",
"population" => 21_471_618,
"capital" => "Luanda",
"latitude" => -8.81155,
"longitude" => 13.242,
"income_level" => "Upper Middle",
"high_income" => false
}
puts country0["name"]
puts country0["population"]
puts country0
# ask for a non-existent key
puts country0["foo"]
# insert something new in to the dictionary
country0["foo"] = "bar"
# the value now exists
puts country0
puts country0["foo"]
# check out the keys, then the values
puts country0.keys.to_s
puts country0.values.to_s
puts country0.keys.count
puts country0.keys.length
puts country0.keys.size
puts country0.values.count
puts country0.values.length
puts country0.values.size
puts country0.keys.sort
# Antigua and Barbuda:
country1_id = "ATG"
country1_name = "Antigua and Barbuda"
country1_population = 89_985
country1_capital = "Saint John's"
country1_latitude = 17.1175
country1_longitude = -61.8456
country1_income_level = "High"
country1_high_income = true
# Belarus:
country2_id = "BLR"
country2_name = "Belarus"
country2_population = 9_466_000
country2_capital = "Minsk"
country2_latitude = 53.9678
country2_longitude = 27.5766
country2_income_level = "Upper Middle"
country2_high_income = false
# Central African Republic:
country3_id = "CAF"
country3_name = "Central African Republic"
country3_population = 4_616_417
country3_capital = "Bangui"
country3_latitude = 5.63056
country3_longitude = 21.6407
country3_income_level = "Low"
country3_high_income = false
# Switzerland:
country4_id = "CHE"
country4_name = "Switzerland"
country4_population = 8_081_482
country4_capital = "Bern"
country4_latitude = 46.948
country4_longitude = 7.44821
country4_income_level = "High"
country4_high_income = true
# is country4 population greater than country 1
# puts country4_population > country1_population
# output all of the country ids
# puts country0_id, country1_id, country2_id, country3_id, country4_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment