ruby single word pig latin with regex -
calling forth ruby regex masters!
i trying solve problem in ruby using regex. input single word. 2 conditions apply:
- if starts vowels [aeiou], returns word + 'way'. example,
pig_latin('aye') #=> 'ayeway'
- if starts consonants, moves first letter of word end of string, + 'ay'.
pig_latin('map') #=> 'apmay'
i have tried:
def pig_latin(ay) ay.gsub(/\a[aeiou](w+)/, '\2\1way') end pig_latin('map') #=> 'map' pig_latin('aye') #=> 'aye'
when tried
ay.gsub(/^[aeiou](\w+)*/, '\2\1way') pig_latin('map') #=> 'map' pig_latin('aye') #=> 'yeway'
getting close. @ least recognizes consonants - removes 'a' in 'aye'.
i have tried reading regex doc, on gsub , rubular helps illuminate bit, still in dark ages.
in case 2 gsub
calls allowed:
def pig_latin(ay) ay[0] =~ /[aeiou]/ ? ay.gsub(/([aeiou])(\w+)*/, '\1\2way') : ay.gsub(/([^aeiou])(\w+)*/, '\2\1ay') end pig_latin("aye") #=> ayeway pig_latin("map") #=> apmay
in case 2 gsub
calls not allowed:
def pig_latin(ay) ay += ( (ay[0] =~ /[aeiou]/).nil? ? "" : "w") ay.gsub(/([^aeiou]?)([aeiou])(\w+)(.?)/, '\2\3\1\4ay') end pig_latin("aye") #=> ayeway pig_latin("map") #=> apmay
Comments
Post a Comment