From: "Eregon (Benoit Daloze) via ruby-core" Date: 2026-07-13T21:20:49+00:00 Subject: [ruby-core:126078] [Ruby Misc#22192] Clarify the scope of default visibility changed inside a block Issue #22192 has been updated by Eregon (Benoit Daloze). TruffleRuby also behaves like CRuby here. My understanding is method visibility is some state that only exists in methods and not in blocks (same as `$~`, etc). There should already be some ruby/spec for that. ---------------------------------------- Misc #22192: Clarify the scope of default visibility changed inside a block https://bugs.ruby-lang.org/issues/22192#change-118079 * Author: shugo (Shugo Maeda) * Status: Assigned * Assignee: matz (Yukihiro Matsumoto) ---------------------------------------- `private` etc. called with no arguments inside a block changes the default visibility of the enclosing lexical scope, and the change survives the block: ```ruby class C %i[foo bar].each do |name| private define_method(name) { } end def afterwards; end # also private end C.private_method_defined?(:foo) #=> true C.private_method_defined?(:afterwards) #=> true ``` When multiple Procs share the same lexical scope, `private` in a Proc affects all Procs: ```ruby class Host SET_PRIVATE = proc { private } DEFINE = proc { def probe; end } end Host::SET_PRIVATE.call Host::DEFINE.call Host.private_method_defined?(:probe) #=> true ``` Neither CRuby nor ruby/spec has corresponding tests. I assume this is intentional, but am I correct? It seems that the visibility change is effective only within the block on JRuby, so I'd like to clarify this. -- 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/