1
votes

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

syntax error, unexpected keyword_end, expecting end-of-input (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:

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
Error is the line calculator_spec.rb:89: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError).. Which you didn't include in the post. Check line# 89Arup Rakshit
@user3624730 What is your question?sawa
@sawa He is not able to find the reason of syntax error. But the spec code is not till line# 89Arup Rakshit
Sorry, it actually is, I didn't include a lot of comments before the code that were added by someone who gave me the assignment.user3624730
Line 89 is the end of the "multiply" method - the last 'end'user3624730

1 Answers

4
votes

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.