Created
September 21, 2012 19:56
-
-
Save havenwood/3763549 to your computer and use it in GitHub Desktop.
Ruby 2.0 Refinements Pig Latin
This file contains 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
module PigLatin | |
refine String do | |
def to_p | |
self.split(/(\-|\s+)/).map do |snippet| | |
if snippet.match /^[a-z']+[^a-z0-9]*$/i | |
snippet.match /^(qu|[^aeiou]*)(.+?)([^a-z]*)$/i do |letter| | |
first, punc = letter[2] + letter[1], letter[3] | |
first.capitalize! if snippet.match /^[A-Z][a-z]/ | |
last = first.downcase == 'a' ? 'y' : 'ay' | |
first + last + punc | |
end | |
else | |
snippet | |
end | |
end.join | |
end | |
end | |
end | |
module UsingNothing | |
"I'm a normal string!".to_p | |
end | |
# NoMethodError: undefined method `to_p' for "I'm a normal string!":String | |
module Latinize | |
using PigLatin | |
"Look at that, it's pig latin!".to_p | |
end | |
# => "Ooklay atay atthay, it'say efinedray igpay atinlay!" | |
# Pig latin regex borrowed from igpay_atinlay gem. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment