Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created June 28, 2018 09:59
Show Gist options
  • Save rodloboz/a2b02170cd187ef18e6f51d0592cad50 to your computer and use it in GitHub Desktop.
Save rodloboz/a2b02170cd187ef18e6f51d0592cad50 to your computer and use it in GitHub Desktop.
Acronymize with #map
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