-
-
Save jcsalterego/5234470 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Changes from the original:
#inject
;[]
is the starting value forfinal_pigs
, andword
is all the elements withinphrase.split
; for more information, check the docs[0]
instead of#at(0)
; I don't know any good reason to use#at
new_string << new_string.shift
line, but I can't think of a better way to do that now#inject
block,final_pigs
is used as basically an implicit return of what the nextfinal_pigs
values should be; this is part of the usage of#inject