Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - syntax error, unexpected tIDENTIFIER, expecting end-of-input

I have a piece of code that I am trying to run with rspec.

require 'spec_helper'

    describe "User" do
        before { @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") }
        subject { @user }

        it { should respond_to(:name)}
    end

I get the following error

c:\Sites\sample_app>rspec spec/models/user_spec.rb
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec
/core/configuration.rb:819:in `load': c:/Sites/sample_app/spec/models/user_spec.
rb:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input (SyntaxError)
before { @user...er'

Now I retype the test in a new file and run that and no error. I guess there is some encoding issue or something that I just have not learnt about that I am not getting. Is there anyone that could shed some light on this issue?

like image 998
Ryan-Neal Mes Avatar asked Sep 22 '13 16:09

Ryan-Neal Mes


1 Answers

You seem to indeed have been bitten by the "zero-width space problem". This is extremely frustrating, I know. You'd think that "show white space" should take care of the problem, particularly in an editor as advanced as Sublime.

In any event, to detect the existence of these characters in Sublime 2, you can use this plugin. to display them.

As an aside, I wasted hours if not days on this problem a few months ago when I copy/pasted some code from a website that had one of the little nasties in it.

I searched the Sublime 2 site for any postings related to this (e.g. feature requests, etc.), but didn't find anything, so I don't know if this is on their radar.

Note: The original version of this answer assumed that the zero-width space character was the same as the non-breaking space, which it is not. Non-breaking spaces, while they will still generate syntax errors, are much easier to detect since they "take up space" in your display (i.e. "look like" a space). There's also the "zero-width non-breaking space", which is different still. While it's easy to enter the non-breaking space on a Mac (i.e. Option-Space), I know of no way to enter any of the zero-width spaces.

like image 120
Peter Alfvin Avatar answered Nov 14 '22 21:11

Peter Alfvin