Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created March 18, 2015 14:02
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/6c2594bd7c2128d4d71e to your computer and use it in GitHub Desktop.
Save phlipper/6c2594bd7c2128d4d71e to your computer and use it in GitHub Desktop.
TECH601-00 Day 5 Warmup
# Convert all of the following country data in to a Hash, like that of Angola
# Angola
angola = {
"id" => "AGO",
"name" => "Angola",
"population" => 21_471_618,
"capital" => "Luanda",
"latitude" => -8.81155,
"longitude" => 13.242,
"income_level" => "Upper Middle",
"high_income" => false
}
# Antigua and Barbuda
# id: "ATG"
# name: "Antigua and Barbuda"
# population: 89_985
# capital: "Saint John's"
# latitude: 17.1175
# longitude: -61.8456
# income_level: "High"
# high_income: true
antigua_and_barbuda = {
"id" => "ATG",
"name" => "Antigua and Barbuda",
"population" => 89_985,
"capital" => "Saint John's",
"latitude" => 17.1175,
"longitude" => -61.8456,
"income_level" => "High",
"high_income" => true
}
# Belarus
# id: "BLR"
# name: "Belarus"
# population: 9_466_000
# capital: "Minsk"
# latitude: 53.9678
# longitude: 27.5766
# income_level: "Upper Middle"
# high_income: false
belarus = {
"id" => "BLR",
"name" => "Belarus",
"population" => 9_466_000,
"capital" => "Minsk",
"latitude" => 53.9678,
"longitude" => 27.5766,
"income_level" => "Upper Middle",
"high_income" => false
}
# Central African Republic
# id: "CAF"
# name: "Central African Republic"
# population: 4_616_417
# capital: "Bangui"
# latitude: 5.63056
# longitude: 21.6407
# income_level: "Low"
# high_income: false
central_african_republic = {
"id" => "CAF",
"name" => "Central African Republic",
"population" => 4_616_417,
"capital" => "Bangui",
"latitude" => 5.63056,
"longitude" => 21.6407,
"income_level" => "Low",
"high_income" => false
}
# Switzerland
# id: "CHE"
# name: "Switzerland"
# population: 8_081_482
# capital: "Bern"
# latitude: 46.948
# longitude: 7.44821
# income_level: "High"
# high_income: true
switzerland = {
"id" => "CHE",
"name" => "Switzerland",
"population" => 8_081_482,
"capital" => "Bern",
"latitude" => 46.948,
"longitude" => 7.44821,
"income_level" => "High",
"high_income" => true
}
# Output Angola
# puts angola
# Output each key/value pair for Angola
# angola.each_pair { |key, value| puts "#{key},#{value}" }
# Output each key/value pair for Switzerland
# switzerland.each_pair{|k,v|puts"#{k},#{v}"}
# Output each key/value pair for Belarus
# belarus.each_pair do |key, value|
# puts "#{key},#{value}"
# end
# a list of countries
countries = [
angola,
switzerland,
belarus,
antigua_and_barbuda,
central_african_republic
]
# loop through each country in the list
countries.each_with_index do |country, index|
puts "#{country["name"]} at index #{index}"
puts "---"
# for each country (Hash), loop through each key and value
country.each_pair do |key, value|
puts "#{key},#{value}"
end
end
puts "---"
puts "CSV"
puts "---"
# Super neat CSV / Spreadsheet example!!!
headers = countries.first.keys
puts headers.join(",")
countries.each do |country|
puts country.values.join(",")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment