Created
April 30, 2016 21:22
-
-
Save lrechert/ed339d4289b04cf6c3bbfeb2c77318c9 to your computer and use it in GitHub Desktop.
Ruby Weekly Challenge - Make Acronym
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
# 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 |
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
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