Created
June 28, 2018 09:59
-
-
Save rodloboz/a2b02170cd187ef18e6f51d0592cad50 to your computer and use it in GitHub Desktop.
Acronymize with #map
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
def acronymize(sentence) | |
# map => [] | |
# applies transformations to elements | |
# 1. split sentence into words (list use Array) | |
words = sentence.split | |
# 2. Go through/Take each word | |
# and take/grab the first letter (capitalize) | |
initials = words.map { |word| word[0].capitalize } | |
# 3. Join words into a single sentence | |
initials.join | |
end | |
# TDD with Rspec | |
describe "#acronymize" do | |
it "should return a string" do | |
actual = acronymize("away from keyboard").class.to_s | |
expected = "String" | |
expect(actual).to eq(expected) | |
end | |
it "should return an empty string when passed an empty string" do | |
actual = acronymize("") | |
expected = "" | |
expect(actual).to eq(expected) # returns true/false | |
end | |
it "returns the acronym on downcased sentences" do | |
actual = acronymize("working from home") | |
expected = "WFH" | |
expect(actual).to eq(expected) | |
end | |
it "returns the acronym on upcased sentences" do | |
actual = acronymize("AWAY FROM KEYBOARD") | |
expected = "AFK" | |
expect(actual).to eq(expected) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment