1

我目前正在学习 Ruby,并且在我正在使用的教程中遇到了一个奇怪的问题。我正在使用Learn Ruby The Hard Way 中的这个练习,它不断产生语法错误,我不知道为什么。

我尝试使用的代码是

    puts <<-'HERE'
        There's something going on here.
        With the PARAGRAPH thing
        We'll be able to type as much as we like.
        Even 4 lines if we want, or 5, or 6.
    HERE

但是,它总是会产生以下语法错误

ex9.rb:12: syntax error, unexpected tIDENTIFIER, expecting $end
We'll be able to type as much as we like.
     ^

任何帮助,将不胜感激!我正在使用 TextWrangler,TextWrangler将其解析为块引用,但 Ruby 不是。

4

3 回答 3

2

它适用于 ruby​​-1.9.3-p194:

[1] pry(main)> puts <<-'HERE'
[1] pry(main)*         There's something going on here.
[1] pry(main)*         With the PARAGRAPH thing
[1] pry(main)*         We'll be able to type as much as we like.
[1] pry(main)*         Even 4 lines if we want, or 5, or 6.
[1] pry(main)*     HERE
        There's something going on here.
        With the PARAGRAPH thing
        We'll be able to type as much as we like.
        Even 4 lines if we want, or 5, or 6.
=> nil

我会怀疑隐藏字符或编码问题。--encoding检查您的语言环境,或在启动解释器时尝试使用标志。

于 2012-06-09T17:16:34.773 回答
1

尽管在您发布的代码片段中看起来不像,但您可能确实在-in的两侧或一侧有一个空格<<-'HERE'

如果这样做,您可能会看到如下警告:

test.rb:1: warning: `<<' after local variable is interpreted as binary operator
test.rb:1: warning: even though it seems like here document
test.rb:4: syntax error, unexpected tIDENTIFIER, expecting $end
        We'll be able to type as much as we like.
             ^
于 2012-06-09T17:30:52.937 回答
1

我遇到了这个问题,结果发现上面示例中的底部文件标记“HERE”没有靠在左边距(无论如何在笔记本中)。

它前面的任何空格,或制表符,它拒绝点空白工作。

于 2012-10-16T14:25:00.960 回答