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

时间:2014-05-17 07:20:18

标签: ruby rspec syntax-error

这完全让我感到难过。类似的线程提示此错误消息:

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

是因为我错过了我的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

我的代码:

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

完整的错误消息是:

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

非常感谢。

1 个答案:

答案 0 :(得分:3)

得到了..

看下面 -

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

更正了一个 -

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

使用编辑器特定缩进可以轻松找到这些内容。我使用 Gvim ,我使用了gg=G,这显示了我在哪里出现了错误的缩进。您可以使用特定编辑器来执行相同操作。