I want to convert a country code like "US" to an Emoji flag, ie transform "US" string to the appropriate Unicode in Ruby.
Asked
Active
Viewed 997 times
11
-
Convert it, we don't mind. – Aleksei Matiushkin Jun 14 '18 at 14:06
-
Downvote indicates otherwise. – mahemoff Jun 14 '18 at 14:10
-
Alternatively, you could use a hash: `{ 'US' => '', 'NK' => '', ...}` – Stefan Jun 14 '18 at 14:14
-
1@Stefan Could but it's too much unnecessary code when it can be programmatically generated. Also one can hope platforms will stay up to date with any changes. – mahemoff Jun 14 '18 at 14:17
2 Answers
6
Here is a port of that to Ruby:
country = 'US'
flagOffset = 0x1F1E6
asciiOffset = 0x41
firstChar = country[0].ord - asciiOffset + flagOffset
secondChar = country[1].ord - asciiOffset + flagOffset
flag = [firstChar, secondChar].pack("U*")

vcsjones
- 138,677
- 31
- 291
- 286