rspec:语法错误,意外的keyword_end,期望结束输入(SyntaxError)

时间:2021-10-20 22:54:38

This is completely stumping me. Similar threads have suggested this error message:

这完全把我难住了。类似的线程提示了这个错误消息:

syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)

语法错误,意外的keyword_end,期望结束输入(SyntaxError)

is because I am missing an "end" somewhere in my rspec file. I have combed through it and can't see where an end would be suitable. Here is my program and spec file:

是因为我在rspec文件中漏掉了一个“结尾”。我已经梳理过了,看不出有什么地方适合。这是我的程序和规格文件:

require "calculator"

describe "add" do
  it "adds 0 and 0" do
    add(0,0).should == 0
  end

  it "adds 2 and 2" do
    add(2,2).should == 4
  end

  it "adds positive numbers" do
    add(2,6).should == 8
  end
end

describe "subtract" do
  it "subtracts numbers" do
    subtract(10,4).should == 6
  end
end

describe "sum" do
  it "computes the sum of an empty array" do
    sum([]).should == 0
  end

  it "computes the sum of an array of one number" do
    sum([7]).should == 7
  end

  it "computes the sum of an array of two numbers" do
    sum([7,11]).should == 18
  end

  it "computes the sum of an array of many numbers" do
    sum([1,3,5,7,9]).should == 25
  end
end

describe "#multiply" do

  it "multiplies two numbers" do
      mutiply([3, 4]).should == 12
  end

  it "multiplies several numbers"
    multiply([2, 5, 8]).should == 80
  end
end
describe "#power" do
  it "raises one number to the power of another number" do
      power(2, 3).should == 8
  end
end

describe "#factorial" do
  it "computes the factorial of 0" do
      factorial(0).should == 1
  end
  it "computes the factorial of 1"
        factorial(1).should == 1
  end
  it "computes the factorial of 2"
        factorial(2).should == 2
  end
  it "computes the factorial of 5"
        factorial(5).should == 120
  end
  it "computes the factorial of 10"
    factorial(10).should == 3628800
  end
end

My code:

我的代码:

def add(x, y)
  x + y
end

def subtract(x, y)
  x - y
end

def sum(an_array)
  if an_array = []
    return 0
  else
  sum = 0
  an_array.each { |n| sum += n }
  sum
  end
end

def multiply(some_array)
  if some_array == []
    return 0
  else
  product = 1
  some_array.each { |n| product = product * n }
  product
  end
end

def power(base, exp)
  out = 1
  exp.times { out = out * base }
  out
end

def factorial(n)
  if n = 0
    1
  else
    n * factorial(n - 1)
  end
end

The full error message is:

完整的错误信息是:

Juliuss-MacBook-Pro:02_calculator juliushamilton$ rake
(in /Users/juliushamilton/learn_ruby-master)
/Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load': /Users/juliushamilton/learn_ruby-master/02_calculator/calculator_spec.rb:89: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)
    from /Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /Users/juliushamilton/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
/Users/juliushamilton/.rvm/rubies/ruby-2.0.0-p451/bin/ruby -S rspec /Users/juliushamilton/learn_ruby-master/02_calculator/calculator_spec.rb -I/Users/juliushamilton/learn_ruby-master/02_calculator -I/Users/juliushamilton/learn_ruby-master/02_calculator/solution -f documentation -r ./rspec_config failed

Thanks very much.

非常感谢。

1 个解决方案

#1


3  

Got it..

明白了. .

Look below -

看下面,

describe "#multiply" do

  it "multiplies two numbers" do
    mutiply([3, 4]).should == 12
  end

  it "multiplies several numbers"  # you missed "do" here
    multiply([2, 5, 8]).should == 80
  end
end

Corrected one is -

纠正一个是- - - - - -

describe "#multiply" do

  it "multiplies two numbers" do
    mutiply([3, 4]).should == 12
  end

  it "multiplies several numbers" do
    multiply([2, 5, 8]).should == 80
  end
end

This things can be easily find out by using editor specifc indentation. I use Gvim, where I used gg=G, which has shown me where is wrong indentation. You can use your editor specific one to do the same.

通过使用editor specifc缩进可以很容易地发现这一点。我使用Gvim,在这里我使用了gg=G,它显示了错误的缩进。您可以使用特定的编辑器来执行相同的操作。

#1


3  

Got it..

明白了. .

Look below -

看下面,

describe "#multiply" do

  it "multiplies two numbers" do
    mutiply([3, 4]).should == 12
  end

  it "multiplies several numbers"  # you missed "do" here
    multiply([2, 5, 8]).should == 80
  end
end

Corrected one is -

纠正一个是- - - - - -

describe "#multiply" do

  it "multiplies two numbers" do
    mutiply([3, 4]).should == 12
  end

  it "multiplies several numbers" do
    multiply([2, 5, 8]).should == 80
  end
end

This things can be easily find out by using editor specifc indentation. I use Gvim, where I used gg=G, which has shown me where is wrong indentation. You can use your editor specific one to do the same.

通过使用editor specifc缩进可以很容易地发现这一点。我使用Gvim,在这里我使用了gg=G,它显示了错误的缩进。您可以使用特定的编辑器来执行相同的操作。