"David A. Black" <dblack / wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.44.0411301808310.27136-100000 / wobblini...
> Hi --
>
> On Wed, 1 Dec 2004, Christoph wrote:
>
> > Francis Hwang schrieb:
> >
> > >
> > > On Nov 30, 2004, at 6:12 PM, Florian Gross wrote:
> > >
> > >> It also seems not to be possible to directly add methods to
literals
> > >> like this: (Not sure why -- maybe Ruby optimizes literals?)
> > >
> > Pardon my igmorance but what is a literal?
> >
> > > Errrr. So what's the difference between
> > >
> > > def ("x").foo; end
> > >
> > > and
> > >
> > > def (("x")).foo; end
> > >
> > > Why do the extra parens make the parser treat it differently? My
head
> > > hurts.
> >
> > Mine too - I find these error messages rather missleading
> > after all both
> >
> > ("x") == "x"
> >
> > (("x")) == "x"
> >
> > evalute to true
>
> So does:
>
>   a = "x"
>   a == "x"
>
> but still, during parsing a bare identifier like 'a' and a literal
> like '"x"' are not treated the same way.

You can view "x" as an object constructor (much similar to 1, [] {1=>2}
etc.).  Different from 1 "x" creates new instances every time it is
executed:

09:24:22 [robert.klemme]: ruby -e '10.times { p "x".id }'
134690520
134690496
134690472
134690448
134690424
134690400
134690376
134690352
134690328
134690304

That explains why a) it doesn't make sense to do 'def "x".foo()...end'
(regardless of the number of brackets around "x") and b) Ruby issues the
warning / error message.

Kind regards

    robert