Created
August 30, 2016 05:27
-
-
Save danielpowell4/368c997ca6e2c43e521267a291a0eb24 to your computer and use it in GitHub Desktop.
Returns a sentence reversing words if they have 5 or more characters
This file contains 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
# best answer | |
def spinWords(string) | |
string.gsub(/\w{5,}/, &:reverse) | |
end | |
# top answer | |
def spinWords(string) | |
string.split.map { |s| s.length >= 5 ? s.reverse : s }.join ' ' | |
end | |
# another answer | |
def spinWords(string) | |
words = [] | |
string.split.each do |word| | |
if word.length >= 5 | |
words << word.reverse | |
else | |
words << word | |
end | |
end | |
return words.join(' ') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment