Last active
August 29, 2015 14:18
-
-
Save phlipper/0efa7b643827fdaeaf3e to your computer and use it in GitHub Desktop.
TECH603 Day 2 - Working with country data and Hashes
This file contains hidden or 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
# 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 | |
headers = countries.first.keys.map { |key| %("#{key}") }.join(",") | |
puts headers | |
countries.each do |country| | |
values = country.values.map do |value| | |
%("#{value}") | |
end | |
puts values.join(",") | |
end |
This file contains hidden or 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
# 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"].inspect | |
# 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 | |
country1 = { | |
"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 | |
} | |
puts country1["longitude"] | |
puts country1["latitude"] | |
puts country1.key?("foo") | |
unless country1.key?("bar") | |
puts "I can't find the key 'bar'" | |
end | |
puts "I can't find the key 'bar'" if !country1.key?("bar") | |
country1["baz"] = nil # or nil | |
unless country1["baz"] | |
puts "The value of 'bar' is falsey" | |
end | |
# 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