Pig Latin

excerpt: This covers common tasks on string such as slicing and searching. tags: string-slice string-sequence string-search

Write function that translates a text to Pig Latin and back. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. “The quick brown fox” becomes “Hetay uickqay rownbay oxfay”.

1
2
3
4
5
6
7
8
9
def pig_latin(word)  
  if 'aeiou'.include?(word[0])
    return "#{word}way"
  end
  
  return "#{word[1..-1]}#{word[0]}ay"
end

p pig_latin('maple')