Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 奇怪的耙行为-是否损坏?_Ruby_Unit Testing_Tdd_Rake - Fatal编程技术网

Ruby 奇怪的耙行为-是否损坏?

Ruby 奇怪的耙行为-是否损坏?,ruby,unit-testing,tdd,rake,Ruby,Unit Testing,Tdd,Rake,编辑:算出了。这告诉我我的ruby正在产生一个无限循环。现在如果我能想出如何修复这个循环 我运行了一个rake测试,这就是终端的所有输出: (in /home/macs/Desktop/projects/odin/odin3_ruby/learn_ruby) #translate 在我将ruby更改为以下内容之前,测试运行良好: def translate(x) vowel = /\b[aeiou]*/ array = x.split("") until array

编辑:算出了。这告诉我我的ruby正在产生一个无限循环。现在如果我能想出如何修复这个循环

我运行了一个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
##主题
#
#*模块
#*字符串
#
##猪拉丁语
#
#猪拉丁语是一种虚构的儿童语言,旨在让人感到困惑。它遵循一些简单的规则(如下),但如果说得很快,非儿童(和非母语人士)就很难理解。
#
#规则1:如果一个单词以元音开头,在单词末尾加上一个“ay”音。
#
#规则2:如果一个单词以辅音开头,把它移到单词的末尾,然后在单词的末尾加上一个“ay”音。
#
#(还有一些关于边缘情况的规则,也有一些地区性的变体,但这应该足以理解测试。)
#
#有关更多详细信息,请参阅。
#
#
需要“pig_拉丁语”
描述“#翻译”做什么
它“翻译一个以元音开头的单词”do
s=翻译(“苹果”)
s、 应==“应用程序”
结束
它“翻译一个以辅音开头的单词”do
s=翻译(“香蕉”)
s、 应==“ananabay”
结束
它“翻译一个以两个辅音开头的单词”吗
s=翻译(“樱桃”)
s、 should==“errychay”
结束
它“翻译两个词”吗
s=翻译(“吃馅饼”)
s、 应==“eatay iepay”
结束
它“翻译一个以三个辅音开头的单词”
翻译(“三”)。应该==“三”
结束
它“将'sch'计为单个音素”do
s=翻译(“学校”)
s、 应该==“oolschay”
结束
它“把'qu'算作单个音素”do
s=翻译(“安静”)
s、 应==“ietquay”
结束
它“把‘去’算作一个辅音,即使前面有一个辅音‘do’
s=平移(“正方形”)
s、 应==“aresquay”
结束
它“翻译很多单词”吗
s=翻译(“敏捷的棕色狐狸”)
s、 应该==“ethay ickquay ownbray oxfay”
结束
#试驾奖金:
#*编写一个测试,断言大写单词仍然大写(当然,首字母大写字母不同)
#*保留原短语的标点符号
结束

您有一个字符串数组:

array = x.split("")
但您将这些字符串与Regexp进行比较:

vowel = /\b[aeiou]*/
#...
until array[0]==vowel do
a==b
对于每个字符串
a
和Regexp
b
都是false,所以您只是以一种复杂的方式编写它:

until false do
也许你的意思是:

until array[0] =~ vowel do
这应该会停止你的无限循环,但你的循环仍然没有多大意义。您可以这样做:

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

因此,您抓取第一个元素,它位于数组的末尾,然后抓取与它匹配的
array
(注意
array#delete
删除所有匹配项)中的所有元素。为什么不干脆
数组。删除(它)
?或者,如果您只想扔掉第一个条目,请使用。

谢谢,我自己完成了它。尽管如此,你的建议还是很有帮助的。我意识到我可以用切片法代替。