本文介绍了Rails 4 - 语法错误,意外的 tIDENTIFIER,期望输入结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码试图用 rspec 运行.

需要'spec_helper'描述用户"做{ @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") }主题 { @user }它 { 应该 response_to(:name)}结尾

我收到以下错误

c:\Sites\sample_app>rspec spec/models/user_spec.rbC:/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: 语法错误,意外的 tIDENTIFIER,期望输入结束 (SyntaxError)在 { @user...er' 之前

现在我在一个新文件中重新输入测试并运行它,没有错误.我想有一些编码问题或者我没有学到的东西.有没有人可以对这个问题有所了解?

解决方案

您似乎确实被零宽度空间问题"所困扰.我知道,这非常令人沮丧.你会认为显示空白"应该解决这个问题,特别是在像 Sublime 这样先进的编辑器中.

无论如何,要检测 Sublime 2 中这些字符的存在,您可以使用这个插件来显示它们.>

顺便说一句,几个月前,当我从一个网站上复制/粘贴一些代码时,我在这个问题上浪费了数小时甚至数天.

我在 Sublime 2 网站上搜索了与此相关的任何帖子(例如功能请求等),但没有找到任何内容,所以我不知道这是否在他们的关注范围内.

注意:此答案的原始版本假定零宽度空格字符与不间断空格相同,但事实并非如此.不间断空格虽然仍会产生语法错误,但更容易检测,因为它们在您的显示中占用空间"(即看起来像"一个空格).还有零宽度不间断空间",它仍然不同.虽然在 Mac 上输入不间断空格(即 Option-Space)很容易,但我知道无法输入任何零宽度空格.

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?

解决方案

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.

这篇关于Rails 4 - 语法错误,意外的 tIDENTIFIER,期望输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:27