Ruby map function for "pig latin" -


i attempting write ruby function takes in string of words , turns pig latin. breaking string array, , attempting iterate on each element. when "eat pie" put in, result "eat ie", unsure why.

string = "eat pie"     array = string.split(" ")  array.map |word|    if word[0].chr == "a" || word[0].chr == "e" || word[0].chr == "i" || word[0].chr == "o" || word[0].chr == "u"     word = word + "ay"   elsif word[1].chr == "a" || word[1].chr == "i" || word[1].chr == "o" || word[1].chr == "u"   temp = word[0]   word[0] = ""   word = word + temp   word = word + "ay"  elsif word[2].chr == "a" || word[2].chr == "i" || word[2].chr == "o" || word[2].chr == "u"   temp = word[0] + word[1]   word[0] = ""   word[0] = ""   word = word + temp    word = word + "ay"  else ## 3 consonants   temp = word[0] + word[1] + word[2]   word[0] = ""   word[0] = ""   word[0] = ""   word = word + temp    word = word + "ay"   end ## end of if statement  end ## end of iteration   puts array.join(" ") 

agree other answers supplied, here's less verbose version of code in case helps you.

input = 'pig latin awesome'  arr = input.split(' ').map |wrd|     if %w(a e o u).include? wrd[0]       wrd + 'ay'     elsif %w(a o u).include? wrd[1]       wrd[1..-1] + wrd[0] + 'ay'     elsif %w(a o u).include? wrd[2]       wrd[2..-1] + wrd[0] + wrd[1] + 'ay'     else       wrd[3..-1] + wrd[0] + wrd[1] + wrd[2] + 'ay'     end end.join(' ')  puts arr 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -