Agreed.
IMHO class << expr should be no different than the normal class Name, but currently various things are allowed in that context such as return, yield, etc.
Constant resolution also becomes somewhat dynamically-scoped with class << expr which is also a weird exception.
So longer term maybe it would be good to deprecate class << entirely and use singleton_class.class_exec do instead.
Agreed.
IMHO class << expr should be no different than the normal class Name, but currently various things are allowed in that context such as return, yield, etc.
Constant resolution also becomes somewhat dynamically-scoped with class << expr which is also a weird exception.
So longer term maybe it would be good to deprecate class << entirely and use singleton_class.class_exec do instead.
or make it syntax sugre of singleton_class.class_exec do?
Both are easy to explain the rule. Now, it is somewhat unclear.
or make it syntax sugre of singleton_class.class_exec do?
I think that would be confusing as class (like module and def) is a keyword which normally adds a new lexical scope and does not capture the parent local variables.
We can break from class syntax.
I hope nobody use it :(
At least TruffleRuby doesn't implement it, and we had no bug report about this, so hopefully not used.
Agreed it should be deprecated/removed as it's confusing at best.
Another option would be to make everything use lexical scope, if the strangeness is indeed because locals are inaccessible inside class << self.
So imagine the following printing 1 2 3.
compile.c (check_yield_place): this function check the yield location.
show a warning if yield in class syntax. [Feature #15575]
do strict check for toplevel yield. Without this patch, 1.times{ yield } in toplevel is valid-syntax (raise LocalJumpError
at runtime) although toplevel simple yield is not valid syntax.
This patch make them syntax error.