Skip to content

Instantly share code, notes, and snippets.

@erikpukinskis
Created March 18, 2014 17:20
Show Gist options
  • Save erikpukinskis/9624771 to your computer and use it in GitHub Desktop.
Save erikpukinskis/9624771 to your computer and use it in GitHub Desktop.
def translate(english)
english.gsub(/[A-Za-z]+/) do |word|
translate_word(word)
end
end
def translate_word(english)
first = english[0]
remainder = english[1, english.length]
pig = remainder + first.downcase + 'ay'
if /[[:upper:]]/.match(first)
pig = pig.capitalize
end
pig
end
puts "\n"
{
'hello' => 'ellohay',
'bird' => 'irdbay',
'hello world' => 'ellohay orldway',
'Hello world' => 'Ellohay orldway',
'Hello, world!!' => 'Ellohay, orldway!!',
}.each do |english, pig|
translation = translate(english)
grade = (translation == pig) ? 'OK!' : 'wrong.'
puts "#{english} => #{translation} #{grade} (expected #{pig})"
end
puts "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment