简体   繁体   English

Ruby:Pig拉丁语翻译器中的字符串范围

[英]Ruby: String Ranges in a Pig Latin translator

I am currently doing Test First's rspec tutorial and have a question pertaining to the Pig_Latin problem. 我目前正在做Test First的rspec教程,并且有一个与Pig_Latin问题有关的问题。

Specifically I want to know about string ranges. 具体来说,我想了解字符串范围。 Here is a section of my code: 这是我的代码的一部分:

if phonemes.include?(word[0]) && phonemes.include?(word[1]) && phonemes.include?(word[2])
 <do something>
end

Instead of the above I tried: 而不是上面的我尝试:

if phonemes.include?(word[0..2]) # i added that character to the list of phonemes 
  <do something>                 # e.g. if the word is school i added "sch" to 
end                              # the array called phonemes

However it does not work even though "sch" is in phonemes and word[0..2] == "sch" 但是,即使"sch"phonemesword[0..2] == "sch"它也不起作用

My question is why can I not use string ranges to manipulate the result. 我的问题是为什么我不能使用字符串范围来操纵结果。 (i will post my full code at the bottom in case this is unclear) (如果不清楚,我将在底部显示完整的代码)

Code (work in progress): 代码(正在进行中):

def translate(string)
array = string.split(" ")
alphabet = ("a".."z").to_a
vowels = ["a", "e", "i", "o", "u"]
phonemes = alphabet - vowels
phonemes << ["qu", "sch", "thr"]
result = []
array.each do |word|
    if vowels.include?(word[0])
        result << (word + "ay")
    elsif phonemes.include?(word[0..1])
        result << "do something"
    elsif phonemes.include?(word[0]) && phonemes.include?(word[1]) && phonemes.include?(word[2])
        result << (word[3..-1] + (word[0..2] + "ay"))
    elsif phonemes.include?(word[0]) && phonemes.include?(word[1])
        result << (word[2..-1] + (word[0..1] + "ay"))
    elsif phonemes.include?(word[0..1])
        result << "do something else"
    elsif phonemes.include?(word[0])
        result << (word[1..-1] + (word[0]+ "ay"))
    end
end
return result.join(" ")
end

As always tips to make the code more efficient would be appreciated (but the most important thing for me is to understand why string ranges aren't working). 与往常一样,使代码更有效的技巧将不胜感激(但是对我而言,最重要的是要了解为什么字符串范围不起作用)。 Thanks. 谢谢。

Your statement phonemes << ["qu", "sch", "thr"] is adding that array as the last element of phonemes , which is why the include? 您的表述phonemes << ["qu", "sch", "thr"]将该数组添加为phonemes的最后一个元素,这就是为什么要include?原因include? is failing. 失败了。 The << operator is designed to add individual elements to the array. <<运算符旨在将单个元素添加到数组。 If you want to add all the elements in that array to phonemes you can use the += operator, instead. 如果要将该数组中的所有元素添加到phonemes ,则可以使用+=运算符。

This is not an answer to your main question, but you asked for tips to improve your code. 这不是您的主要问题的答案,但您要求提供一些技巧来改进您的代码。 I suggest you consider using a case statement where you have your long if-else. 我建议您考虑使用一个if-else长的case语句。 It makes it more readable and reduces duplication. 它使其更具可读性并减少重复。 Something like this: 像这样:

result << case
  when vowels.include?(word[0])
    word + "ay"
  when phonemes.include?(word[0..1])
    "do something"
  when phonemes.include?(word[0]) && phonemes.include?(word[1])
    if phonemes.include?(word[2])
      word[3..-1] + word[0..2] + "ay"
    else
      word[2..-1] + word[0..1] + "ay"
    end
  when phonemes.include?(word[0..1])
    "do something else"
  when phonemes.include?(word[0])
    word[1..-1] + word[0]+ "ay"
  else
    "do something else or raise an error if you reach this point."
end

I did not look at your code closely, but I did notice that you have phonemes.include?(word[0..1]) twice, so the second one will never be executed. 我没有仔细查看您的代码,但是我确实注意到您有两次phonemes.include?(word[0..1]) ,因此第二个将永远不会执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM