# Ruby \<\< is ambiguos

**URL:** <https://rubytalk.org/t/ruby-is-ambiguos/34883>\
**Category:** ruby-talk\
**Created:** [28 January 2007 20:50 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883 "2007-01-28T20:50:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [28 January 2007 20:50 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/1 "2007-01-28T20:50:29Z")

</div>

Hi, I working on syntax highlighting for ED4W and have problem with \<\<  
because it has three uses.

1. \<\< operator  
2. \<\< HERE Doc  
3. class \<\< Name - Singleton Class

The problem is determining if \<\< starts a Here Doc. It is easy to handle  
class\<\<Name is this regard, but I can't see how you can work out \<\< in  
the following:

&nbsp;&nbsp;&nbsp;Ared = "mary"  
&nbsp;&nbsp;&nbsp;angy = "bill"  
&nbsp;&nbsp;&nbsp;angy \<\<Ared # does this start a Here Doc or is it operator \<\<  
&nbsp;&nbsp;&nbsp;print \<\<Ared # as above.

Thanks.

> **···**
>
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [28 January 2007 20:57 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/2 "2007-01-28T20:57:35Z")

</div>

Alle 21:50, domenica 28 gennaio 2007, Neville Franks ha scritto:

> Hi, I working on syntax highlighting for ED4W and have problem with \<\<  
> because it has three uses.
> 
> 1. \<\< operator  
> 2. \<\< HERE Doc  
> 3. class \<\< Name - Singleton Class
> 
> The problem is determining if \<\< starts a Here Doc. It is easy to handle  
> class\<\<Name is this regard, but I can't see how you can work out \<\< in  
> the following:
> 
> &nbsp;&nbsp;&nbsp;Ared = "mary"  
> &nbsp;&nbsp;&nbsp;angy = "bill"  
> &nbsp;&nbsp;&nbsp;angy \<\<Ared # does this start a Here Doc or is it operator \<\<  
> &nbsp;&nbsp;&nbsp;print \<\<Ared # as above.
> 
> Thanks.
> 
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

Some editors (kate and vim, for example) assume the presence of a whitespace  
after the \<\< in the operator case:

a=  
s="abc"  
#this gets highlighted as a HERE Doc  
a\<\<s

#this gets highlighted as an operator  
a\<\< s

Stefano

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [28 January 2007 21:10 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/3 "2007-01-28T21:10:11Z")

</div>

Neville Franks schrieb:

> Hi, I working on syntax highlighting for ED4W and have problem with \<\<  
> because it has three uses.
> 
> 1. \<\< operator  
> 2. \<\< HERE Doc  
> 3. class \<\< Name - Singleton Class
> 
> The problem is determining if \<\< starts a Here Doc. It is easy to handle  
> class\<\<Name is this regard, but I can't see how you can work out \<\< in  
> the following:
> 
> &nbsp;&nbsp;&nbsp;Ared = "mary"  
> &nbsp;&nbsp;&nbsp;angy = "bill"  
> &nbsp;&nbsp;&nbsp;angy \<\<Ared # does this start a Here Doc or is it operator \<\<  
> &nbsp;&nbsp;&nbsp;print \<\<Ared # as above.
> 
> Thanks.
> 
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

Ooops! - I Think Ruby itself has some problems with it:

irb(main):001:0\> Ared = "mary"  
=\> "mary"  
irb(main):002:0\> angy = "bill"  
=\> "bill"  
irb(main):003:0\> angy \<\<Ared  
irb(main):004:0" Ared  
=\> "mary"  
irb(main):005:0\> angy  
=\> "billmary"  
irb(main):006:0\> angy \<\< Ared  
=\> "billmarymary"  
irb(main):007:0\> angy \<\<EOT  
irb(main):008:0" xxxxx  
irb(main):009:0" EOT  
NameError: uninitialized constant EOT  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7  
irb(main):010:0\> angy  
=\> "billmarymary"  
irb(main):011:0\> angy \<\< EOT  
NameError: uninitialized constant EOT  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):11

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![Steven\_Lumos1](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Steven\_Lumos1](https://rubytalk.org/u/Steven_Lumos1)\
**Post date:** [29 January 2007 08:26 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/4 "2007-01-29T08:26:14Z")

</div>

Neville Franks \<subs@surfulater.com\> writes:

> Hi, I working on syntax highlighting for ED4W and have problem with \<\<  
> because it has three uses.
> 
> 1. \<\< operator  
> 2. \<\< HERE Doc  
> 3. class \<\< Name - Singleton Class
> 
> The problem is determining if \<\< starts a Here Doc. It is easy to handle  
> class\<\<Name is this regard, but I can't see how you can work out \<\< in  
> the following:
> 
> &nbsp;&nbsp;&nbsp;Ared = "mary"  
> &nbsp;&nbsp;&nbsp;angy = "bill"  
> &nbsp;&nbsp;&nbsp;angy \<\<Ared # does this start a Here Doc or is it operator \<\<  
> &nbsp;&nbsp;&nbsp;print \<\<Ared # as above.
> 
> Thanks.

Your example is incomplete.

&nbsp;&nbsp;angy \<\<Ared # is an operator  
&nbsp;&nbsp;print \<\<Ared # is a HERE doc...  
&nbsp;&nbsp;some string  
&nbsp;&nbsp;Ared # ...because it has an end marker

That's how the Emacs ruby-mode seems to do it.

Steve

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [1 February 2007 12:25 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/5 "2007-02-01T12:25:08Z")

</div>

There is a similar problem with "?" and "?:".

irb(main):001:0\> ?2  
=\> 50  
irb(main):002:0\> sep = "x" \* (true?2:1)  
SyntaxError: compile error  
(irb):2: parse error, unexpected ':', expecting ')'  
sep = "x" \* (true?2:1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2  
irb(main):003:0\> sep = "x" \* (true ?2:1)  
=\> "xx"

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [28 January 2007 21:06 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/6 "2007-01-28T21:06:33Z")

</div>

Stefano Crocco wrote:

> Alle 21:50, domenica 28 gennaio 2007, Neville Franks ha scritto:
> 
> > &nbsp;&nbsp;&nbsp;Ared = "mary"  
> > &nbsp;&nbsp;&nbsp;angy = "bill"  
> > &nbsp;&nbsp;&nbsp;angy \<\<Ared # does this start a Here Doc or is it operator \<\<  
> > &nbsp;&nbsp;&nbsp;print \<\<Ared # as above.
> > 
> > Thanks.
> > 
> > ---  
> > Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> Some editors (kate and vim, for example) assume the presence of a  
> whitespace  
> after the \<\< in the operator case:
> 
> a=  
> s="abc"  
> #this gets highlighted as a HERE Doc  
> a\<\<s
> 
> #this gets highlighted as an operator  
> a\<\< s
> 
> Stefano

Stefano,  
Thanks, but that isn't how the Ruby interpreter works, so this is really  
a hack that may or may not work with real code.

I can just do the same thing, but if possible I'd like a 'correct'  
solution.

I've been looking at all of the Ruby Editors on Windows and am surprised  
at how poorly they handle Ruby syntax. I would never release a product  
like these.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [28 January 2007 22:55 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/7 "2007-01-28T22:55:22Z")

</div>

Hi,

> Ooops! - I Think Ruby itself has some problems with it:

Perhaps Ruby is smarter than you expected.

> irb(main):007:0\> angy \<\<EOT  
> irb(main):008:0" xxxxx  
> irb(main):009:0" EOT  
> NameError: uninitialized constant EOT  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7

Ruby knows angy is a local variable, so that the interpreter consider  
it is more likely a shift operator than a here-doc. If you want to  
disambiguate, use parentheses.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

> **···**
>
> In message "Re: Ruby \<\< is ambiguos" on Mon, 29 Jan 2007 06:10:11 +0900, Wolfgang Nádasi-Donner \<wonado@donnerweb.de\> writes:

---

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [29 January 2007 23:30 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/8 "2007-01-29T23:30:17Z")

</div>

Steven Lumos wrote:

> Neville Franks \<subs@surfulater.com\> writes:
> 
> > &nbsp;&nbsp;&nbsp;Ared = "mary"  
> > &nbsp;&nbsp;&nbsp;angy = "bill"  
> > &nbsp;&nbsp;&nbsp;angy \<\<Ared # does this start a Here Doc or is it operator \<\<  
> > &nbsp;&nbsp;&nbsp;print \<\<Ared # as above.
> > 
> > Thanks.
> 
> Your example is incomplete.
> 
> &nbsp;&nbsp;angy \<\<Ared # is an operator  
> &nbsp;&nbsp;print \<\<Ared # is a HERE doc...  
> &nbsp;&nbsp;some string  
> &nbsp;&nbsp;Ared # ...because it has an end marker
> 
> That's how the Emacs ruby-mode seems to do it.
> 
> Steve

Steve, in that case Emacs ruby-mode is incorrect.

angy \<\<Ared # is an operator  
&nbsp;&nbsp;some string  
Ared

gives:

C:\ruby\bin\ruby.exe D:\Ed32\BrowserTest\RubyHereDoc3.rb  
D:/Ed32/BrowserTest/RubyHereDoc3.rb:3: undefined method `angy' for  
main:Object (NoMethodError)

To get this to compile, a method angry must be defined:

def angy var  
end

So the only way to determine if \<\< is a Here Doc is to know if the  
preceeding token is a method. And an editor can't know that.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [2 February 2007 01:28 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/9 "2007-02-02T01:28:34Z")

</div>

# There is a similar problem with "?" and "?:".  
# irb(main):001:0\> ?2  
# =\> 50

k

# irb(main):002:0\> sep = "x" \* (true?2:1)  
# SyntaxError: compile error  
# (irb):2: parse error, unexpected ':', expecting ')'  
# sep = "x" \* (true?2:1)  
# ^  
# from (irb):2

careful. "?" is a valid char for identifiers and is also used for inline if.  
stupid example,

irb(main):002:0\> def true?  
irb(main):003:1\> true  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> true?  
=\> true  
irb(main):006:0\> true? 2  
ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `true?'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6  
irb(main):007:0\> true?2  
ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7:in `true?'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7  
irb(main):008:0\> true?2:1  
SyntaxError: compile error  
(irb):8: parse error, unexpected ':', expecting $  
true?2:1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):8  
irb(main):009:0\> true?2  
ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):9:in `true?'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):9  
irb(main):011:0\> true? 2:1  
SyntaxError: compile error  
(irb):11: parse error, unexpected ':', expecting $  
true? 2:1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):11  
irb(main):012:0\> true? ? 2:1  
=\> 2  
irb(main):013:0\> true ? 2:1  
=\> 2

# irb(main):003:0\> sep = "x" \* (true ?2:1)  
# =\> "xx"

k.

kind regards -botp

> **···**
>
> From: Wolfgang Nádasi-Donner [[mailto:wonado@donnerweb.de](mailto:wonado@donnerweb.de)] :

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [28 January 2007 21:13 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/10 "2007-01-28T21:13:32Z")

</div>

You're definitely going to need heuristics like this in syntax highlighting Ruby source code. Only ruby can read Ruby, to borrow the Perl expression. 😉

James Edward Gray II

> **···**
>
> On Jan 28, 2007, at 3:06 PM, Neville Franks wrote:
> 
> > Thanks, but that isn't how the Ruby interpreter works, so this is really  
> > a hack that may or may not work with real code.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [29 January 2007 00:20 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/11 "2007-01-29T00:20:07Z")

</div>

Yukihiro Matsumoto schrieb:

> \>irb(main):007:0\> angy \<\<EOT  
> \>irb(main):008:0" xxxxx  
> \>irb(main):009:0" EOT  
> \>NameError: uninitialized constant EOT  
> \> from (irb):7
> 
> Ruby knows angy is a local variable, so that the interpreter consider  
> it is more likely a shift operator than a here-doc. If you want to  
> disambiguate, use parentheses.

I made an error there. "angy" followed by a String doesn't make any sense. It works very well if written in in a senseful way.

irb(main):001:0\> angy = "abc"  
=\> "abc"  
irb(main):002:0\> angy \<\<\<\<EOT  
irb(main):003:0" ddd  
irb(main):004:0" EOT  
=\> "abcddd\n"

But... - there is one thing I really don't understand in the first example. Ruby recognizes "EOT" as an uninitialized constant, because "\<\<" ist interpreted as shift operator. But why is an open here-doc string recognized in line 008:0" in the example?

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [29 January 2007 23:32 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/12 "2007-01-29T23:32:29Z")

</div>

Neville Franks wrote:

> ...  
> Steve, in that case Emacs ruby-mode is incorrect.
> 
> angy \<\<Ared # is an operator  
> &nbsp;&nbsp;some string  
> Ared
> 
> gives:
> 
> C:\ruby\bin\ruby.exe D:\Ed32\BrowserTest\RubyHereDoc3.rb  
> D:/Ed32/BrowserTest/RubyHereDoc3.rb:3: undefined method `angy' for  
> main:Object (NoMethodError)
> 
> To get this to compile, a method angry must be defined:
> 
> def angy var  
> end
> 
> So the only way to determine if \<\< is a Here Doc is to know if the  
> preceeding token is a method. And an editor can't know that.

PS. Also see my comment here:

[http://seclib.blogspot.com/2005/11/more-on-leftshift-and-heredoc.html](http://seclib.blogspot.com/2005/11/more-on-leftshift-and-heredoc.html)

> **···**
>
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [29 January 2007 23:45 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/13 "2007-01-29T23:45:13Z")

</div>

Hi,

> **···**
>
> In message "Re: Ruby \<\< is ambiguos" on Tue, 30 Jan 2007 08:30:17 +0900, Neville Franks \<subs@surfulater.com\> writes:
> 
> > Steve, in that case Emacs ruby-mode is incorrect.
> 
> Maybe Ruby is too smart for this issue, since it's difficult for  
> editors to determine here-doc from shift operators without serious  
> parsing.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [2 February 2007 07:20 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/14 "2007-02-02T07:20:08Z")

</div>

Peña schrieb:

> # There is a similar problem with "?" and "?:".  
> careful. "?" is a valid char for identifiers and is also used for inline if.  
> stupid example,  
> ...

This means, it is even more complex to write a code highlighter without analysing semantics.

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [29 January 2007 00:39 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/15 "2007-01-29T00:39:12Z")

</div>

> > \>irb(main):007:0\> angy \<\<EOT  
> > \>irb(main):008:0" xxxxx  
> > \>irb(main):009:0" EOT  
> > \>NameError: uninitialized constant EOT  
> > \> from (irb):7
> 
> But... - there is one thing I really don't understand in the first example. Ruby recognizes "EOT" as an uninitialized constant, because "\<\<" ist interpreted as shift operator. But why is an open here-doc string recognized in line 008:0" in the example?

Because the error happened on line 7? 🙂

Regards,

Bill

> **···**
>
> From: "Wolfgang Nádasi-Donner" \<wonado@donnerweb.de\>

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [29 January 2007 00:55 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/16 "2007-01-29T00:55:15Z")

</div>

Hi --

> Yukihiro Matsumoto schrieb:
> 
> > \>irb(main):007:0\> angy \<\<EOT  
> > \>irb(main):008:0" xxxxx  
> > \>irb(main):009:0" EOT  
> > \>NameError: uninitialized constant EOT  
> > \> from (irb):7
> > 
> > Ruby knows angy is a local variable, so that the interpreter consider  
> > it is more likely a shift operator than a here-doc. If you want to  
> > disambiguate, use parentheses.
> 
> I made an error there. "angy" followed by a String doesn't make any sense. It works very well if written in in a senseful way.
> 
> irb(main):001:0\> angy = "abc"  
> =\> "abc"  
> irb(main):002:0\> angy \<\<\<\<EOT  
> irb(main):003:0" ddd  
> irb(main):004:0" EOT  
> =\> "abcddd\n"
> 
> But... - there is one thing I really don't understand in the first example. Ruby recognizes "EOT" as an uninitialized constant, because "\<\<" ist interpreted as shift operator. But why is an open here-doc string recognized in line 008:0" in the example?

I'm not quite sure either, but it seems to interpret \<\<EOT as the  
start of a heredoc, and then later realize that it couldn't have been.  
Compare what happens if there's a space before EOT:

irb(main):012:0\> a = 1  
=\> 1  
irb(main):013:0\> a \<\< EOT  
NameError: uninitialized constant EOT  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):13

David

> **···**
>
> On Mon, 29 Jan 2007, Wolfgang Nádasi-Donner wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [29 January 2007 23:54 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/17 "2007-01-29T23:54:31Z")

</div>

Yukihiro Matsumoto wrote:

> Hi,
> 
> \>Steve, in that case Emacs ruby-mode is incorrect.
> 
> Maybe Ruby is too smart for this issue, since it's difficult for  
> editors to determine here-doc from shift operators without serious  
> parsing.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

Matz, that's precisely the conclusion I've come to. You shouldn't make  
it so hard for us folks that write editors. 🙂

> **···**
>
> > In message "Re: Ruby \<\< is ambiguos" \> on Tue, 30 Jan 2007 08:30:17 +0900, Neville Franks \> \<subs@surfulater.com\> writes:
> 
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [29 January 2007 02:01 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/18 "2007-01-29T02:01:56Z")

</div>

Thanks to everyone for their replies. It looks like the only practical  
way for me handle this is to assume \<\< is an operator if a space follows  
it, otherwise as a Here Doc.

Fortunately this is the only issue I've come across so far re. IDE  
syntax highlighting and I'm a fair way down the track.

> **···**
>
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [30 January 2007 22:35 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/19 "2007-01-30T22:35:43Z")

</div>

> &nbsp;&nbsp;This message is in MIME format. The first part should be readable text,  
> &nbsp;&nbsp;while the remaining parts are likely unreadable without MIME-aware tools.

> Hi --
> 
> \>Yukihiro Matsumoto schrieb:  
> \>\>\>irb(main):007:0\> angy \<\<EOT  
> \>\>\>irb(main):008:0" xxxxx  
> \>\>\>irb(main):009:0" EOT  
> \>\>\>NameError: uninitialized constant EOT  
> \>\>\> from (irb):7  
> \>\>  
> \>\>Ruby knows angy is a local variable, so that the interpreter consider  
> \>\>it is more likely a shift operator than a here-doc. If you want to  
> \>\>disambiguate, use parentheses.  
> \>  
> \>I made an error there. "angy" followed by a String doesn't make any sense.  
> \>It works very well if written in in a senseful way.  
> \>  
> \>irb(main):001:0\> angy = "abc"  
> \>=\> "abc"  
> \>irb(main):002:0\> angy \<\<\<\<EOT  
> \>irb(main):003:0" ddd  
> \>irb(main):004:0" EOT  
> \>=\> "abcddd\n"  
> \>  
> \>But... - there is one thing I really don't understand in the first  
> \>example. Ruby recognizes "EOT" as an uninitialized constant, because "\<\<"  
> \>ist interpreted as shift operator. But why is an open here-doc string  
> \>recognized in line 008:0" in the example?
> 
> I'm not quite sure either, but it seems to interpret \<\<EOT as the  
> start of a heredoc, and then later realize that it couldn't have been.  
> Compare what happens if there's a space before EOT:
> 
> irb(main):012:0\> a = 1  
> =\> 1  
> irb(main):013:0\> a \<\< EOT  
> NameError: uninitialized constant EOT  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):13

I suspect part of the problem is that irb lexes your input to determine  
the continuation prompt (and indeed if it should allow multiple lines of  
input before evalling). IOW, irb has the same problem our poor text  
editor's do 🙂

> **···**
>
> On Mon, Jan 29, 2007 at 09:55:15AM +0900, dblack@wobblini.net wrote:
> 
> > On Mon, 29 Jan 2007, Wolfgang Nádasi-Donner wrote:
> > 
> > David
> > 
> > --  
> > Q. What is THE Ruby book for Rails developers?  
> > A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> > &nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> > Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> > A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Karl\_von\_Laudermann](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karl\_von\_Laudermann](https://rubytalk.org/u/Karl_von_Laudermann)\
**Post date:** [31 January 2007 14:35 UTC](https://rubytalk.org/t/ruby-is-ambiguos/34883/20 "2007-01-31T14:35:10Z")

</div>

FWIW, here's how I implemented it in the ruby mode for jEdit. It  
recognizes the following as a here document:

1. The \<\< characters, optionally followed by a - character, followed  
by printable characters enclosed in single or double quotes. E.g.  
&nbsp;&nbsp;\<\<'hello'  
&nbsp;&nbsp;\<\<-'thingy67%'  
&nbsp;&nbsp;\<\<"foobar $"  
&nbsp;&nbsp;\<\<-"boofar @"

2. The \<\< characters, optionally followed by a - character, followed  
by letters and/or underscores. E.g.  
&nbsp;&nbsp;\<\<Hello\_there  
&nbsp;&nbsp;\<\<-Howdy

Looking at this now, that second case should probably have been  
letters \*and numbers\* and underscores, but not starting with a number,  
i.e. a valid identifier.

> **···**
>
> On Jan 28, 9:01 pm, Neville Franks \<s...@surfulater.com\> wrote:
> 
> > Thanks to everyone for their replies. It looks like the only practical  
> > way for me handle this is to assume \<\< is an operator if a space follows  
> > it, otherwise as a Here Doc.
