Skip to content

Instantly share code, notes, and snippets.

@lrechert
Created April 30, 2016 21:22
Show Gist options
  • Save lrechert/ed339d4289b04cf6c3bbfeb2c77318c9 to your computer and use it in GitHub Desktop.
Save lrechert/ed339d4289b04cf6c3bbfeb2c77318c9 to your computer and use it in GitHub Desktop.
Ruby Weekly Challenge - Make Acronym
# Implement a function called make_acronym that returns the first letters
# of each word in a passed in string. Make sure the letters returned are
# uppercase.
# If the value passed in is not a string return 'Not a string'
# If the value passed in is a string which contains only characters
# other than spaces and alphabet letters, return 'Not letters'
def make_acronym(input_string)
return nil unless input_string
return 'Not a string' unless input_string.is_a? String
return 'Not letters' unless (input_string =~ /^[a-z\s]*$/i)
input_string.split(" ").each_with_object(''){|e, m| m << e[0]}.upcase
end
require_relative '../make_acronym'
describe "make_acronym" do
context "when the input is a valid string" do
it "returns the first letter of each word, capitalized" do
expect(make_acronym('Hello codewarrior')).to eql 'HC'
end
end
context "when the input is not a valid string" do
it "returns 'Not a string'" do
expect(make_acronym(42)).to eql "Not a string"
end
it "returns 'Not a string'" do
expect(make_acronym([2,12])).to eql "Not a string"
end
it "returns 'Not a string'" do
expect(make_acronym({name: 'Abraham'})).to eql "Not a string"
end
context "when the input contains only non-alpha characters" do
it "returns 'Not letters'" do
expect(make_acronym('42')).to eql "Not letters"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment