Skip to content

Instantly share code, notes, and snippets.

@martin2110
Created October 14, 2015 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin2110/1f292d249a05ca859ee3 to your computer and use it in GitHub Desktop.
Save martin2110/1f292d249a05ca859ee3 to your computer and use it in GitHub Desktop.
def initialize
require 'pp'
@word_count = 0
sentence = sentence_to_arr("I The greatest gift of life is friendship, and I have received it.")
puts speak_goat(sentence)
end
def speak_goat(sentence)
sentence.each_with_index do | word, i |
sentence[i] = transform_word(word)
end
sentence.join(' ')
end
def sentence_to_arr(sentence)
sentence.split(/\s+/)
end
def begins_with_consonant?(word)
consonant?(word[0].downcase)
end
def ends_with_punctuation?(word)
punctuation?(word[-1])
end
def punctuation?(char)
char =~ /[[:punct:]]/
end
def consonant?(char)
['a','e','i','o','u'].include?(char.downcase)
end
def transform_word(word)
@word_count += 1
punctuation = ''
first_char = ''
char = word.split(//)
if ends_with_punctuation?(word)
punctuation = char.pop
end
if begins_with_consonant?(word)
first_char = char.shift
else
end
char = char.join("")
char + first_char + 'ma' + ('a'* @word_count ) + punctuation
end
end
GoatLatin.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment