" Range" - 无法解决

时间:2016-09-15 15:59:23

标签: ruby rspec

所以这段代码的重点是翻译猪拉丁语(显然"你不算作元音)..这是一个练习。我尝试更改' for'循环值一个负载,然后尝试不同的' if / else'声明。我正在使用重要部分

来完成该功能
describe "#translate" do
  it "translates many words" do
    s = translate("the quick brown fox")
    expect(s).to eq("ethay ickquay ownbray oxfay")
  end
end
对不起,我差点忘了提一下错误是怎么出现的。如果顶部段落没有意义......我的坏,忘记提及有问题的代码已经过去

  

'#'带空格输入的方法

并且为该rspec出现的错误消息是

2) #translate translates many words
     Failure/Error: s = translate("the quick brown fox")
     ArgumentError:
       bad value for range
     # ./pig_latin.rb:25:in `translate'
     # ./pig_latin_spec.rb:64:in `block (2 levels) in <top (required)>'

我是新手并没有读过很多问题(大多只看解决方案)所以对不起我的观点有点模糊

#write your code here
def translate input
    vowels = ["a", "e", "i", "o"]
    output = input.split("")
    if (output.include?(" ") == false)
    i = 0
    #Method for inputs with no spaces
    while (i == 0) 
      #First Letter is not a vowel
      if ((vowels.include?(output[i])) == false)
          output.insert((output.length - 1), output.delete_at(i))

      else
         #Is a vowel
          output = output.join + "ay"
          i += 1
          break
      end
    end


  #Method for inputs with spaces
  else
      output = output.join.split
      for i in i..(output.length - 1)
      output[i] = output[i].split("")
        for x in x..10
          if ((vowels.include?(output[i][x])) == false)
            output[i].insert((output[i].length - 1), output[i].delete_at(x))
          else
            output[i] = output[i].join + "ay"
            i += 1
            break
          end
        end
      end
    output = output.join(" ")
    end 
    return output
end 

0 个答案:

没有答案