Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Forked from zachgersh/pig_latin.rb
Last active December 15, 2015 08:58
Show Gist options
  • Save jcsalterego/5234470 to your computer and use it in GitHub Desktop.
Save jcsalterego/5234470 to your computer and use it in GitHub Desktop.
def translate(phrase)
phrase.split.inject([]) do |final_pigs, word|
char = word.split(//)
if char[0].match(/[aeiou]/)
final_pigs << "#{word}ay"
else
new_string = char
until new_string[0].match(/[aeio]/)
new_string << new_string.shift
end
final_pigs << new_string.join + "ay"
end
final_pigs
end
end
@zachgersh
Copy link

thanks @jcsalterego Really interesting use of inject! I really hadn't considered doing the summation of [ ] and word.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment