From: "David A. Black" Date: 2004-12-01T10:14:40+09:00 Subject: Re: singleton methods : when are they not permitted? Hi -- On Wed, 1 Dec 2004, Francis Hwang wrote: > > 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?) > > > > irb(main):014:0> def ("x").foo; end > > SyntaxError: compile error > > (irb):14: can't define singleton method for literals > > > > However this can be worked around trivially by using an expression > > instead: > > > > irb(main):015:0> def (("x")).foo; end > > => nil > > 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. I think the difference is that (("x")) causes the expression inside the inner parens to be evaluated first, so by the time it's seen by def, it isn't a literal but the result of an evaluated expression. So def treats it as if it were seeing: a = "x" def (a).foo ... At least that's my reading of it. David -- David A. Black dblack@wobblini.net