On Mar 14, 2006, at 9:17 PM, dblack / wobblini.net wrote:
> On Wed, 15 Mar 2006, Gary Wright wrote:
>
> Either is fine:
>
> irb(main):001:0> def x; class << String; self; end; end
> => nil

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
                 ^
         from (irb):12
irb(main):013:0>

>> I would think that
>>
>> 	class expression
>> 	   #code
>> 	end
>>
>> would be analogous to
>>
>> 	expression.class_eval { # code }
>
> Do you mean you'd like the class keyword not to start a new local
> scope?

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
	end
with
	class obj.singleton_class
         end

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, mental / rydia.net 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.


Gary Wright