Skip to content

Instantly share code, notes, and snippets.

@sshaw
Last active December 20, 2015 04:09
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 sshaw/6068404 to your computer and use it in GitHub Desktop.
Save sshaw/6068404 to your computer and use it in GitHub Desktop.
Generate random country data (names and codes) in Ruby: ISO, IOC, FIFA and more
require "normalize_country" # need 0.1.0
module RandomCountry
@@cache = {}
@@selector = [].respond_to?(:choice) ? :choice : :sample # :choice is for Ruby < 1.9
def self.random(format)
@@cache[format] ||= NormalizeCountry.to_a(format)
@@cache[format].send(@@selector)
end
class << self
NormalizeCountry.formats.each do |format|
define_method(format) { random(format) }
end
end
end
p RandomCountry.random(:alpha2)
p RandomCountry.alpha2
p RandomCountry.alpha3
p RandomCountry.short
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment