Skip to content

Unexpected behavior when using module_function followed by define_method #6854

@mspoehr

Description

@mspoehr

Environment Information

$ jruby -v
jruby 9.3.0.0 (2.6.8) 2021-09-17 85c20e780f OpenJDK 64-Bit Server VM 11.0.11+9 on 11.0.11+9 +jit [darwin-x86_64]
$ uname -a
Darwin HOSTNAME 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64
  • No special environment variables set.

Expected Behavior

Example 1:

Using define_method in a module after calling module_function creates public module functions.

module Mod
  module_function
  define_method 'test' do
  end
end

Mod.test

Example 2:

Using define_method in a loop after calling module_function creates multiple public module functions.

module Mod
  module_function
  ['a', 'b'].each do |method_name|
    define_method method_name do
    end
  end
end

Mod.a
Mod.b

I would expect both examples to run without errors, and both do on MRI Ruby 2.5.9, 2.6.8, and JRuby 9.2.19.0.

Actual Behavior

On JRuby 9.3.0.0, I get the following

Example 1:

NoMethodError: private method `test' called for Mod:Module
  <main> at example1.rb:7

Example 2:

NoMethodError: undefined method `a' for Mod:Module
  <main> at example2.rb:9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions