Build Your Own World Almanac in 7 Lines of Ruby (w/ the factbook Gem)

Hello,

   The lastest (and greatest) version of the factbook library [1],
that is, v1.1.1
now includes a new almanac class that lets you build your own world almanac
e.g. listing all the world's 260+ countries and territories with
capitals, population,
internet users, mobile telephone subscripts/100, religions, ethnic
groups, languages, and so on. See ALMANAC.md [2] as an example.

  How to build your own in three steps:

Step 1: Get a copy of the factbook.json archive

   Use git clone (or unpack the zip archive) using
factbook/factbook.json [3] github repo.

Step 2: Load the profiles using the Almanac class

   almanac = Factbook::Almanac.from_json( Factbook.codes.countries,
json_dir: './factbook.json' )

Step 3: Use your template of choice and render / build the Almanac text corpus

  TEMPLATE = <<EOS
  ### <%= page.name %>

  <%= page.capital %> • <%= page.area %> • pop. <%= page.population %>
  ...
  EOS

  puts almanac.render( TEMPLATE )

That's it. See the scripts/almanac.rb [4] as an example. Cheers.

  PS: Bonus Question - Name the largest country in Africa:

  A) Nigeria
  B) South Africa
  C) Kongo
  D) Algeria Anyone?

[1] https://github.com/worlddb/factbook
[2] https://github.com/factbook/factbook.json/blob/master/ALMANAC.md
[3] https://github.com/factbook/factbook.json
[4] https://github.com/worlddb/factbook/blob/master/script/almanac.rb

Kongo?

···

-----Original Message-----
From: "Gerald Bauer" <gerald.bauer@gmail.com>
Sent: ‎11/‎9/‎2015 3:27 PM
To: "Ruby users" <ruby-talk@ruby-lang.org>
Subject: Build Your Own World Almanac in 7 Lines of Ruby (w/ the factbook Gem)

Hello,

   The lastest (and greatest) version of the factbook library [1],
that is, v1.1.1
now includes a new almanac class that lets you build your own world almanac
e.g. listing all the world's 260+ countries and territories with
capitals, population,
internet users, mobile telephone subscripts/100, religions, ethnic
groups, languages, and so on. See ALMANAC.md [2] as an example.

  How to build your own in three steps:

Step 1: Get a copy of the factbook.json archive

   Use git clone (or unpack the zip archive) using
factbook/factbook.json [3] github repo.

Step 2: Load the profiles using the Almanac class

   almanac = Factbook::Almanac.from_json( Factbook.codes.countries,
json_dir: './factbook.json' )

Step 3: Use your template of choice and render / build the Almanac text corpus

  TEMPLATE = <<EOS
  ### <%= page.name %>

  <%= page.capital %> • <%= page.area %> • pop. <%= page.population %>
  ...
  EOS

  puts almanac.render( TEMPLATE )

That's it. See the scripts/almanac.rb [4] as an example. Cheers.

  PS: Bonus Question - Name the largest country in Africa:

  A) Nigeria
  B) South Africa
  C) Kongo
  D) Algeria Anyone?

[1] https://github.com/worlddb/factbook
[2] https://github.com/factbook/factbook.json/blob/master/ALMANAC.md
[3] https://github.com/factbook/factbook.json
[4] https://github.com/worlddb/factbook/blob/master/script/almanac.rb

Hello,
  Try:

africa = Factbook::JsonPageReader.new( './factbook.json'
).read_pages( Factbook.codes.africa )

africa.sort_by! do |l,r]
  ## needs to convert 2,381,741 sq km to number
  ## use quick hack e.g. remove , from string
  r.area.gsub(',','').to_i <=> l.area.gsub(',','').to_i
end

puts africa[0].name

  And Ruby will tell you the answer :wink: Cheers.