From: "zverok (Victor Shepelev) via ruby-core" Date: 2024-07-16T12:14:25+00:00 Subject: [ruby-core:118617] [Ruby master Bug#20637] SyntaxError class definition in method body can be bypassed Issue #20637 has been updated by zverok (Victor Shepelev). As far as I understand, that���s legitimate code that works and might be useful for some metaprogramming: ```ruby def f(o) class << o class ::A; end # Syntax OK module B; end # Syntax OK end end o = Object.new f(o) o.singleton_class::B #=> #::B A #=> A ``` ---------------------------------------- Bug #20637: SyntaxError class definition in method body can be bypassed https://bugs.ruby-lang.org/issues/20637#change-109145 * Author: tompng (tomoya ishida) * Status: Open * ruby -v: ruby 3.4.0dev (2024-07-11T06:59:45Z master a1f7432550) [x86_64-linux] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Class definition in method body is prohibited in Ruby ~~~ruby def f class ::A; end # class definition in method body (SyntaxError) module B; end # module definition in method body (SyntaxError) end ~~~ But it can be bypassed by using `class <<` ~~~ruby def f class << Object.new class ::A; end # Syntax OK module B; end # Syntax OK end end ~~~ -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/