Bug #8261

module_function for methods of same name

Added by Michael Fellinger about 1 year ago. Updated about 1 year ago.

[ruby-core:54223]
Status:Rejected
Priority:Low
Assignee:-
Category:core
Target version:2.1.0
ruby -v:ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux] Backport:

Description

This affects all versions from 1.8 to 2.0 I have tested.

I'd expect all three examples below to have the same output, but not only does the example with module_function end up with a private instance method, it also has the wrong one.

If the name of the method is different, it behaves as one would expect, this only happens if the methods have the same name.

module A
def a
1
end

module_function

def a
2
end
end

class X
include A
end

module B
def b
1
end

def self.b
2
end
end

class Y
include B
end

module C
def c
1
end

class << self
def c
2
end
end
end

class Z
include C
end

p [A.a, X.new.send(:a)]

=> [2, 2]

p [B.b, Y.new.b]

=> [2, 1]

p [C.c, Z.new.c]

=> [2, 1]

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

  • Status changed from Open to Rejected

Correct behavior.

#2 Updated by Marc-Andre Lafortune about 1 year ago

Right. Unlike private, protected, public, the method module_function called with no arguments does nothing.

Please refer to the doc http://ruby-doc.org/core-2.0/Module.html#method-i-module_function vs http://ruby-doc.org/core-2.0/Module.html#method-i-private

Also available in: Atom PDF