猪拉丁红宝石翻译问题

时间:2014-04-16 15:35:26

标签: ruby

您好我的猪拉丁码有问题,我正在使用Ruby的Learn First文件,我基本上想要做的是以下内容,如果一个单词以元音开头=&gt ;苹果会变成苹果,如果以辅音开始=> banana => ananabay,如果以两个辅音开头=> cherry => errychay,它还可以翻译两个单词=>吃馅饼会变成吃饭等等等等,这是我的代码:

def translate(string)

    vowels = [ "a" , "e" , "i" , "o" , "u"]
    alphabet = ("a" .. "z").to_a
    consonants = alphabet - vowels

    string_split = string.split

    string_split.map! do |w|
        if vowels.include?(w[0])
            w + 'ay'
        elsif consonants.include?(w[0]) &&

            consonants.include?(w[1])

            w [2..-1] + w [0..1]+ 'ay'

        elsif w [0..1] == "qu"

            w[2..-1] + "quay"

        elsif w[0..2] == "thr"

            w [3..-1]+"thray"

        elsif w[0..2]== "sch"

            w[3..-1]+"schay"

        elsif consonants.include?(w[0])

            w[ 1..-1] + w[0..0] + 'ay'
        else
                w

        end
    end

    return string_split.join(" ")

end

1 个答案:

答案 0 :(得分:0)

非常简单,elsif而非elseif

def translate(string)

  vowels = [ "a" , "e" , "i" , "o" , "u"]
  alphabet = ("a" .. "z").to_a
  consonants = alphabet - vowels

  string_split = string.split

  string_split.map! do |w|
    if vowels.include?(w[0])
      w + 'ay'
    elsif consonants.include?(w[0]) &&

        consonants.include?(w[1])

      w [2..-1] + w [0..1]+ 'ay'

    elsif w [0..1] == "qu"

      w[2..-1] + "quay"

    elsif w[0..2] == "thr"

      w [3..-1]+"thray"

    elsif w[0..2]== "sch"

      w[3..-1]+"schay"

    else consonants.include?(w[0])

      w[ 1..-1] + w[0..0] + 'ay'


    end
  end

  return string_split.join(" ")

end