A. Desai
A. Desai

Reputation: 1

Encountering: syntax error, unexpected tIDENTIFIER, expecting keyword_end

I'm creating a directed_graph class in Ruby to practice using RSpec. I keep getting the above error (at line 13, which is the line below with "eql(0)" on it).

I don't really understand the error, especially since this RSpec code looks very similar to other RSpec code I've written for other projects that works.

require "directed_graph"
include directed_graph

describe directed_graph do

    describe ".vertices" do
        context "given an empty graph" do
            it "returns an empty hash" do
                g = directed_graph.new()
                expect(g.vertices().length()).to() eql(0)
            end
        end
    end

end

EDIT: I believe the problem was (1) directed_graph was a class, and classes must start with uppercase letters (so I renamed is DirectedGraph), and (2) you're not supposed to write "include" for classes.

I fixed those two, and my code seems to be runnign fine for now. I'm going to leave this up here in case I missed something big.

Upvotes: 0

Views: 433

Answers (1)

Greg Answer
Greg Answer

Reputation: 717

I believe the code should look like this:

require "directed_graph"
include DirectedGraph

describe DirectedGraph do
  describe ".vertices" do
    context "given an empty graph" do
      it "returns an empty hash" do
        expect(directed_graph.new.vertices.length).to eql(0)
      end
    end
  end
end

Let me explain why. First include usually includes Classes/Modules. Classes and modules in ruby are denoted with Capital Letters for each part of their name (also know as UpperCamelCase). When you describe a class in in rspec, you should also use UpperCamelCase. I also cleaned up the code a little bit to make it easier to read. You don't always need the () to denote a function. It is implied. But sometimes you do need it, for example with the expect function.

Upvotes: 0

Related Questions