rubocop / rubocop/ruby-style-guide

`extend self` and `module_function` are not the same

Open
#556 35 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
16.5k
Forks
3.3k
PR merge metrics
No merged PRs in 30d

Description

extend self and module_function are not the same. The guide says:

Favor the use of module_function over extend self when you want to turn a module's instance methods into class methods.
https://github.com/bbatsov/ruby-style-guide#module-function

Unfortunately, extend self keeps the instance methods public, but module_function makes them private.

# `extend self` makes the copies public
module Pub
  extend self
  def x; end
end

class PubClass
  include Pub
end

PubClass.new.x #=> nil

# `module_function` makes the copies private
module Priv
  def x; end
  module_function :x
end

class PrivClass
  include Priv
end

PrivClass.new.x
# NoMethodError: private method `x' called for #<PrivClass:0x007fba1c0e7e90>

Is making them private the point?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the module_function section in the linked Ruby style guide and review the examples in this issue. Determine whether the guidance should explain the visibility difference or be changed; done means the section clearly and accurately describes the relationship between extend self and module_function.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.