【问题标题】:odd rake behavior - is it broken?奇怪的 rake 行为 - 它坏了吗?
【发布时间】:2013-10-26 15:39:42
【问题描述】:

编辑:想通了。这告诉我我的红宝石正在产生一个无限循环。现在,如果我能弄清楚如何修复循环......

我运行了一个 rake 测试,这就是输出到终端的所有内容:

(in /home/macs/Desktop/projects/odin/odin3_ruby/learn_ruby)

#translate

在我将 ruby​​ 更改为此之前,测试运行良好:

def translate(x)
    vowel = /\b[aeiou]*/
    array = x.split("")

    until array[0]==vowel do
        it = array[0]
        array.push(it)
        array.delete(it)
    end
    new = array.join("")    
    new+="ay"
end

我不记得在我更改之前我的 ruby​​ 代码是什么。

如果你想看看我的 rake 文件,那就是这个。顺便说一句,我从教程网站下载了这个文件,我很肯定我根本没有改变它。

# # Topics
#
# * modules
# * strings
#
# # Pig Latin
#
# Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below) but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.
#
# Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
#
# Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.
#
# (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
#
# See <http://en.wikipedia.org/wiki/Pig_latin> for more details.
#
#

require "pig_latin"

describe "#translate" do

  it "translates a word beginning with a vowel" do
    s = translate("apple")
    s.should == "appleay"
  end

  it "translates a word beginning with a consonant" do
    s = translate("banana")
    s.should == "ananabay"
  end

  it "translates a word beginning with two consonants" do
    s = translate("cherry")
    s.should == "errychay"
  end

  it "translates two words" do
    s = translate("eat pie")
    s.should == "eatay iepay"
  end

  it "translates a word beginning with three consonants" do
    translate("three").should == "eethray"
  end

  it "counts 'sch' as a single phoneme" do
    s = translate("school")
    s.should == "oolschay"
  end

  it "counts 'qu' as a single phoneme" do
    s = translate("quiet")
    s.should == "ietquay"
  end

  it "counts 'qu' as a consonant even when it's preceded by a consonant" do
    s = translate("square")
    s.should == "aresquay"
  end

  it "translates many words" do
    s = translate("the quick brown fox")
    s.should == "ethay ickquay ownbray oxfay"
  end

  # Test-driving bonus:
  # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
  # * retain the punctuation from the original phrase

end

【问题讨论】:

    标签: ruby unit-testing tdd rake


    【解决方案1】:

    你有一个字符串数组:

    array = x.split("")
    

    但您正在将这些字符串与正则表达式进行比较:

    vowel = /\b[aeiou]*/
    #...
    until array[0]==vowel do
    

    a == b 对于每个字符串 a 和正则表达式 b 都是错误的,所以您只是以复杂的方式编写:

    until false do
    

    也许你的意思是:

    until array[0] =~ vowel do
    

    这应该会停止您的无限循环,但您的循环仍然没有多大意义。你这样做:

    it = array[0]
    array.push(it)
    array.delete(it)
    

    所以你抓住第一个元素,push 到数组的末尾,然后 deletearray 匹配 it 的所有内容(注意 Array#delete 删除 all 匹配)。为什么不只是array.delete(it)?或者,如果您只想折腾第一个条目,请使用shift

    【讨论】:

    • 谢谢,我最终让它自己工作了。不过,你的建议很有帮助。我意识到我可以改用 slice 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2014-07-31
    • 2020-01-26
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多