Project

General

Profile

Feature #12268

Open3 should use extend self instead of module_function calls

Added by DanRathbun (Dan Rathbun) about 3 years ago. Updated over 2 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:74882]

Description

Open3 should use extend self instead of module_function calls after defining each method.

Using module_function causes access issues when the Open3 module is mixed into other class or module namespaces. Ie, the very method proxies I wish to use in my "mixee" class, get created as private because of module_function in "open3.rb".

History

Updated by akr (Akira Tanaka) over 2 years ago

  • Status changed from Open to Rejected

module_function is the standard way to define module methods.

I think it is because "include mod" makes us possible to call module methods without module name,
as "popen3(...)" instead of "Open3.popen3(...)",
"sin(...)" instead of "Math.sin(...)", etc.,
without providing the methods as public API.

"extend self" style makes applications which uses "include mod" will provide the methods as public API
which is not intentional in most cases.
(Most applications should not provide popen3() or sin() method as public API.)

If you really want it, you can invoke "public" method.

% ruby -e '
module M
  def m
    :m
  end
  module_function :m
end
class C
  include M
end
p C.new.m
'
-e:11:in `<main>': private method `m' called for #<C:0x007f8b5fbd3d68> (NoMethodError)
% ruby -e '
module M
  def m
    :m
  end
  extend self       
end
class C
  include M
end
p C.new.m
'
:m
% ruby -e '
module M
  def m
    :m
  end
  module_function :m
end
class C
  include M
  public :m
end      
p C.new.m
'
:m

Also available in: Atom PDF