Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active June 23, 2020 22:54
Show Gist options
  • Save harrisonmalone/a40f9706916bdef3c972b6414944c55d to your computer and use it in GitHub Desktop.
Save harrisonmalone/a40f9706916bdef3c972b6414944c55d to your computer and use it in GitHub Desktop.
def pig(sentence)
array_of_words = sentence.split(" ")
# here we split every word in the sentence
pig_array = array_of_words.map do |word|
letter = word[0]
# here we get the first letter of each word and store it
array_of_letters = word.split("")
# here we split each of the words into an array of letters
array_of_letters.shift
# we remove the first item from the array of letters
word = array_of_letters.join("")
# we join the array of letters back again to be words in an array
"#{word}#{letter}ay"
# here we interpolate with the word, the first letter and the ay
end
result = pig_array.join(" ")
# the edit block gives us back a new array with the words we want, here we join that array back into one string
return result
# we return this result which we don't actually have to do but is good for readability
end
pig("hello there")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment