Skip to content

private/public/module_function without arguments, called on another module from a block, change the caller's visibility #133

Closed

Description

@Largo

private, public and module_function with no arguments, called with an explicit receiver (or through send) from a block inside a method, change the default visibility of the calling method's scope instead of nothing:

module M; end
class C; def foo; 1.times { M.send :module_function }; def yyy; end; end; end
C.new.foo
p C.public_instance_methods(false).sort
# CRuby:    [:foo, :yyy]
# IronRuby: TypeError: A module function cannot be defined on a class.
class Module; public :private; end
module M; end
class C; def foo; 1.times { M.private; def pri1; end }; def pub2; end; end; new.foo; end
p C.public_instance_methods(false).sort, C.private_instance_methods(false).sort
# CRuby:    [:foo, :pri1, :pub2] []
# IronRuby: [:foo] [:pri1, :pub2]

The visibility mode belongs to the receiver's own scope (here M's body, which isn't running), so CRuby changes nothing visible. IronRuby seems to apply it to the innermost Ruby scope of the caller.

Found by running the scripts PR #119 deleted (Tests/Experiments, Tests/Legacy) on CRuby 4.0.9 and on IronRuby built from master c7f70cf (net10.0, Linux). (Experiments/Methods/Visibility/s.rb, flags.rb)

Activity

  1. added a commit that references this issue on Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions