【问题标题】:Test-First-Ruby 04_pig_latin - getting an empty stringTest-First-Ruby 04_pig_latin - 得到一个空字符串
【发布时间】:2018-01-24 01:56:13
【问题描述】:

我正在通过 Test-First-Ruby 学习 Ruby,但我为程序编写的一些将单词翻译成猪拉丁语的代码遇到了问题。我没有返回翻译后的单词,而是得到一个空字符串。这里依次是我的代码、测试它的规范文件,以及我尝试在终端中运行规范文件时得到的失败/错误代码(Mac OS 10.12、Ruby 2.5.0)

代码如下:

def translate(string)
array = string.split(" ")
pigged_array = array.map! {|x| pigify(x)}
result = pigged_array.join(" ")
return result
end

def pigify(word)
vowels = ["a", "e", "i", "o", "u"]

  if vowels.include? word[0].downcase
    puts word + "ay"

 # two cases for "qu"
  elsif word[0..1] == "qu"
    puts word[2..-1] + "quay"
  elsif word[1..2] == "qu"
    puts word[3..-1] + word[0..2] + "ay"

# for words that start with 3 consonants
  elsif !(vowels.include? word[0]) && !(vowels.include? word[1]) && !(vowels.include? word[2])
    puts word[3..-1] + word[0..2] + "ay"

# for words that start with 2 consonants
  elsif !(vowels.include? word[0]) && !(vowels.include? word[1]) # for 2
    puts word[2..-1] + word[0..1] + "ay"

# for words that start with a single consonant
  else
    puts word[1..-1] + word[0] + "ay"
  end
end

这是规范文件:

require "04_pig_latin"

describe "#translate" do
  it "translates a word beginning with a vowel" do
    s = translate("apple")
    expect(s).to eq("appleay")
  end

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

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

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

  it "translates a word beginning with three consonants" do
    expect(translate("three")).to eq("eethray")
  end

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

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

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

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

最后,这是我在终端中运行规范文件时收到的消息:

#translate

appleay
  #translates a word beginning with a vowel (FAILED - 1)

Failures:

  1) #translate translates a word beginning with a vowel
     Failure/Error: expect(s).to eq("appleay")

       expected: "appleay"
            got: ""

       (compared using ==)
     # ./spec/04_pig_latin_spec.rb:29:in `block (2 levels) in <top 
(required)>'

Finished in 0.00168 seconds (files took 0.11091 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/04_pig_latin_spec.rb:27 # #translate translates a word beginning with a vowel

【问题讨论】:

  • 抱歉,Sawa 不清楚。问题是“为什么我得到一个空字符串而不是翻译的单词?” SimpleLime 完美回答!

标签: ruby test-first


【解决方案1】:

您的pigify 方法是将字符串puts 发送到终端,但不会将它们返回给调用者。 puts 方法将返回 nil 并且 ruby​​ 返回方法中最后评估的行的结果。在您的pigify 中,在每个if/elsif/else 分支中,评估的最后一行是puts,因此pigify 返回nil。然后你 join 将所有 nil 值放在一起,并得到一长串空格(或者,在单个单词串的情况下,返回一个空白字符串)作为你的值。

要解决此问题,只需删除对 puts 的调用 pigify 并保留字符串:

def pigify(word)
  vowels = ["a", "e", "i", "o", "u"]

  if vowels.include? word[0].downcase
    word + "ay"
  # two cases for "qu"
  elsif word[0..1] == "qu"
    word[2..-1] + "quay"
  elsif word[1..2] == "qu"
    word[3..-1] + word[0..2] + "ay"
  # for words that start with 3 consonants
  elsif !(vowels.include? word[0]) && !(vowels.include? word[1]) && !(vowels.include? word[2])
    word[3..-1] + word[0..2] + "ay"
  # for words that start with 2 consonants
  elsif !(vowels.include? word[0]) && !(vowels.include? word[1]) # for 2
    word[2..-1] + word[0..1] + "ay"
  # for words that start with a single consonant
  else
    word[1..-1] + word[0] + "ay"
  end
end

现在每个分支中计算的最后一行是被修改的字符串,所以修改后的字符串将从方法中返回。

【讨论】:

  • 谢谢!你的解释非常清楚,我完全明白你在说什么!我忘记了 puts 返回 nil。
猜你喜欢
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多