On Mar 14, 2006, at 9:17 PM, email@example.com wrote:
On Wed, 15 Mar 2006, Gary W. wrote:
Either is fine:
irb(main):001:0> def x; class << String; self; end; end
A singleton class/end block is parsed and evaluated as an expression.
But a regular class/end block is not accepted by the parser:
irb(main):012:0> def x; (class A; self; end); end
SyntaxError: compile error
(irb):12: class definition in method body
def x; (class A; self; end); end
Do you mean you’d like the class keyword not to start a new local
No, I realize the scoping rules are different and useful. I was
just pointing out how the Ruby parser forbids expressions after the
class keyword. If Kernel#singleton_class was defined I had in my mind
that you could replace:
class << obj
if you allowed an expression after the class keyword thus getting
rid of the << syntax which seems ‘foreign’ to me relative to the rest
of the Ruby syntax.
Anyway, firstname.lastname@example.org pointed out the ambiguity that would create
with respect to parsing an arbitrary expression vs. the ancestor
declaration syntax (Sub < Super).
So even with Kernel#singleton_class you would still need to use
the ‘class << obj’ notation if you want to open the singleton class
and avoid creating a closure as occurs with the block syntax.