-2

私は 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
4

1 に答える 1

1

pigifyメソッドはputs文字列を端末に送信していますが、呼び出し元に返していません。putsメソッドは戻りnil、ruby はメソッドで最後に評価された行の結果を返します。あなたpigifyの各if/elsif/elseブランチではputs、評価された最後の行pigifynilです。次に、joinすべてのnil値をまとめて、スペースの長い文字列 (または、単一の単語文字列の場合は空白文字列) を値として取得します。

これを修正するには、putsinへの呼び出しを削除し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

各ブランチで評価される最後の行が変更される文字列になるため、変更された文字列がメソッドから返されます。

于 2018-01-24T02:09:28.110 に答える