Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created October 17, 2018 16:44
Show Gist options
  • Save rodloboz/3c064fd75795afd9eed6f47d32f64713 to your computer and use it in GitHub Desktop.
Save rodloboz/3c064fd75795afd9eed6f47d32f64713 to your computer and use it in GitHub Desktop.
def acronymize(string)
# TODO: write a method that returns
# the acronym for the given string
# 1. Split the sentence into words
# 2. Grab the first letter of each word
# 3. Capitalize it
# 4. Join the first letters together
# 5. return the result
# gsub: string to replace , replace with
# split returns an array
words = string.gsub(",","").split
# acronym = []
# words.each do |word|
# acronym << word[0].capitalize
# end
# takes an array and returns a new array
# with the transformation applied
words.map { |word| word[0].capitalize }.join
# acronym.join("")
end
puts acronymize("you only live once") == "YOLO"
puts acronymize("you only live once".upcase) == "YOLO"
puts acronymize("") == ""
puts acronymize("you only , live once") == "YOLO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment