- Convert data from a file into Ruby collections
- Restructure array and hash data
Your friend JJ just moved to Japan and loves it. However, sometimes he gets
confused because his new friends text him emoticons that he doesn't recognize,
like \(◎o◎)/!
and ((d[-_-]b))
.
He asked you to create a method that will translate these emoticons to English.
He also asked you to create a method that will convert his English emoticons,
like :)
, into their Japanese equivalents so that he can look cool in texts to
his new friends.
-
Write a method,
load_library
, that loads theemoticons.yml
file. -
Write a method,
get_japanese_emoticon
, that will take a traditional Western emoticon, like:)
and translate it to its Japanese version. It will relyload_library
to work. Refer to the table below for translations. -
Write a method,
get_english_meaning
, that takes a Japanese emoticon and returns its meaning in English. This method will also rely onload_library
to first load the YAML file.
Meaning | English | Japanese |
---|---|---|
angel | O:) | ☜(⌒▽⌒)☞ |
angry | >:( | ヽ(o`皿′o)ノ |
bored | :O | (ΘεΘ;) |
confused | %) | (゜.゜) |
embarrassed | :$ | (#^.^#) |
fish | ><> | >゜))))彡 |
glasses | 8D | (^0_0^) |
grinning | =D | ( ̄ー ̄) |
happy | :) | (^v^) |
kiss | :* | (*^3^)/~☆ |
sad | :'( | (T ▽ T) |
surprised | :o | o_O |
wink | ;) | (^_-) |
YAML is a recursive acronym for "YAML Ain't Markup Language". YAML is used because it is easier for humans to read and write than typing out entire arrays, hashes, etc.
For instance, take this fruit YAML file:
# fruits.yml
- Apple
- Orange
- Strawberry
- Mango
When Ruby loads the the YAML file above, the list of fruits would become an array:
require "yaml"
fruits = YAML.load_file('fruits.yml')
fruits
# => ["Apple","Orange","Strawberry","Mango"]
Another example could be a hash:
# government.yml
president: Barack Obama
vice president: Joe Biden
secretary of state: John Kerry
secretary of the treasury: Jacob Lew
When Ruby loads the the YAML file above, the list of position titles and names would become a hash of keys and values:
require "yaml"
gov = YAML.load_file('government.yml')
gov
# =>
# {
# "president" => "Barack Obama",
# "vice president" => "Joe Biden",
# "secretary of state" => "John Kerry",
# "secretary of the treasury" => "Jacob Lew"
# }
A YAML file has an extension of .yml
. For more info about YAML syntax, see
Ansible's docs. You can read more about YAML
on the Wikipedia page.
This is a test-driven lab so just get those specs to pass! The first step will
be to load the YAML file in the lib/
folder. Check out the resources below for
help loading a YAML file.
Important: When defining hash keys, depending on the syntax that you use, Ruby may automatically convert a given String key into a Symbol. So, for instance, if we were to open IRB and declare a hash using the hash-rocket, the resulting key remains a String:
hash = {"get_emoticon" => {}}
hash #=> {"get_emoticon"=>{}}
However, if the alternate syntax is used, the key will be converted:
hash = {"get_emoticon": {}}
hash #=> {:get_emoticon=>{}}
Keep this in mind as you work on this lab. The tests will accept either, but you will need to be consistent in your own code when referencing hash keys. YAML will not convert the emoticons to symbols when reading emoticons.yml
.
View Emoticon Translator on Learn.co and start learning to code for free.