Skip to content

Instantly share code, notes, and snippets.

@andersr
Created October 13, 2013 16:28
Show Gist options
  • Save andersr/6964166 to your computer and use it in GitHub Desktop.
Save andersr/6964166 to your computer and use it in GitHub Desktop.
# Write a method that takes a sentence and returns it with each word reversed in place.
my_sentence = "This is a sentence we'll use to reverse each word in the sentence."
def reversed_words(sentence)
reversed_words_sentence = sentence.split(/[" ",.]/).collect do |word|
word.reverse
end
reversed_words_sentence.join(" ")
end
puts reversed_words(my_sentence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment