Skip to content

Instantly share code, notes, and snippets.

@sgrshah
Created June 17, 2013 22:28
Show Gist options
  • Save sgrshah/5801062 to your computer and use it in GitHub Desktop.
Save sgrshah/5801062 to your computer and use it in GitHub Desktop.
Word Reverse - Sagar
# From [Rubeque](http://rubeque.com/problems/reverse-each-word)
# Write a method that takes a sentence and returns it with each word reversed in place.
# ruby
# reverse_each_word.rb
# Write a method that takes a sentence and returns it with each word reversed in place.
# A String has many methods that can be called on it:
# http://www.ruby-doc.org/core-1.9.3/String.html
def reverse_each_word(sentence)
sentence.split(" ").each{|word| word.reverse!}.join(" ")
end
puts reverse_each_word("Hello there, and how are you?").inspect
#=> "olleH ,ereht dna woh era ?uoy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment