Skip to content

Instantly share code, notes, and snippets.

@scalp42
Forked from ericallam/module_function.rb
Last active September 11, 2015 06:03
Show Gist options
  • Save scalp42/6ceb831987231af72e5f to your computer and use it in GitHub Desktop.
Save scalp42/6ceb831987231af72e5f to your computer and use it in GitHub Desktop.
Using module_function instead of extend self
# a lot of people in ruby do this
module Foo
extend self
def bar
'bar'
end
end
Foo.bar
# => 'bar'
class Baz; include Foo; end
Baz.new.bar
# => 'bar'
# but what they really should be doing is this:
module Foo
def bar
'bar'
end
module_function :bar
end
Foo.bar
# => 'bar'
class Baz; include Foo; end
Baz.new.bar
# => 'bar'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment