Skip to content

Instantly share code, notes, and snippets.

@gabebw
Created March 4, 2012 01:16
Show Gist options
  • Select an option

  • Save gabebw/1969764 to your computer and use it in GitHub Desktop.

Select an option

Save gabebw/1969764 to your computer and use it in GitHub Desktop.
module_function is not the same as self.method
# This works
module TestOne
def module_method
puts "TestOne works"
end
module_function :module_method
class A
include TestOne
def instance_method
module_method
end
end
end
# This fails
module TestTwo
def self.module_method
puts "TestTwo fails"
end
class A
include TestTwo
def instance_method
module_method
end
end
end
TestOne::A.new.instance_method
TestTwo::A.new.instance_method
# Output:
#
# TestOne works
# module_function.rb:25:in `instance_method': undefined local variable or method `module_method' for #<TestTwo::A:0x0000010084d400> (NameError)
# from module_function.rb:31:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment