Skip to content

Instantly share code, notes, and snippets.

@danielpowell4
Created August 30, 2016 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpowell4/368c997ca6e2c43e521267a291a0eb24 to your computer and use it in GitHub Desktop.
Save danielpowell4/368c997ca6e2c43e521267a291a0eb24 to your computer and use it in GitHub Desktop.
Returns a sentence reversing words if they have 5 or more 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