[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Ruby << is ambiguos

Yukihiro Matsumoto

1/28/2007 10:55:00 PM

Hi,

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

|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
| 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.

matz.

6 Answers

WoNáDo

1/29/2007 12:18:00 AM

0

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

Bill Kelly

1/29/2007 12:39:00 AM

0

From: "Wolfgang Nádasi-Donner" <wonado@donnerweb.de>
>
>> |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




dblack

1/29/2007 12:55:00 AM

0

Neville Franks

1/29/2007 2:02:00 AM

0

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.g... http://www.surf...

--
Posted via http://www.ruby-....

Logan Capaldo

1/30/2007 10:36:00 PM

0

On Mon, Jan 29, 2007 at 09:55:15AM +0900, dblack@wobblini.net wrote:
> This message is in MIME format. The first part should be readable text,
> while the remaining parts are likely unreadable without MIME-aware tools.

> Hi --
>
> On Mon, 29 Jan 2007, Wolfgang Nádasi-Donner wrote:
>
> >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
> 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 :)
>
> David
>
> --
> Q. What is THE Ruby book for Rails developers?
> A. RUBY FOR RAILS by David A. Black (http://www.manning...)
> (See what readers are saying! http://www.r.../r...)
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
> A. Ruby Power and Light, LLC (http://www.r...)


Karl von Laudermann

1/31/2007 2:31:00 PM

0

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.

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.
<<'hello'
<<-'thingy67%'
<<"foobar $"
<<-"boofar @"

2. The << characters, optionally followed by a - character, followed
by letters and/or underscores. E.g.
<<Hello_there
<<-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.